use of org.jaudiotagger.tag.InvalidFrameException in project MusicDNA by harjot-oberai.
the class FlacTagReader method read.
public FlacTag read(RandomAccessFile raf) throws CannotReadException, IOException {
FlacStreamReader flacStream = new FlacStreamReader(raf);
flacStream.findStream();
//Hold the metadata
VorbisCommentTag tag = null;
List<MetadataBlockDataPicture> images = new ArrayList<MetadataBlockDataPicture>();
//Seems like we have a valid stream
boolean isLastBlock = false;
while (!isLastBlock) {
if (logger.isLoggable(Level.CONFIG)) {
logger.config("Looking for MetaBlockHeader at:" + raf.getFilePointer());
}
//Read the header
MetadataBlockHeader mbh = MetadataBlockHeader.readHeader(raf);
if (mbh == null) {
break;
}
if (logger.isLoggable(Level.CONFIG)) {
logger.config("Reading MetadataBlockHeader:" + mbh.toString() + " ending at " + raf.getFilePointer());
}
//JAUDIOTAGGER-466:CBlocktype can be null
if (mbh.getBlockType() != null) {
switch(mbh.getBlockType()) {
//We got a vorbiscomment comment block, parse it
case VORBIS_COMMENT:
byte[] commentHeaderRawPacket = new byte[mbh.getDataLength()];
raf.read(commentHeaderRawPacket);
tag = vorbisCommentReader.read(commentHeaderRawPacket, false);
break;
case PICTURE:
try {
MetadataBlockDataPicture mbdp = new MetadataBlockDataPicture(mbh, raf);
images.add(mbdp);
} catch (IOException ioe) {
logger.warning("Unable to read picture metablock, ignoring:" + ioe.getMessage());
} catch (InvalidFrameException ive) {
logger.warning("Unable to read picture metablock, ignoring" + ive.getMessage());
}
break;
//This is not a metadata block we are interested in so we skip to next block
default:
if (logger.isLoggable(Level.CONFIG)) {
logger.config("Ignoring MetadataBlock:" + mbh.getBlockType());
}
raf.seek(raf.getFilePointer() + mbh.getDataLength());
break;
}
}
isLastBlock = mbh.isLastBlock();
mbh = null;
}
//just initialize Flac with an empty VorbisTag
if (tag == null) {
tag = VorbisCommentTag.createNewTag();
}
FlacTag flacTag = new FlacTag(tag, images);
return flacTag;
}
use of org.jaudiotagger.tag.InvalidFrameException in project MusicDNA by harjot-oberai.
the class AbstractID3v2FrameBody method read.
/**
* This reads a frame body from a ByteBuffer into the appropriate FrameBody class and update the position of the
* buffer to be just after the end of this frameBody
*
* The ByteBuffer represents the tag and its position should be at the start of this frameBody. The size as
* indicated in the header is passed to the frame constructor when reading from file.
*
* @param byteBuffer file to read
* @throws InvalidFrameException if unable to construct a frameBody from the ByteBuffer
*/
//TODO why don'timer we just slice byteBuffer, set limit to size and convert readByteArray to take a ByteBuffer
//then we wouldn'timer have to temporary allocate space for the buffer, using lots of needless memory
//and providing extra work for the garbage collector.
public void read(ByteBuffer byteBuffer) throws InvalidTagException {
int size = getSize();
logger.config("Reading body for" + this.getIdentifier() + ":" + size);
//Allocate a buffer to the size of the Frame Body and read from file
byte[] buffer = new byte[size];
byteBuffer.get(buffer);
//Offset into buffer, incremented by length of previous dataType
//this offset is only used internally to decide where to look for the next
//dataType within a frameBody, it does not decide where to look for the next frame body
int offset = 0;
//Go through the ObjectList of the Frame reading the data into the
for (AbstractDataType object : objectList) //correct dataType.
{
logger.finest("offset:" + offset);
//size because the next datatype may be of length 0.)
if (offset > (size)) {
logger.warning("Invalid Size for FrameBody");
throw new InvalidFrameException("Invalid size for Frame Body");
}
//if it fails frame is invalid
try {
object.readByteArray(buffer, offset);
} catch (InvalidDataTypeException e) {
logger.warning("Problem reading datatype within Frame Body:" + e.getMessage());
throw e;
}
//Increment Offset to start of next datatype.
offset += object.getSize();
}
}
use of org.jaudiotagger.tag.InvalidFrameException in project MusicDNA by harjot-oberai.
the class ID3Compression method uncompress.
/**
* Decompress realFrameSize bytes to decompressedFrameSize bytes and return as ByteBuffer
*
* @param byteBuffer
* @param decompressedFrameSize
* @param realFrameSize
* @return
* @throws org.jaudiotagger.tag.InvalidFrameException
*
*/
protected static ByteBuffer uncompress(String identifier, String filename, ByteBuffer byteBuffer, int decompressedFrameSize, int realFrameSize) throws InvalidFrameException {
logger.config(filename + ":About to decompress " + realFrameSize + " bytes, expect result to be:" + decompressedFrameSize + " bytes");
// Decompress the bytes into this buffer, size initialized from header field
byte[] result = new byte[decompressedFrameSize];
byte[] input = new byte[realFrameSize];
//Store position ( just after frame header and any extra bits)
//Read frame data into array, and then put buffer back to where it was
int position = byteBuffer.position();
byteBuffer.get(input, 0, realFrameSize);
byteBuffer.position(position);
Inflater decompresser = new Inflater();
decompresser.setInput(input);
try {
int inflatedTo = decompresser.inflate(result);
logger.config(filename + ":Decompressed to " + inflatedTo + " bytes");
} catch (DataFormatException dfe) {
logger.log(Level.CONFIG, "Unable to decompress this frame:" + identifier, dfe);
//Update position of main buffer, so no attempt is made to reread these bytes
byteBuffer.position(byteBuffer.position() + realFrameSize);
throw new InvalidFrameException(ErrorMessage.ID3_UNABLE_TO_DECOMPRESS_FRAME.getMsg(identifier, filename, dfe.getMessage()));
}
decompresser.end();
return ByteBuffer.wrap(result);
}
use of org.jaudiotagger.tag.InvalidFrameException in project MusicDNA by harjot-oberai.
the class ID3v23Frame method read.
/**
* Read the frame from a byteBuffer
*
* @param byteBuffer buffer to read from
*/
public void read(ByteBuffer byteBuffer) throws InvalidFrameException, InvalidDataTypeException {
String identifier = readIdentifier(byteBuffer);
if (!isValidID3v2FrameIdentifier(identifier)) {
logger.config(getLoggingFilename() + ":Invalid identifier:" + identifier);
byteBuffer.position(byteBuffer.position() - (getFrameIdSize() - 1));
throw new InvalidFrameIdentifierException(getLoggingFilename() + ":" + identifier + ":is not a valid ID3v2.30 frame");
}
//Read the size field (as Big Endian Int - byte buffers always initialised to Big Endian order)
frameSize = byteBuffer.getInt();
if (frameSize < 0) {
logger.warning(getLoggingFilename() + ":Invalid Frame Size:" + frameSize + ":" + identifier);
throw new InvalidFrameException(identifier + " is invalid frame:" + frameSize);
} else if (frameSize == 0) {
logger.warning(getLoggingFilename() + ":Empty Frame Size:" + identifier);
//We don'timer process this frame or add to frameMap because contains no useful information
//Skip the two flag bytes so in correct position for subsequent frames
byteBuffer.get();
byteBuffer.get();
throw new EmptyFrameException(identifier + " is empty frame");
} else if (frameSize > byteBuffer.remaining()) {
logger.warning(getLoggingFilename() + ":Invalid Frame size of " + frameSize + " larger than size of" + byteBuffer.remaining() + " before mp3 audio:" + identifier);
throw new InvalidFrameException(identifier + " is invalid frame:" + frameSize + " larger than size of" + byteBuffer.remaining() + " before mp3 audio:" + identifier);
}
//Read the flag bytes
statusFlags = new StatusFlags(byteBuffer.get());
encodingFlags = new EncodingFlags(byteBuffer.get());
String id;
//If this identifier is a valid v24 identifier or easily converted to v24
id = ID3Tags.convertFrameID23To24(identifier);
// Cant easily be converted to v24 but is it a valid v23 identifier
if (id == null) {
// frame body for it.
if (ID3Tags.isID3v23FrameIdentifier(identifier)) {
id = identifier;
} else // Unknown so will be created as FrameBodyUnsupported
{
id = UNSUPPORTED_ID;
}
}
logger.fine(getLoggingFilename() + ":Identifier was:" + identifier + " reading using:" + id + "with frame size:" + frameSize);
//Read extra bits appended to frame header for various encodings
//These are not included in header size but are included in frame size but won'timer be read when we actually
//try to read the frame body data
int extraHeaderBytesCount = 0;
int decompressedFrameSize = -1;
if (((EncodingFlags) encodingFlags).isCompression()) {
//Read the Decompressed Size
decompressedFrameSize = byteBuffer.getInt();
extraHeaderBytesCount = FRAME_COMPRESSION_UNCOMPRESSED_SIZE;
logger.fine(getLoggingFilename() + ":Decompressed frame size is:" + decompressedFrameSize);
}
if (((EncodingFlags) encodingFlags).isEncryption()) {
//Consume the encryption byte
extraHeaderBytesCount += FRAME_ENCRYPTION_INDICATOR_SIZE;
encryptionMethod = byteBuffer.get();
}
if (((EncodingFlags) encodingFlags).isGrouping()) {
//Read the Grouping byte, but do nothing with it
extraHeaderBytesCount += FRAME_GROUPING_INDICATOR_SIZE;
groupIdentifier = byteBuffer.get();
}
if (((EncodingFlags) encodingFlags).isNonStandardFlags()) {
//Probably corrupt so treat as a standard frame
logger.severe(getLoggingFilename() + ":InvalidEncodingFlags:" + Hex.asHex(((EncodingFlags) encodingFlags).getFlags()));
}
if (((EncodingFlags) encodingFlags).isCompression()) {
if (decompressedFrameSize > (100 * frameSize)) {
throw new InvalidFrameException(identifier + " is invalid frame, frame size " + frameSize + " cannot be:" + decompressedFrameSize + " when uncompressed");
}
}
//Work out the real size of the frameBody data
int realFrameSize = frameSize - extraHeaderBytesCount;
if (realFrameSize <= 0) {
throw new InvalidFrameException(identifier + " is invalid frame, realframeSize is:" + realFrameSize);
}
ByteBuffer frameBodyBuffer;
//Read the body data
try {
if (((EncodingFlags) encodingFlags).isCompression()) {
frameBodyBuffer = ID3Compression.uncompress(identifier, getLoggingFilename(), byteBuffer, decompressedFrameSize, realFrameSize);
if (((EncodingFlags) encodingFlags).isEncryption()) {
frameBody = readEncryptedBody(id, frameBodyBuffer, decompressedFrameSize);
} else {
frameBody = readBody(id, frameBodyBuffer, decompressedFrameSize);
}
} else if (((EncodingFlags) encodingFlags).isEncryption()) {
frameBody = readEncryptedBody(identifier, byteBuffer, frameSize);
} else {
//Create Buffer that only contains the body of this frame rather than the remainder of tag
frameBodyBuffer = byteBuffer.slice();
frameBodyBuffer.limit(realFrameSize);
frameBody = readBody(id, frameBodyBuffer, realFrameSize);
}
//it then be created as FrameBodyUnsupported
if (!(frameBody instanceof ID3v23FrameBody)) {
logger.config(getLoggingFilename() + ":Converted frameBody with:" + identifier + " to deprecated frameBody");
frameBody = new FrameBodyDeprecated((AbstractID3v2FrameBody) frameBody);
}
} finally {
//Update position of main buffer, so no attempt is made to reread these bytes
byteBuffer.position(byteBuffer.position() + realFrameSize);
}
}
Aggregations