001    package helliker.id3;
002    
003    /*
004     *  Copyright (C) 2001,2002 Jonathan Hilliker
005     *
006     *  This library is free software; you can redistribute it and/or
007     *  modify it under the terms of the GNU Lesser General Public
008     *  License as published by the Free Software Foundation; either
009     *  version 2.1 of the License, or (at your option) any later version.
010     *
011     *  This library is distributed in the hope that it will be useful,
012     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014     *  Lesser General Public License for more details.
015     *
016     *  You should have received a copy of the GNU Lesser General Public
017     *  License along with this library; if not, write to the Free Software
018     *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019     */
020    /**
021     * This class holds the information found in an id3v2 frame. At this point at
022     * least, the information in the tag header is read-only and can only be set set
023     * in the constructor. This is for simplicity's sake. This object doesn't
024     * automatically unsynchronise, encrypt, or compress the data.
025     */
026    
027    public class ID3v2Frame {
028    
029      /**
030       * Frame Header Size of 10 Bytes
031       *
032       *
033       */
034      private static final int FRAME_HEAD_SIZE = 10;
035    
036      /**
037       * Flags Size of two 2 Bytes
038       *
039       *
040       */
041      private static final int FRAME_FLAGS_SIZE = 2;
042    
043      /**
044       * ???? WHAT IS THIS
045       *
046       *
047       */
048      private static final int MAX_EXTRA_DATA = 5;
049    
050      /**
051       * Holds the possible ID3 tag encodings.
052       *
053       */
054      static final String[] ENC_TYPES = {"ISO-8859-1", "UTF16",
055        "UTF-16BE", "UTF-8"};
056      /**
057       * The Frame ID will be kept may init diffrent
058       */
059      private String id = null;
060      /**
061       * a - Tag alter preservation.<br/>
062       * <br/>
063       * This flag tells the tag parser what to do with this frame if it is unknown
064       * and the tag is altered in any way. This applies to all kinds of
065       * alterations, including adding more padding and reordering the frames.<br/>
066       *
067       * <tableborder="0">
068       *
069       *   <tr>
070       *
071       *     <td>
072       *       0 -(false)
073       *     </td>
074       *
075       *     <td>
076       *       Frame should be preserved.
077       *     </td>
078       *
079       *   </tr>
080       *
081       *   <tr>
082       *
083       *     <td>
084       *       1 - (true)
085       *     </td>
086       *
087       *     <td>
088       *       Frame should be discarded.
089       *     </td>
090       *
091       *   </tr>
092       *
093       * </table>
094       *
095       */
096      private boolean tagAlterDiscard;
097      /**
098       * b - File alter preservation.</br> <br/>
099       * This flag tells the tag parser what to do with this frame if it is unknown
100       * and the file, excluding the tag, is altered. This does not apply when the
101       * audio is completely replaced with other audio data.</br>
102       * <tableborder="0">
103       *
104       *   <tr>
105       *
106       *     <td>
107       *       0 (false)
108       *     </td>
109       *
110       *     <td>
111       *       Frame should be preserved.
112       *     </td>
113       *
114       *   </tr>
115       *
116       *   <tr>
117       *
118       *     <td>
119       *       1 (true)
120       *     </td>
121       *
122       *     <td>
123       *       Frame should be discarded.
124       *     </td>
125       *
126       *   </tr>
127       *
128       * </table>
129       *
130       */
131      private boolean fileAlterDiscard;
132      /**
133       * c - Read only.<br/>
134       * <br/>
135       * This flag, if set, tells the software that the contents of this frame are
136       * intended to be read only. Changing the contents might break something, e.g.
137       * a signature. If the contents are changed, without knowledge of why the
138       * frame was flagged read only and without taking the proper means to
139       * compensate, e.g. recalculating the signature, the bit MUST be cleared.
140       */
141      private boolean readOnly;
142      /**
143       * h - Grouping identity.</br> <br/>
144       * This flag indicates whether or not this frame belongs in a group with other
145       * frames. If set, a group identifier byte is added to the frame. Every frame
146       * with the same group identifier belongs to the same group.<br/>
147       *
148       * <tableborder="0">
149       *
150       *   <tr>
151       *
152       *     <td>
153       *       0 (false)
154       *     </td>
155       *
156       *     <td>
157       *       Frame does not contain group information
158       *     </td>
159       *
160       *   </tr>
161       *
162       *   <tr>
163       *
164       *     <td>
165       *       1 (true)
166       *     </td>
167       *
168       *     <td>
169       *       Frame contains group information
170       *     </td>
171       *
172       *   </tr>
173       *
174       * </table>
175       *
176       */
177      private boolean grouped;
178      /**
179       * k - Compression.<br/>
180       * <br />
181       * This flag indicates whether or not the frame is compressed. A 'Data Length
182       * Indicator' byte MUST be included in the frame.<br/>
183       *
184       * <table>
185       *
186       *   <tr>
187       *
188       *     <td>
189       *       0 (false)
190       *     </td>
191       *
192       *     <td>
193       *       Frame is not compressed.
194       *     </td>
195       *
196       *   </tr>
197       *
198       *   <tr>
199       *
200       *     <tdvalign="top">
201       *       1 (true)
202       *     </td>
203       *
204       *     <td>
205       *       Frame is compressed using zlib <a
206       *       href="ftp://ftp.isi.edu/in-notes/rfc1950.txt" title="P. Deutsch,
207       *       Aladdin Enterprises & J-L. Gailly, 'ZLIB Compressed Data Format
208       *       Specification version 3.3', RFC 1950, May 1996.">[ZLIB]</a> deflate
209       *       method. If set, this requires the 'Data Length Indicator' bit to be
210       *       set as well.
211       *     </td>
212       *
213       *   </tr>
214       *
215       * </table>
216       *
217       */
218      private boolean compressed;
219      /**
220       * m - Encryption.<br/>
221       * <br/>
222       * This flag indicates whether or not the frame is encrypted. If set, one byte
223       * indicating with which method it was encrypted will be added to the frame.
224       * See description of the ENCR frame for more information about encryption
225       * method registration. Encryption should be done after compression. Whether
226       * or not setting this flag requires the presence of a 'Data Length Indicator'
227       * depends on the specific algorithm used.<br/>
228       *
229       * <table>
230       *
231       *   <tr>
232       *
233       *     <td>
234       *       0 (false)
235       *     </td>
236       *
237       *     <td>
238       *       Frame is not encrypted.
239       *     </td>
240       *
241       *   </tr>
242       *
243       *   <tr>
244       *
245       *     <td>
246       *       1 (true)
247       *     </td>
248       *
249       *     <td>
250       *       Frame is encrypted.
251       *     </td>
252       *
253       *   </tr>
254       *
255       * </table>
256       *
257       */
258      private boolean encrypted;
259      /**
260       * n - Unsynchronisation<br />
261       * <br />
262       * This flag indicates whether or not unsynchronisation was applied to this
263       * frame. See section 6 for details on unsynchronisation. If this flag is set
264       * all data from the end of this header to the end of this frame has been
265       * unsynchronised. Although desirable, the presence of a 'Data Length
266       * Indicator' is not made mandatory by unsynchronisation.
267       * <table>
268       *
269       *   <tr>
270       *
271       *     <td>
272       *       0 (false)
273       *     </td>
274       *
275       *     <td>
276       *       Frame has not been unsynchronised.
277       *     </td>
278       *
279       *   </tr>
280       *
281       *   <tr>
282       *
283       *     <td>
284       *       1 (true)
285       *     </td>
286       *
287       *     <td>
288       *       Frame has been unsyrchronised.
289       *     </td>
290       *
291       *   </tr>
292       *
293       * </table>
294       *
295       */
296      private boolean unsynchronised;
297      /**
298       * p - Data length indicator<br />
299       * <br />
300       * This flag indicates that a data length indicator has been added to the
301       * frame. The data length indicator is the value one would write as the 'Frame
302       * length' if all of the frame format flags were zeroed, represented as a 32
303       * bit synchsafe integer.<br/>
304       *
305       * <table>
306       *
307       *   <tr>
308       *
309       *     <td>
310       *       0 (false)
311       *     </td>
312       *
313       *     <td>
314       *       There is no Data Length Indicator.
315       *     </td>
316       *
317       *     <tr>
318       *
319       *       <td>
320       *         1 (true)
321       *       </td>
322       *
323       *       <td>
324       *         A data length Indicator has been added to the frame.
325       *       </td>
326       *
327       *     </tr>
328       *
329       *   </table>
330       *
331       */
332      private boolean lengthIndicator;
333      /**
334       * The Group Bit ???
335       */
336      private byte group;
337      /**
338       * The Encryption Tpe Byte
339       */
340      private byte encrType;
341      /**
342       * The Data Length
343       */
344      private int dataLength;
345      /**
346       * The Frame Data in Byte[]
347       */
348      private byte[] frameData;
349    
350    
351      /**
352       * Create an ID3v2Frame with a specified id, a byte array containing the frame
353       * header flags, and a byte array containing the data for this frame.
354       *
355       *@param id                        the id of this frame
356       *@param flags                     the flags found in the header of the frame
357       *      (2 bytes)
358       *@param data                      the data found in this frame
359       *@exception ID3v2FormatException  if an error occurs
360       */
361      public ID3v2Frame(String id, byte[] flags, byte[] data)
362         throws ID3v2FormatException {
363    
364        this(id, data);
365    
366        parseFlags(flags);
367      }
368    
369    
370      /**
371       * Create and ID3v2 frame with the specified id and data. All the flag bits
372       * are set to false.
373       *
374       *@param id    the id of this frame
375       *@param data  the data for this frame
376       */
377      public ID3v2Frame(String id, byte[] data) {
378        this.id = id;
379    
380        tagAlterDiscard = false;
381        fileAlterDiscard = checkDefaultFileAlterDiscard();
382        readOnly = false;
383        grouped = false;
384        compressed = false;
385        encrypted = false;
386        unsynchronised = false;
387        lengthIndicator = false;
388        group = '\0';
389        encrType = '\0';
390        dataLength = -1;
391    
392        parseData(data);
393      }
394    
395    
396      /**
397       * Create an ID3v2Frame with the specified id, data, and flags set. It is
398       * expected that the corresponing data for the flags that require extra data
399       * is found in the data array in the standard place.
400       *
401       *@param id                the id for this frame
402       *@param data              the data for this frame
403       *@param tagAlterDiscard   the tag alter preservation flag
404       *@param fileAlterDiscard  the file alter preservation flag
405       *@param readOnly          the read only flag
406       *@param grouped           the grouping identity flag
407       *@param compressed        the compression flag
408       *@param encrypted         the encryption flag
409       *@param unsynchronised    the unsynchronisation flag
410       *@param lengthIndicator   the data length indicator flag
411       */
412      public ID3v2Frame(String id, byte[] data, boolean tagAlterDiscard,
413                        boolean fileAlterDiscard, boolean readOnly,
414                        boolean grouped, boolean compressed,
415                        boolean encrypted, boolean unsynchronised,
416                        boolean lengthIndicator) {
417    
418        this.id = id;
419        this.tagAlterDiscard = tagAlterDiscard;
420        this.fileAlterDiscard = fileAlterDiscard;
421        this.readOnly = readOnly;
422        this.grouped = grouped;
423        this.compressed = compressed;
424        this.encrypted = encrypted;
425        this.unsynchronised = unsynchronised;
426        this.lengthIndicator = lengthIndicator;
427    
428        group = '\0';
429        encrType = '\0';
430        dataLength = -1;
431    
432        parseData(data);
433      }
434    
435    
436      /**
437       * Set the data for this frame. This does nothing if this frame is read only.
438       *
439       *@param newData  a byte array containing the new data
440       */
441      public void setFrameData(byte[] newData) {
442        if (!readOnly) {
443          frameData = newData;
444        }
445      }
446    
447    
448      /**
449       * Returns the data for this frame
450       *
451       *@return   the data for this frame
452       */
453      public byte[] getFrameData() {
454        return frameData;
455      }
456    
457    
458      /**
459       * Return the length of this frame in bytes, including the header.
460       *
461       *@return   the length of this frame
462       */
463      public int getFrameLength() {
464        int length = frameData.length + FRAME_HEAD_SIZE;
465    
466        if (grouped) {
467          length += 1;
468        }
469        if (encrypted) {
470          length += 1;
471        }
472        if (lengthIndicator) {
473          length += 4;
474        }
475    
476        return length;
477      }
478    
479    
480      /**
481       * Returns a byte array representation of this frame that can be written to a
482       * file. Includes the header and data.
483       *
484       *@return   a binary representation of this frame to be written to a file
485       */
486      public byte[] getFrameBytes() {
487        int length = frameData.length;
488        int bytesWritten = 0;
489        byte[] flags = getFlagBytes();
490        byte[] extra = getExtraDataBytes();
491        byte[] b;
492    
493        if (grouped) {
494          length += 1;
495        }
496        if (encrypted) {
497          length += 1;
498        }
499        if (lengthIndicator) {
500          length += 4;
501        }
502    
503        b = new byte[length + FRAME_HEAD_SIZE];
504    
505        System.arraycopy(id.getBytes(), 0, b, 0, id.length());
506        bytesWritten += id.length();
507        System.arraycopy(BinaryParser.convertToBytes(length), 0, b,
508          bytesWritten, 4);
509        bytesWritten += 4;
510        System.arraycopy(flags, 0, b, bytesWritten, flags.length);
511        bytesWritten += flags.length;
512        System.arraycopy(extra, 0, b, bytesWritten, extra.length);
513        bytesWritten += extra.length;
514        System.arraycopy(frameData, 0, b, bytesWritten, frameData.length);
515        bytesWritten += frameData.length;
516    
517        return b;
518      }
519    
520    
521      /**
522       * Returns true if the tag alter preservation bit has been set. If set then
523       * the frame should be discarded if it is altered and the id is unknown.
524       *
525       *@return   true if the tag alter preservation bit has been set
526       */
527      public boolean getTagAlterDiscard() {
528        return tagAlterDiscard;
529      }
530    
531    
532      /**
533       * Returns true if the file alter preservation bit has been set. If set then
534       * the frame should be discarded if the file is altered and the id is unknown.
535       *
536       *@return   true if the file alter preservation bit has been set
537       */
538      public boolean getFileAlterDiscard() {
539        return fileAlterDiscard;
540      }
541    
542    
543      /**
544       * Returns true if this frame is read only
545       *
546       *@return   true if this frame is read only
547       */
548      public boolean getReadOnly() {
549        return readOnly;
550      }
551    
552    
553      /**
554       * Returns true if this frame is a part of a group
555       *
556       *@return   true if this frame is a part of a group
557       */
558      public boolean getGrouped() {
559        return grouped;
560      }
561    
562    
563      /**
564       * Returns true if this frame is compressed
565       *
566       *@return   true if this frame is compressed
567       */
568      public boolean getCompressed() {
569        return compressed;
570      }
571    
572    
573      /**
574       * Returns true if this frame is encrypted
575       *
576       *@return   true if this frame is encrypted
577       */
578      public boolean getEncrypted() {
579        return encrypted;
580      }
581    
582    
583      /**
584       * Returns true if this frame is unsynchronised
585       *
586       *@return   true if this frame is unsynchronised
587       */
588      public boolean getUnsynchronised() {
589        return unsynchronised;
590      }
591    
592    
593      /**
594       * Returns true if this frame has a length indicator added
595       *
596       *@return   true if this frame has a length indicator added
597       */
598      public boolean getLengthIndicator() {
599        return lengthIndicator;
600      }
601    
602    
603      /**
604       * Returns the group identifier if added. Otherwise the null byte is returned.
605       *
606       *@return   the groupd identifier if added, null byte otherwise
607       */
608      public byte getGroup() {
609        return group;
610      }
611    
612    
613      /**
614       * If encrypted, this returns the encryption method byte. If it is not
615       * encrypted, the null byte is returned.
616       *
617       *@return   the encryption method if set and the null byte if not
618       */
619      public byte getEncryptionType() {
620        return encrType;
621      }
622    
623    
624      /**
625       * If a length indicator has been added, the length of the data is returned.
626       * Otherwise -1 is returned.
627       *
628       *@return   the length of the data if a length indicator is present or -1
629       */
630      public int getDataLength() {
631        return dataLength;
632      }
633    
634    
635      /**
636       * If possible, this method attempts to convert textual part of the data into
637       * a string. If this frame does not contain textual information, an empty
638       * string is returned.
639       *
640       *@return                          the textual portion of the data in this
641       *      frame
642       *@exception ID3v2FormatException  if an error occurs
643       */
644      public String getDataString() throws ID3v2FormatException {
645        String str = new String();
646    
647        if (frameData.length > 1) {
648          try {
649            if ((id.charAt(0) == 'T') || id.equals(ID3v2Frames.OWNERSHIP_FRAME)) {
650    
651              str = getDecodedString(frameData, 0, 1);
652            } else if (id.equals(ID3v2Frames.USER_DEFINED_URL)) {
653              int offset = getValOffset(frameData);
654              if(offset < frameData.length){
655                  str = new String(frameData, offset, frameData.length - offset, ENC_TYPES[0]);
656              }
657            } else if (id.charAt(0) == 'W') {
658              str = new String(frameData, 0, frameData.length, ENC_TYPES[0]);
659            } else if (id.equals(ID3v2Frames.UNSYNCHRONISED_LYRIC_TRANSCRIPTION)
660               || id.equals(ID3v2Frames.COMMENTS)
661               || id.equals(ID3v2Frames.TERMS_OF_USE)) {
662              int offset = getValOffset(frameData);
663              if(offset < frameData.length){
664                  str = getDecodedString(frameData, 0, offset);
665              }
666            }
667          } catch (java.io.UnsupportedEncodingException e) {
668            throw new ID3v2FormatException("Frame: " + id + " has "
669               + " not specified a valid encoding type.");
670          }
671        }
672    
673        return str;
674      }
675    
676      /** Returns the index of the first non-null byte in framedata
677       * that occurs after the null-terminated string starting at
678       * offset. Whether the null-terminated string must be terminated
679       * by one or two null bytes depends on the encoding
680       * byte found at framedata[eindex].
681       */
682      static int getValOffset(byte[] bdata, int eindex, int offset){
683          int ienc = bdata[eindex];
684          //skip anything that precedes (double) null terminator
685          boolean singleNullByte = (ienc != 1 && ienc != 2);
686          int offs = offset;
687          boolean lastByteWasNull = false;
688          for(; offs<bdata.length; offs++){
689              boolean byteIsNull = (bdata[offs] == 0x00);
690              if(byteIsNull && (singleNullByte || lastByteWasNull)){
691                  offs++;
692                  break;
693              }
694              lastByteWasNull = byteIsNull;
695          }
696          return offs;
697      }
698    
699      private static int getValOffset(byte[] bdata){
700          return getValOffset(bdata, 0, 1);
701      }
702    
703    
704      /**
705       * Returns true if there is no data in the frame.
706       *
707       *@return   true if there is no data in the frame
708       */
709      public boolean isEmpty() {
710        return frameData.length <= 1;
711      }
712    
713    
714      /**
715       * Return a string representation of this object that contains all the
716       * information contained within it.
717       *
718       *@return   a string representation of this object
719       */
720      public String toString() {
721        String str = null;
722    
723        try {
724          str = id + "\nTagAlterDiscard:\t\t" + tagAlterDiscard
725             + "\nFileAlterDiscard:\t\t" + fileAlterDiscard
726             + "\nReadOnly:\t\t\t" + readOnly
727             + "\nGrouped:\t\t\t" + grouped
728             + "\nCompressed:\t\t\t" + compressed
729             + "\nEncrypted:\t\t\t" + encrypted
730             + "\nUnsynchronised:\t\t\t" + unsynchronised
731             + "\nLengthIndicator:\t\t" + lengthIndicator
732             + "\nData:\t\t\t\t" + getDataString().toString();
733        } catch (Exception e) {
734          // Do nothing, this is just toString, errors irrelevant
735        }
736    
737        return str;
738      }
739    
740    
741      /**
742       * A helper function for the getFrameBytes method that processes the info in
743       * the frame and returns the 2 byte array of flags to be added to the header.
744       *
745       *@return   a value of type 'byte[]'
746       */
747      private byte[] getFlagBytes() {
748        byte flags[] = {0x00, 0x00};
749    
750        if (tagAlterDiscard) {
751          flags[0] = BinaryParser.setBit(flags[0], 6);
752        }
753        if (fileAlterDiscard) {
754          flags[0] = BinaryParser.setBit(flags[0], 5);
755        }
756        if (readOnly) {
757          flags[0] = BinaryParser.setBit(flags[0], 4);
758        }
759        if (grouped) {
760          flags[1] = BinaryParser.setBit(flags[1], 6);
761        }
762        if (compressed) {
763          flags[1] = BinaryParser.setBit(flags[1], 3);
764        }
765        if (encrypted) {
766          flags[1] = BinaryParser.setBit(flags[1], 2);
767        }
768        if (unsynchronised) {
769          flags[1] = BinaryParser.setBit(flags[1], 1);
770        }
771        if (lengthIndicator) {
772          flags[1] = BinaryParser.setBit(flags[1], 0);
773        }
774    
775        return flags;
776      }
777    
778    
779      /**
780       * A helper function for the getFrameBytes function that returns an array of
781       * all the data contained in any extra fields that may be present in this
782       * frame. This includes the group, the encryption type, and the length
783       * indicator. The length of the array returned is variable length.
784       *
785       *@return   an array of bytes containing the extra data fields in the frame
786       */
787      private byte[] getExtraDataBytes() {
788        byte[] buf = new byte[MAX_EXTRA_DATA];
789        byte[] ret;
790        int bytesCopied = 0;
791    
792        if (grouped) {
793          buf[bytesCopied] = group;
794          bytesCopied += 1;
795        }
796        if (encrypted) {
797          buf[bytesCopied] = encrType;
798          bytesCopied += 1;
799        }
800        if (lengthIndicator) {
801          byte[] num = BinaryParser.convertToBytes(dataLength);
802          System.arraycopy(num, 0, buf, bytesCopied, num.length);
803          bytesCopied += num.length;
804        }
805    
806        ret = new byte[bytesCopied];
807        System.arraycopy(buf, 0, ret, 0, bytesCopied);
808    
809        return ret;
810      }
811    
812    
813      /**
814       * Converts the byte array into a string based on the type of encoding. One
815       * byte in the array should contain the type of encoding. Where it is located
816       * is specifed by the eIndex parameter. If an invalid type of encoding is
817       * found, the empty string is returned.
818       *
819       *@param b                                         the array of bytes to
820       *      convert/decode
821       *@param eIndex                                    the index in the array
822       *      where the encoding type resides
823       *@param offset                                    where in the array to start
824       *      the string
825       *@return                                          the decoded string or an
826       *      empty string if the encoding is wrong
827       *@exception java.io.UnsupportedEncodingException  if an error occurs
828       */
829      private String getDecodedString(byte[] b, int eIndex, int offset)
830         throws java.io.UnsupportedEncodingException {
831    
832        String str = "";
833    
834        int encType = (int) b[eIndex];
835        if ((encType >= 0) && (encType < ENC_TYPES.length)) {
836          str = new String(b, offset, b.length - offset,
837            ENC_TYPES[encType]);
838        }
839        return str;
840      }
841    
842    
843      /**
844       * Returns true if this frame should have the file alter preservation bit set
845       * by default.
846       *
847       *@return   true if the file alter preservation should be set by default
848       */
849      private boolean checkDefaultFileAlterDiscard() {
850        return id.equals(ID3v2Frames.AUDIO_SEEK_POINT_INDEX)
851           || id.equals(ID3v2Frames.AUDIO_ENCRYPTION)
852           || id.equals(ID3v2Frames.EVENT_TIMING_CODES)
853           || id.equals(ID3v2Frames.EQUALISATION)
854           || id.equals(ID3v2Frames.MPEG_LOCATION_LOOKUP_TABLE)
855           || id.equals(ID3v2Frames.POSITION_SYNCHRONISATION_FRAME)
856           || id.equals(ID3v2Frames.SEEK_FRAME)
857           || id.equals(ID3v2Frames.SYNCHRONISED_LYRIC)
858           || id.equals(ID3v2Frames.SYNCHRONISED_TEMPO_CODES)
859           || id.equals(ID3v2Frames.RELATIVE_VOLUME_ADJUSTMENT)
860           || id.equals(ID3v2Frames.ENCODED_BY)
861           || id.equals(ID3v2Frames.LENGTH);
862      }
863    
864    
865      /**
866       * Read the information from the flags array.
867       *
868       *@param flags                     the flags found in the frame header
869       *@exception ID3v2FormatException  if an error occurs
870       */
871      private void parseFlags(byte[] flags) throws ID3v2FormatException {
872        if (flags.length != FRAME_FLAGS_SIZE) {
873          throw new ID3v2FormatException("Error parsing flags of frame: "
874             + id + ".  Expected 2 bytes.");
875        } else {
876          tagAlterDiscard = BinaryParser.bitSet(flags[0], 6);
877          fileAlterDiscard = BinaryParser.bitSet(flags[0], 5);
878          readOnly = BinaryParser.bitSet(flags[0], 4);
879          grouped = BinaryParser.bitSet(flags[1], 6);
880          compressed = BinaryParser.bitSet(flags[1], 3);
881          encrypted = BinaryParser.bitSet(flags[1], 2);
882          unsynchronised = BinaryParser.bitSet(flags[1], 1);
883          lengthIndicator = BinaryParser.bitSet(flags[1], 0);
884    
885          if (compressed && !lengthIndicator) {
886            throw new ID3v2FormatException("Error parsing flags of frame: "
887               + id + ".  Compressed bit set  " + "without data length bit set.");
888          }
889        }
890      }
891    
892    
893      /**
894       * Pulls out extra information inserted in the frame data depending on what
895       * flags are set.
896       *
897       *@param data  the frame data
898       */
899      private void parseData(byte[] data) {
900        int bytesRead = 0;
901    
902        if (grouped) {
903          group = data[bytesRead];
904          bytesRead += 1;
905        }
906        if (encrypted) {
907          encrType = data[bytesRead];
908          bytesRead += 1;
909        }
910        if (lengthIndicator) {
911          byte[] num = new byte[4];
912          System.arraycopy(data, bytesRead, num, 0, num.length);
913          dataLength = BinaryParser.convertToInt(num);
914          bytesRead += num.length;
915        }
916    
917        frameData = new byte[data.length - bytesRead];
918        System.arraycopy(data, bytesRead, frameData, 0, frameData.length);
919      }
920    
921    }
922