EMMA Coverage Report (generated Tue Mar 14 21:50:42 EST 2006)
[all classes][org.farng.mp3]

COVERAGE SUMMARY FOR SOURCE FILE [AbstractMP3FragmentBody.java]

nameclass, %method, %block, %line, %
AbstractMP3FragmentBody.java100% (1/1)81%  (13/16)76%  (377/494)76%  (100/132)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class AbstractMP3FragmentBody100% (1/1)81%  (13/16)76%  (377/494)76%  (100/132)
getBriefDescription (): String 0%   (0/1)0%   (0/47)0%   (0/12)
getDescription (): String 0%   (0/1)0%   (0/45)0%   (0/12)
getObjectListIterator (): Iterator 0%   (0/1)0%   (0/4)0%   (0/1)
has6ByteHeader (): boolean 100% (1/1)89%  (80/90)86%  (18/21)
read (RandomAccessFile): void 100% (1/1)89%  (40/45)92%  (11/12)
equals (Object): boolean 100% (1/1)90%  (18/20)83%  (5/6)
isSubsetOf (Object): boolean 100% (1/1)91%  (39/43)82%  (9/11)
AbstractMP3FragmentBody (): void 100% (1/1)100% (11/11)100% (4/4)
AbstractMP3FragmentBody (AbstractMP3FragmentBody): void 100% (1/1)100% (30/30)100% (8/8)
appendToObjectList (AbstractMP3Object): void 100% (1/1)100% (6/6)100% (2/2)
getObject (String): Object 100% (1/1)100% (26/26)100% (8/8)
getSize (): int 100% (1/1)100% (21/21)100% (6/6)
iterator (): Iterator 100% (1/1)100% (4/4)100% (1/1)
setObject (String, Object): void 100% (1/1)100% (23/23)100% (7/7)
toString (): String 100% (1/1)100% (53/53)100% (14/14)
write (RandomAccessFile): void 100% (1/1)100% (26/26)100% (8/8)

1package org.farng.mp3;
2 
3import org.farng.mp3.id3.ID3v2_2;
4import org.farng.mp3.id3.ID3v2_3;
5import org.farng.mp3.id3.ID3v2_4;
6import org.farng.mp3.object.AbstractMP3Object;
7 
8import java.io.IOException;
9import java.io.RandomAccessFile;
10import java.util.ArrayList;
11import java.util.Iterator;
12import java.util.List;
13 
14/**
15 * This class is contained in the <code>AbstractMP3Fragment</code> and represents the actual data of tags. It contains
16 * default get/set methods for all data objects. The data is represented as an ArrayList of <code>MP3Object</code>.
17 * ID3v2 tags have frames. Lyrics3 tags have fields. ID3v1 tags do not have fragments.
18 *
19 * @author Eric Farng
20 * @version $Revision: 1.6 $
21 */
22public abstract class AbstractMP3FragmentBody extends AbstractMP3FileItem {
23 
24    private static final int SIZE_OBJECT_LIST = 16;
25    private static final int SIZE_BRIEF_DESCRIPTION = 64;
26    private static final int SIZE_DESCRIPTION = 256;
27    /**
28     * list of <code>MP3Object</code>
29     */
30    private List objectList = new ArrayList(AbstractMP3FragmentBody.SIZE_OBJECT_LIST);
31 
32    /**
33     * Creates a new MP3FragmentBody object.
34     */
35    protected AbstractMP3FragmentBody() {
36        super();
37        setupObjectList();
38    }
39 
40    /**
41     * Creates a new AbstractMP3FragmentBody object.
42     */
43    protected AbstractMP3FragmentBody(final AbstractMP3FragmentBody copyObject) {
44        super(copyObject);
45        final Iterator iterator = copyObject.iterator();
46        while (iterator.hasNext()) {
47            final AbstractMP3Object oldObject = (AbstractMP3Object) iterator.next();
48            final AbstractMP3Object newObject = (AbstractMP3Object) TagUtility.copyObject(oldObject);
49            objectList.add(newObject);
50        }
51    }
52 
53    /**
54     * This method calls <code>toString</code> for all it's objects and appends them without any newline characters.
55     *
56     * @return brief description string
57     */
58    public String getBriefDescription() {
59        final StringBuffer stringBuffer = new StringBuffer(AbstractMP3FragmentBody.SIZE_BRIEF_DESCRIPTION);
60        final Iterator iterator = objectList.listIterator();
61        while (iterator.hasNext()) {
62            final AbstractMP3Object object = (AbstractMP3Object) iterator.next();
63            final String objectToString = object.toString();
64            if (objectToString != null && objectToString.length() > 0) {
65                final String identifier = object.getIdentifier();
66                stringBuffer.append(identifier);
67                stringBuffer.append("=\"");
68                stringBuffer.append(objectToString);
69                stringBuffer.append("\"; ");
70            }
71        }
72        return stringBuffer.toString();
73    }
74 
75    /**
76     * This method calls <code>toString</code> for all it's objects and appends them. It contains new line characters
77     * and is more suited for display purposes
78     *
79     * @return formatted description string
80     */
81    public String getDescription() {
82        final StringBuffer stringBuffer = new StringBuffer(AbstractMP3FragmentBody.SIZE_DESCRIPTION);
83        final Iterator iterator = objectList.listIterator();
84        while (iterator.hasNext()) {
85            final AbstractMP3Object object = (AbstractMP3Object) iterator.next();
86            final String identifier = object.getIdentifier();
87            stringBuffer.append(identifier);
88            stringBuffer.append(" = ");
89            final String string = object.toString();
90            stringBuffer.append(string);
91            stringBuffer.append(TagConstant.SEPERATOR_LINE);
92        }
93        final String toString = stringBuffer.toString();
94        return toString.trim();
95    }
96 
97    public Iterator getObjectListIterator() {
98        return objectList.listIterator();
99    }
100 
101    /**
102     * Sets the all objects of identifier type to <code>object</code> argument.
103     *
104     * @param identifier <code>MP3Object</code> identifier
105     * @param object     new object value
106     */
107    public void setObject(final String identifier, final Object object) {
108        final Iterator iterator = objectList.listIterator();
109        while (iterator.hasNext()) {
110            final AbstractMP3Object abstractMP3Object = (AbstractMP3Object) iterator.next();
111            final String currentIdentifier = abstractMP3Object.getIdentifier();
112            if (currentIdentifier.equals(identifier)) {
113                abstractMP3Object.setValue(object);
114            }
115        }
116    }
117 
118    /**
119     * Returns the object of the <code>MP3Object</code> with the specified <code>identifier</code>
120     *
121     * @param identifier <code>MP3Object</code> identifier
122     *
123     * @return the object of the <code>MP3Object</code> with the specified <code>identifier</code>
124     */
125    public Object getObject(final String identifier) {
126        Object object = null;
127        final Iterator iterator = objectList.listIterator();
128        while (iterator.hasNext()) {
129            final AbstractMP3Object abstractMP3Object = (AbstractMP3Object) iterator.next();
130            final String currentIdentifier = abstractMP3Object.getIdentifier();
131            if (currentIdentifier.equals(identifier)) {
132                object = abstractMP3Object.getValue();
133            }
134        }
135        return object;
136    }
137 
138    /**
139     * Returns the estimated size in bytes of this object if it was to be written to file. This is not guaranteed to be
140     * accurate 100% of the time.
141     *
142     * @return estimated size in bytes of this object
143     */
144    public int getSize() {
145        //todo get this working 100% of the time
146        int size = 0;
147        final Iterator iterator = objectList.listIterator();
148        while (iterator.hasNext()) {
149            final AbstractMP3Object object = (AbstractMP3Object) iterator.next();
150            size += object.getSize();
151        }
152        return size;
153    }
154 
155    /**
156     * Returns true if this instance and its entire <code>MP3Object</code> array list is a subset of the argument. This
157     * class is a subset if it is the same class as the argument.
158     *
159     * @param object object to determine subset of
160     *
161     * @return true if this instance and its entire object array list is a subset of the argument.
162     */
163    public boolean isSubsetOf(final Object object) {
164        if (!super.isSubsetOf(object)) {
165            return false;
166        }
167        if (!(object instanceof AbstractMP3FragmentBody)) {
168            return false;
169        }
170        final List superset = ((AbstractMP3FragmentBody) object).objectList;
171        final int objectListSize = objectList.size();
172        for (int i = 0; i < objectListSize; i++) {
173            final AbstractMP3Object abstractMP3Object = (AbstractMP3Object) objectList.get(i);
174            if (abstractMP3Object.getValue() != null && !superset.contains(abstractMP3Object)) {
175                return false;
176            }
177        }
178        return true;
179    }
180 
181    /**
182     * Returns true if this object and its entire <code>MP3Object</code> array list equals the argument. This object is
183     * equal to the argument only if they are the same class.
184     *
185     * @param obj object to determine equality of
186     *
187     * @return true if this object and its entire <code>MP3Object</code> array list equals the argument.
188     */
189    public boolean equals(final Object obj) {
190        if (!(obj instanceof AbstractMP3FragmentBody)) {
191            return false;
192        }
193        final AbstractMP3FragmentBody abstractMP3FragmentBody = (AbstractMP3FragmentBody) obj;
194        if (!objectList.equals(abstractMP3FragmentBody.objectList)) {
195            return false;
196        }
197        return super.equals(obj);
198    }
199 
200    /**
201     * Returns an iterator of the <code>MP3Object</code> object list.
202     *
203     * @return iterator of the <code>MP3Object</code> object list.
204     */
205    public Iterator iterator() {
206        return objectList.iterator();
207    }
208 
209    protected void appendToObjectList(final AbstractMP3Object object) {
210        objectList.add(object);
211    }
212 
213    /**
214     * Read the data from the given file into this object. The file needs to have its file pointer in the correct
215     * location.
216     *
217     * @param file file to read from
218     *
219     * @throws IOException         on any I/O error
220     * @throws InvalidTagException if there is any error in the data format.
221     */
222    public void read(final RandomAccessFile file) throws IOException, InvalidTagException {
223        final int size = readHeader(file);
224        final byte[] buffer = new byte[size];
225        file.read(buffer);
226        final Iterator iterator = objectList.listIterator();
227        int offset = 0;
228        while (iterator.hasNext()) {
229            // sanjay@revasoft.com start bug fix
230            if (offset > size - 1) {
231                throw new InvalidTagException("Invalid size for Frame Body");
232            }
233 
234            // sanjay@revasoft.com end bug fix
235            final AbstractMP3Object object = (AbstractMP3Object) iterator.next();
236            object.readByteArray(buffer, offset);
237            offset += object.getSize();
238        }
239    }
240 
241    /**
242     * Calls <code>toString</code> for all <code>MP3Object</code> objects and creates a string with a new line
243     * character.
244     *
245     * @return description string
246     */
247    public String toString() {
248        final StringBuffer stringBuffer = new StringBuffer(AbstractMP3FragmentBody.SIZE_DESCRIPTION);
249        final String thisIdentifier = getIdentifier();
250        stringBuffer.append(thisIdentifier);
251        stringBuffer.append(TagConstant.SEPERATOR_LINE);
252        final Iterator iterator = objectList.listIterator();
253        while (iterator.hasNext()) {
254            final AbstractMP3Object object = (AbstractMP3Object) iterator.next();
255            final String objectIdentifier = object.getIdentifier();
256            stringBuffer.append(objectIdentifier);
257            stringBuffer.append(" = ");
258            final String string = object.toString();
259            stringBuffer.append(string);
260            stringBuffer.append(TagConstant.SEPERATOR_LINE);
261        }
262        return stringBuffer.toString();
263    }
264 
265    /**
266     * Write the contents of this object to the file at the position it is currently at.
267     *
268     * @param file destination file
269     *
270     * @throws IOException on any I/O error
271     */
272    public void write(final RandomAccessFile file) throws IOException {
273        final int size = getSize();
274        writeHeader(file, size);
275        final Iterator iterator = objectList.listIterator();
276        while (iterator.hasNext()) {
277            final AbstractMP3Object object = (AbstractMP3Object) iterator.next();
278            final byte[] buffer = object.writeByteArray();
279            file.write(buffer);
280        }
281    }
282 
283    /**
284     * Reads the header for the fragment body. The header contains things such as the length.
285     *
286     * @param file file to read the header from.
287     *
288     * @return size of body in bytes as stated in the header.
289     *
290     * @throws IOException         on any I/O error
291     * @throws InvalidTagException if there is any error in the data format.
292     */
293    protected abstract int readHeader(RandomAccessFile file) throws IOException, InvalidTagException;
294 
295    /**
296     * Create the order of <code>MP3Object</code> objects that this body expects. This method needs to be overwritten.
297     */
298    protected abstract void setupObjectList();
299 
300    /**
301     * Write the body header to the file at the current file position.
302     *
303     * @param file file to write to
304     * @param size number of bytes the body contains.
305     *
306     * @throws IOException on any I/O error
307     */
308    protected abstract void writeHeader(RandomAccessFile file, int size) throws IOException;
309 
310    protected static boolean has6ByteHeader() {
311        final Exception exception = new Exception();
312        final StackTraceElement[] stackArray = exception.getStackTrace();
313        final String id3v2_2name = ID3v2_2.class.getName();
314        final String id3v2_3name = ID3v2_3.class.getName();
315        final String id3v2_4name = ID3v2_4.class.getName();
316        boolean has6ByteHeader = false;
317        boolean withinTag = false;
318        for (int i = stackArray.length - 1; i >= 0; i--) {
319            final String className = stackArray[i].getClassName();
320            if (id3v2_2name.equals(className)) {
321                has6ByteHeader = true;
322                withinTag = true;
323            } else if (id3v2_3name.equals(className)) {
324                has6ByteHeader = false;
325                withinTag = true;
326            } else if (id3v2_4name.equals(className)) {
327                has6ByteHeader = false;
328                withinTag = true;
329            }
330        }
331        if (!withinTag) {
332            throw new UnsupportedOperationException("FragmentBody not called within ID3v2 tag.");
333        }
334        return has6ByteHeader;
335    }
336}

[all classes][org.farng.mp3]
EMMA 2.0.5312 (C) Vladimir Roubtsov