Search in sources :

Example 1 with TagNotFoundException

use of org.jaudiotagger.tag.TagNotFoundException in project MusicDNA by harjot-oberai.

the class MP3File method readV2Tag.

/**
     * Read V2tag if exists
     *
     * TODO:shouldn'timer we be handing TagExceptions:when will they be thrown
     *
     * @param file
     * @param loadOptions
     * @throws IOException
     * @throws TagException
     */
private void readV2Tag(File file, int loadOptions, int startByte) throws IOException, TagException {
    //a buffer then we can read the IDv2 information without needing any more File I/O
    if (startByte >= AbstractID3v2Tag.TAG_HEADER_LENGTH) {
        logger.finer("Attempting to read id3v2tags");
        FileInputStream fis = null;
        FileChannel fc = null;
        ByteBuffer bb;
        try {
            fis = new FileInputStream(file);
            fc = fis.getChannel();
            // avoid using fc.map method since it does not work on Android ICS and JB. Bug report: https://code.google.com/p/android/issues/detail?id=53637
            // bb = fc.map(FileChannel.MapMode.READ_ONLY,0,startByte);
            bb = ByteBuffer.allocate(startByte);
            fc.read(bb, 0);
        } finally {
            if (fc != null) {
                fc.close();
            }
            if (fis != null) {
                fis.close();
            }
        }
        try {
            bb.rewind();
            if ((loadOptions & LOAD_IDV2TAG) != 0) {
                logger.config("Attempting to read id3v2tags");
                try {
                    this.setID3v2Tag(new ID3v24Tag(bb, file.getName()));
                } catch (TagNotFoundException ex) {
                    logger.config("No id3v24 tag found");
                }
                try {
                    if (id3v2tag == null) {
                        this.setID3v2Tag(new ID3v23Tag(bb, file.getName()));
                    }
                } catch (TagNotFoundException ex) {
                    logger.config("No id3v23 tag found");
                }
                try {
                    if (id3v2tag == null) {
                        this.setID3v2Tag(new ID3v22Tag(bb, file.getName()));
                    }
                } catch (TagNotFoundException ex) {
                    logger.config("No id3v22 tag found");
                }
            }
        } finally {
            bb.clear();
        }
    } else {
        logger.config("Not enough room for valid id3v2 tag:" + startByte);
    }
}
Also used : TagNotFoundException(org.jaudiotagger.tag.TagNotFoundException) FileChannel(java.nio.channels.FileChannel) ByteBuffer(java.nio.ByteBuffer)

Example 2 with TagNotFoundException

use of org.jaudiotagger.tag.TagNotFoundException in project MusicDNA by harjot-oberai.

the class MP3File method extractID3v2TagDataIntoFile.

/**
     * Extracts the raw ID3v2 tag data into a file.
     *
     * This provides access to the raw data before manipulation, the data is written from the start of the file
     * to the start of the Audio Data. This is primarily useful for manipulating corrupted tags that are not
     * (fully) loaded using the standard methods.
     *
     * @param outputFile to write the data to
     * @return
     * @throws TagNotFoundException
     * @throws IOException
     */
public File extractID3v2TagDataIntoFile(File outputFile) throws TagNotFoundException, IOException {
    int startByte = (int) ((MP3AudioHeader) audioHeader).getMp3StartByte();
    if (startByte >= 0) {
        //Read byte into buffer
        FileInputStream fis = new FileInputStream(file);
        FileChannel fc = fis.getChannel();
        ByteBuffer bb = ByteBuffer.allocate(startByte);
        fc.read(bb);
        //Write bytes to outputFile
        FileOutputStream out = new FileOutputStream(outputFile);
        out.write(bb.array());
        out.close();
        fc.close();
        fis.close();
        return outputFile;
    }
    throw new TagNotFoundException("There is no ID3v2Tag data in this file");
}
Also used : TagNotFoundException(org.jaudiotagger.tag.TagNotFoundException) FileChannel(java.nio.channels.FileChannel) ByteBuffer(java.nio.ByteBuffer)

Aggregations

ByteBuffer (java.nio.ByteBuffer)2 FileChannel (java.nio.channels.FileChannel)2 TagNotFoundException (org.jaudiotagger.tag.TagNotFoundException)2