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 ObjectNumberFixedLength extends AbstractMP3Object { |
13 | |
14 | int length = 0; |
15 | |
16 | /** |
17 | * Creates a new ObjectNumberFixedLength object. |
18 | */ |
19 | public ObjectNumberFixedLength(final String identifier, final int size) { |
20 | if (size < 0) { |
21 | throw new IllegalArgumentException("Length is less than zero: " + this.length); |
22 | } |
23 | this.length = size; |
24 | this.identifier = identifier; |
25 | } |
26 | |
27 | /** |
28 | * Creates a new ObjectNumberFixedLength object. |
29 | */ |
30 | public ObjectNumberFixedLength(final ObjectNumberFixedLength copyObject) { |
31 | super(copyObject); |
32 | this.length = copyObject.length; |
33 | } |
34 | |
35 | public int getLength() { |
36 | return this.length; |
37 | } |
38 | |
39 | public void setSize(final int length) { |
40 | if (length > 0) { |
41 | this.length = length; |
42 | } |
43 | } |
44 | |
45 | public int getSize() { |
46 | return this.length; |
47 | } |
48 | |
49 | public boolean equals(final Object obj) { |
50 | if ((obj instanceof ObjectNumberFixedLength) == false) { |
51 | return false; |
52 | } |
53 | final ObjectNumberFixedLength objectNumberFixedLength = (ObjectNumberFixedLength) obj; |
54 | if (this.length != objectNumberFixedLength.length) { |
55 | return false; |
56 | } |
57 | return super.equals(obj); |
58 | } |
59 | |
60 | public void readByteArray(final byte[] arr, final int offset) { |
61 | if (arr == null) { |
62 | throw new NullPointerException("Byte array is null"); |
63 | } |
64 | if ((offset < 0) || (offset >= arr.length)) { |
65 | throw new IndexOutOfBoundsException("Offset to byte array is out of bounds: offset = " + |
66 | offset + |
67 | ", array.length = " + |
68 | arr |
69 | .length); |
70 | } |
71 | long lvalue = 0; |
72 | for (int i = offset; i < (offset + this.length); i++) { |
73 | lvalue <<= 8; |
74 | lvalue += arr[i]; |
75 | } |
76 | this.value = new Long(lvalue); |
77 | } |
78 | |
79 | public void readString(final String str, final int offset) { |
80 | if (str == null) { |
81 | throw new NullPointerException("Number string is null"); |
82 | } |
83 | if ((offset < 0) || (offset >= str.length())) { |
84 | throw new IndexOutOfBoundsException("Offset to number string is out of bounds: offset = " + |
85 | offset + |
86 | ", string.length()" + |
87 | str.length()); |
88 | } |
89 | this.value = Long.getLong(str.substring(offset)); |
90 | } |
91 | |
92 | public String toString() { |
93 | if (this.value == null) { |
94 | return ""; |
95 | } |
96 | return this.value.toString(); |
97 | } |
98 | |
99 | public byte[] writeByteArray() { |
100 | final byte[] arr; |
101 | arr = new byte[this.length]; |
102 | if (this.value != null) { |
103 | long temp = TagUtility.getWholeNumber(this.value); |
104 | for (int i = this.length - 1; i >= 0; i--) { |
105 | arr[i] = (byte) (temp & 0xFF); |
106 | temp >>= 8; |
107 | } |
108 | } |
109 | return arr; |
110 | } |
111 | |
112 | public String writeString() { |
113 | if (this.value == null) { |
114 | return String.valueOf(new char[this.length]); |
115 | } |
116 | return this.value.toString(); |
117 | } |
118 | } |