1 | package org.farng.mp3.object; |
2 | |
3 | import org.farng.mp3.TagUtility; |
4 | |
5 | /** |
6 | * ID3v2 and Lyrics3v2 tags have individual fields <code>AbstractMP3Fragment</code>s Then each fragment is broken down |
7 | * in to individual <code>AbstractMP3Object</code>s |
8 | * |
9 | * @author Eric Farng |
10 | * @version $Revision: 1.5 $ |
11 | */ |
12 | public class ObjectNumberVariableLength extends AbstractMP3Object { |
13 | |
14 | int minLength = 1; |
15 | |
16 | /** |
17 | * Creates a new ObjectNumberVariableLength object. |
18 | */ |
19 | public ObjectNumberVariableLength(final String identifier, final int minimumSize) { |
20 | this.identifier = identifier; |
21 | if (minimumSize > 0) { |
22 | this.minLength = minimumSize; |
23 | } |
24 | } |
25 | |
26 | /** |
27 | * Creates a new ObjectNumberVariableLength object. |
28 | */ |
29 | public ObjectNumberVariableLength(final ObjectNumberVariableLength copyObject) { |
30 | super(copyObject); |
31 | this.minLength = copyObject.minLength; |
32 | } |
33 | |
34 | public int getMaximumLenth() { |
35 | return 8; |
36 | } |
37 | |
38 | public int getMinimumLength() { |
39 | return this.minLength; |
40 | } |
41 | |
42 | public void setMinimumSize(final int minimumSize) { |
43 | if (minimumSize > 0) { |
44 | this.minLength = minimumSize; |
45 | } |
46 | } |
47 | |
48 | public int getSize() { |
49 | if (this.value == null) { |
50 | return 0; |
51 | } |
52 | int current; |
53 | long temp = TagUtility.getWholeNumber(this.value); |
54 | int size = 0; |
55 | for (int i = 1; i <= 8; i++) { |
56 | current = (byte) temp & 0xFF; |
57 | if (current != 0) { |
58 | size = i; |
59 | } |
60 | temp >>= 8; |
61 | } |
62 | return (this.minLength > size) ? this.minLength : size; |
63 | } |
64 | |
65 | public boolean equals(final Object obj) { |
66 | if ((obj instanceof ObjectNumberVariableLength) == false) { |
67 | return false; |
68 | } |
69 | final ObjectNumberVariableLength objectNumberVariableLength = (ObjectNumberVariableLength) obj; |
70 | if (this.minLength != objectNumberVariableLength.minLength) { |
71 | return false; |
72 | } |
73 | return super.equals(obj); |
74 | } |
75 | |
76 | public void readByteArray(final byte[] arr, final int offset) { |
77 | if (arr == null) { |
78 | throw new NullPointerException("Byte array is null"); |
79 | } |
80 | if ((offset < 0) || (offset >= arr.length)) { |
81 | throw new IndexOutOfBoundsException("Offset to byte array is out of bounds: offset = " + |
82 | offset + |
83 | ", array.length = " + |
84 | arr |
85 | .length); |
86 | } |
87 | long lvalue = 0; |
88 | for (int i = offset; i < arr.length; i++) { |
89 | lvalue <<= 8; |
90 | lvalue += arr[i]; |
91 | } |
92 | this.value = new Long(lvalue); |
93 | } |
94 | |
95 | public void readString(final String str, final int offset) { |
96 | if (str == null) { |
97 | throw new NullPointerException("Number string is null"); |
98 | } |
99 | if ((offset < 0) || (offset >= str.length())) { |
100 | throw new IndexOutOfBoundsException("Offset to number string is out of bounds: offset = " + |
101 | offset + |
102 | ", string.length()" + |
103 | str.length()); |
104 | } |
105 | this.value = Long.getLong(str.substring(offset)); |
106 | } |
107 | |
108 | public String toString() { |
109 | if (this.value == null) { |
110 | return ""; |
111 | } |
112 | return this.value.toString(); |
113 | } |
114 | |
115 | public byte[] writeByteArray() { |
116 | final int size = getSize(); |
117 | final byte[] arr; |
118 | if (size == 0) { |
119 | arr = new byte[0]; |
120 | } else { |
121 | long temp = TagUtility.getWholeNumber(this.value); |
122 | arr = new byte[size]; |
123 | for (int i = size - 1; i >= 0; i--) { |
124 | arr[i] = (byte) (temp & 0xFF); |
125 | temp >>= 8; |
126 | } |
127 | } |
128 | return arr; |
129 | } |
130 | |
131 | public String writeString() { |
132 | if (this.value == null) { |
133 | return ""; |
134 | } |
135 | return this.value.toString(); |
136 | } |
137 | } |