| 1 | package org.farng.mp3.object; |
| 2 | |
| 3 | import org.farng.mp3.TagUtility; |
| 4 | |
| 5 | import java.util.ArrayList; |
| 6 | import java.util.Iterator; |
| 7 | |
| 8 | /** |
| 9 | * ID3v2 and Lyrics3v2 tags have individual fields <code>AbstractMP3Fragment</code>s Then each fragment is broken down |
| 10 | * in to individual <code>AbstractMP3Object</code>s |
| 11 | * |
| 12 | * @author Eric Farng |
| 13 | * @version $Revision: 1.5 $ |
| 14 | */ |
| 15 | public class ObjectGroupRepeated extends AbstractMP3Object { |
| 16 | |
| 17 | private ArrayList objectList; |
| 18 | private ArrayList propertyList; |
| 19 | |
| 20 | /** |
| 21 | * Creates a new ObjectGroupRepeated object. |
| 22 | */ |
| 23 | public ObjectGroupRepeated(final String identifier) { |
| 24 | this.identifier = identifier; |
| 25 | this.propertyList = new ArrayList(); |
| 26 | this.objectList = new ArrayList(); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Creates a new ObjectGroupRepeated object. |
| 31 | */ |
| 32 | public ObjectGroupRepeated(final ObjectGroupRepeated copyObject) { |
| 33 | super(copyObject); |
| 34 | AbstractMP3Object newObject; |
| 35 | for (int i = 0; i < copyObject.objectList.size(); i++) { |
| 36 | newObject = (AbstractMP3Object) TagUtility.copyObject(copyObject.objectList.get(i)); |
| 37 | this.objectList.add(newObject); |
| 38 | } |
| 39 | for (int i = 0; i < copyObject.propertyList.size(); i++) { |
| 40 | newObject = (AbstractMP3Object) TagUtility.copyObject(copyObject.propertyList.get(i)); |
| 41 | this.propertyList.add(newObject); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | public ArrayList getObjectList() { |
| 46 | return this.objectList; |
| 47 | } |
| 48 | |
| 49 | public ArrayList getPropertyList() { |
| 50 | return this.propertyList; |
| 51 | } |
| 52 | |
| 53 | public int getSize() { |
| 54 | int size = 0; |
| 55 | AbstractMP3Object object; |
| 56 | final Iterator iterator = this.objectList.listIterator(); |
| 57 | while (iterator.hasNext()) { |
| 58 | object = (AbstractMP3Object) iterator.next(); |
| 59 | size += object.getSize(); |
| 60 | } |
| 61 | return size; |
| 62 | } |
| 63 | |
| 64 | public void addObject(final AbstractMP3Object object) { |
| 65 | this.objectList.add(object); |
| 66 | } |
| 67 | |
| 68 | public void addProperty(final AbstractMP3Object object) { |
| 69 | this.propertyList.add(object); |
| 70 | } |
| 71 | |
| 72 | public boolean equals(final Object obj) { |
| 73 | if ((obj instanceof ObjectGroupRepeated) == false) { |
| 74 | return false; |
| 75 | } |
| 76 | final ObjectGroupRepeated objectGroupRepeated = (ObjectGroupRepeated) obj; |
| 77 | if (this.objectList.equals(objectGroupRepeated.objectList) == false) { |
| 78 | return false; |
| 79 | } |
| 80 | if (this.propertyList.equals(objectGroupRepeated.propertyList) == false) { |
| 81 | return false; |
| 82 | } |
| 83 | return super.equals(obj); |
| 84 | } |
| 85 | |
| 86 | public void readByteArray(final byte[] arr, int offset) { |
| 87 | if (arr == null) { |
| 88 | throw new NullPointerException("Byte array is null"); |
| 89 | } |
| 90 | if ((offset < 0) || (offset >= arr.length)) { |
| 91 | throw new IndexOutOfBoundsException("Offset to byte array is out of bounds: offset = " + |
| 92 | offset + |
| 93 | ", array.length = " + |
| 94 | arr |
| 95 | .length); |
| 96 | } |
| 97 | AbstractMP3Object object; |
| 98 | Class className; |
| 99 | Iterator iterator; |
| 100 | if (this.propertyList.size() > 0) { |
| 101 | while (offset < arr.length) { |
| 102 | iterator = this.propertyList.listIterator(); |
| 103 | while (iterator.hasNext()) { |
| 104 | className = iterator.next().getClass(); |
| 105 | try { |
| 106 | object = (AbstractMP3Object) className.newInstance(); |
| 107 | this.objectList.add(object); |
| 108 | object.readByteArray(arr, offset); |
| 109 | offset += object.getSize(); |
| 110 | } catch (IllegalAccessException ex) { |
| 111 | ex.printStackTrace(); |
| 112 | |
| 113 | // do nothing, just skip this one |
| 114 | } catch (InstantiationException ex) { |
| 115 | ex.printStackTrace(); |
| 116 | |
| 117 | // do nothing, just skip this one |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | public String toString() { |
| 125 | String str = ""; |
| 126 | AbstractMP3Object object; |
| 127 | final Iterator iterator = this.objectList.listIterator(); |
| 128 | while (iterator.hasNext()) { |
| 129 | object = (AbstractMP3Object) iterator.next(); |
| 130 | str += (object.toString() + "\n"); |
| 131 | } |
| 132 | return str; |
| 133 | } |
| 134 | |
| 135 | public byte[] writeByteArray() { |
| 136 | AbstractMP3Object object; |
| 137 | final byte[] totalArray = new byte[this.getSize()]; |
| 138 | byte[] objectArray; |
| 139 | final Iterator iterator = this.objectList.listIterator(); |
| 140 | while (iterator.hasNext()) { |
| 141 | object = (AbstractMP3Object) iterator.next(); |
| 142 | objectArray = object.writeByteArray(); |
| 143 | System.arraycopy(objectArray, 0, totalArray, 0, totalArray.length); |
| 144 | } |
| 145 | return totalArray; |
| 146 | } |
| 147 | } |