throws UnsupportedEncodingException {
// get the tagsize from the buffers size.
int tagSize = data.limit();
byte[] b;
// Create a result object
Id3v2Tag tag = new Id3v2Tag();
// ---------------------------------------------------------------------
// If the flags indicate an extended header to be present, read its
// size and skip it. (It does not contain any useful information, maybe
// CRC)
if ((version == Id3v2Tag.ID3V23 || version == Id3v2Tag.ID3V24)
&& ID3Flags[1]) {
processExtendedHeader(data, version);
}
// ---------------------------------------------------------------------
/*
* Now start the extraction of the text frames.
*/
// The frame names differ in lengths between version 2 to 3
int specSize = (version == Id3v2Tag.ID3V22) ? 3 : 4;
// As long as we have unread bytes...
for (int a = 0; a < tagSize; a++) {
// Create buffer taking the name of the frame.
b = new byte[specSize];
// Do we still have enough bytes for reading the name?
if (data.remaining() <= specSize)
break;
// Read the Name
data.get(b);
// Convert the bytes (of the name) into a String.
String field = new String(b);
// If byte[0] is zero, we have invalid data
if (b[0] == 0)
break;
// Now we read the length of the current frame
int frameSize = readSize(data, version);
// If the framesize is greater than the bytes we've left to read,
// or the frame length is zero, abort. Invalid data
if ((frameSize > data.remaining()) || frameSize <= 0) {
// ignore empty frames
System.err.println(field
+ " Frame size error, skiping the rest of the tag:"
+ frameSize);
break;
}
b = new byte[frameSize
+ ((version == Id3v2Tag.ID3V23 || version == Id3v2Tag.ID3V24) ? 2
: 0)];
// Read the complete frame into the byte array.
data.get(b);
// Check the frame name once more
if (!"".equals(field)) {
Id3Frame f = null;
/*
* Now catch possible errors occuring in the data
* interpretation. Even if a frame is not valid regarding the
* spec, the rest of the tag could be read.
*/
try {
// Create the Frame upon the byte array data.
f = createId3Frame(field, b, version);
} catch (UnsupportedEncodingException uee) {
throw uee;
} catch (Exception e) {
e.printStackTrace();
}
// If the frame was successfully parsed, add it to the tag.
if (f != null)
tag.add(f);
}
}
return tag;
}