| 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 ObjectStringFixedLength extends AbstractMP3Object { |
| 13 | |
| 14 | int length = 0; |
| 15 | |
| 16 | /** |
| 17 | * Creates a new ObjectStringFixedLength object. |
| 18 | */ |
| 19 | public ObjectStringFixedLength(final String identifier, final int length) { |
| 20 | if (length < 0) { |
| 21 | throw new IllegalArgumentException("length is less than zero: " + length); |
| 22 | } |
| 23 | this.identifier = identifier; |
| 24 | this.length = length; |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Creates a new ObjectStringFixedLength object. |
| 29 | */ |
| 30 | public ObjectStringFixedLength(final ObjectStringFixedLength copyObject) { |
| 31 | super(copyObject); |
| 32 | this.length = copyObject.length; |
| 33 | } |
| 34 | |
| 35 | public void setLength(final int size) { |
| 36 | this.length = size; |
| 37 | } |
| 38 | |
| 39 | public int getLength() { |
| 40 | return this.length; |
| 41 | } |
| 42 | |
| 43 | public int getSize() { |
| 44 | return this.length; |
| 45 | } |
| 46 | |
| 47 | public boolean equals(final Object obj) { |
| 48 | if ((obj instanceof ObjectStringFixedLength) == false) { |
| 49 | return false; |
| 50 | } |
| 51 | final ObjectStringFixedLength objectStringFixedLength = (ObjectStringFixedLength) obj; |
| 52 | if (this.length != objectStringFixedLength.length) { |
| 53 | return false; |
| 54 | } |
| 55 | return super.equals(obj); |
| 56 | } |
| 57 | |
| 58 | public void readString(final String str, final int offset) { |
| 59 | if (str == null) { |
| 60 | throw new NullPointerException("String is null"); |
| 61 | } |
| 62 | if ((offset < 0) || (offset >= str.length())) { |
| 63 | throw new IndexOutOfBoundsException("Offset to String is out of bounds: offset = " + |
| 64 | offset + |
| 65 | ", string.length()" + |
| 66 | str.length()); |
| 67 | } |
| 68 | this.value = str.substring(offset, this.length + offset); |
| 69 | } |
| 70 | |
| 71 | public String toString() { |
| 72 | return writeString(); |
| 73 | } |
| 74 | |
| 75 | public String writeString() { |
| 76 | final String str; |
| 77 | if (this.value == null) { |
| 78 | str = String.valueOf(new char[this.length]); |
| 79 | } else { |
| 80 | final int vlength = ((String) this.value).length(); |
| 81 | if (vlength > this.length) { |
| 82 | str = ((String) this.value).substring(0, this.length); |
| 83 | } else if (vlength == this.length) { |
| 84 | str = (String) this.value; |
| 85 | } else { |
| 86 | str = TagUtility.padString((String) this.value, this.length, ' ', false); |
| 87 | } |
| 88 | } |
| 89 | return str; |
| 90 | } |
| 91 | } |