String ID3 = new String(b);
// Check signature, if not "ID3" the file does not contain a ID3V2 tag
// at all or the specification of ID3.org wasn't respected.
if (!ID3.equals("ID3")) {
throw new CannotReadException("Not an ID3 tag");
}
// Begins tag parsing --------------------------------------------------
// ---------------------------------------------------------------------
// Version of tag ID3v2.xx.xx
String versionHigh = String.valueOf(raf.read());
String versionID3 = versionHigh + "." + raf.read();
// parsing the ID3V2 tag header flags.
ID3Flags = processID3Flags(raf.readByte());
// ---------------------------------------------------------------------
// Read the tagsize from header, which is a sync safe integer
int tagSize = readSyncsafeInteger(raf);
// ---------------------------------------------------------------------
// Fill a byte buffer, then process according to correct version
b = new byte[tagSize + 2];
// ByteBuffer bb = ByteBuffer.allocateDirect(tagSize+2);
raf.readFully(b);
ByteBuffer bb = ByteBuffer.wrap(b);
// bb.put(b);
// bb.position(0);
// raf.readFully(b);
// MappedByteBuffer buffer = raf.getChannel().map(MapMode.READ_ONLY,
// raf.getFilePointer(), tagSize + 1);
// buffer.load();
// ByteBuffer bb = buffer;
// ByteBuffer bb = ByteBuffer.wrap(b);
if (ID3Flags[0] == true) {
// We have unsynchronization, first re-synchronize
bb = synchronizer.synchronize(bb);
}
// Up to now we use the same reader for all versions and pass a flag
if (versionHigh.equals("2")) {
tag = tagReader.read(bb, ID3Flags, Id3v2Tag.ID3V22);
} else if (versionHigh.equals("3")) {
tag = tagReader.read(bb, ID3Flags, Id3v2Tag.ID3V23);
} else if (versionHigh.equals("4")) {
tag = tagReader.read(bb, ID3Flags, Id3v2Tag.ID3V24);
} else {
/*
* The implementation of entagges ID3V2 tag parsing does not ignore
* unknown ID3V2 tag definitions, so we must throw an error.
*/
throw new CannotReadException("ID3v2 tag version " + versionID3
+ " not supported !");
}
return tag;
}