-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathID3v2CommentFrameData.java
More file actions
155 lines (137 loc) · 4.49 KB
/
Copy pathID3v2CommentFrameData.java
File metadata and controls
155 lines (137 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package Mp3File;
import java.io.UnsupportedEncodingException;
public class ID3v2CommentFrameData extends AbstractID3v2FrameData {
private static final String DEFAULT_LANGUAGE = "eng";
private String language;
private EncodedText description;
private EncodedText comment;
public ID3v2CommentFrameData(boolean unsynchronisation) {
super(unsynchronisation);
}
public ID3v2CommentFrameData(boolean unsynchronisation, String language, EncodedText description, EncodedText comment) {
super(unsynchronisation);
if (description != null && comment != null && description.getTextEncoding() != comment.getTextEncoding()) {
throw new IllegalArgumentException("description and comment must have same text encoding");
}
this.language = language;
this.description = description;
this.comment = comment;
}
public ID3v2CommentFrameData(boolean unsynchronisation, byte[] bytes) throws InvalidDataException {
super(unsynchronisation);
synchroniseAndUnpackFrameData(bytes);
}
@Override
protected void unpackFrameData(byte[] bytes) throws InvalidDataException {
try {
language = BufferTools.byteBufferToString(bytes, 1, 3);
} catch (UnsupportedEncodingException e) {
language = "";
}
int marker = BufferTools.indexOfTerminatorForEncoding(bytes, 4, bytes[0]);
if (marker >= 4) {
description = new EncodedText(bytes[0], BufferTools.copyBuffer(bytes, 4, marker - 4));
marker += description.getTerminator().length;
} else {
description = new EncodedText(bytes[0], "");
marker = 4;
}
comment = new EncodedText(bytes[0], BufferTools.copyBuffer(bytes, marker, bytes.length - marker));
}
@Override
protected byte[] packFrameData() {
byte[] bytes = new byte[getLength()];
if (comment != null) bytes[0] = comment.getTextEncoding();
else bytes[0] = 0;
String langPadded;
if (language == null) {
langPadded = DEFAULT_LANGUAGE;
} else if (language.length() > 3) {
langPadded = language.substring(0, 3);
} else {
langPadded = BufferTools.padStringRight(language, 3, '\00');
}
try {
BufferTools.stringIntoByteBuffer(langPadded, 0, 3, bytes, 1);
} catch (UnsupportedEncodingException e) {
}
int marker = 4;
if (description != null) {
byte[] descriptionBytes = description.toBytes(true, true);
BufferTools.copyIntoByteBuffer(descriptionBytes, 0, descriptionBytes.length, bytes, marker);
marker += descriptionBytes.length;
} else {
byte[] terminatorBytes = comment != null ? comment.getTerminator() : new byte[]{0};
BufferTools.copyIntoByteBuffer(terminatorBytes, 0, terminatorBytes.length, bytes, marker);
marker += terminatorBytes.length;
}
if (comment != null) {
byte[] commentBytes = comment.toBytes(true, false);
BufferTools.copyIntoByteBuffer(commentBytes, 0, commentBytes.length, bytes, marker);
}
return bytes;
}
@Override
protected int getLength() {
int length = 4;
if (description != null) length += description.toBytes(true, true).length;
else length += comment != null ? comment.getTerminator().length : 1;
if (comment != null) length += comment.toBytes(true, false).length;
return length;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
public EncodedText getComment() {
return comment;
}
public void setComment(EncodedText comment) {
this.comment = comment;
}
public EncodedText getDescription() {
return description;
}
public void setDescription(EncodedText description) {
this.description = description;
}
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + ((comment == null) ? 0 : comment.hashCode());
result = prime * result
+ ((description == null) ? 0 : description.hashCode());
result = prime * result
+ ((language == null) ? 0 : language.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
ID3v2CommentFrameData other = (ID3v2CommentFrameData) obj;
if (comment == null) {
if (other.comment != null)
return false;
} else if (!comment.equals(other.comment))
return false;
if (description == null) {
if (other.description != null)
return false;
} else if (!description.equals(other.description))
return false;
if (language == null) {
if (other.language != null)
return false;
} else if (!language.equals(other.language))
return false;
return true;
}
}