Logo Search packages:      
Sourcecode: jampal version File versions  Download package

void pgbennett::id3::ID3v2Tag::parseFrames ( UnsynchRandomAccessFile  raf ) throws IOException, ID3v2FormatException [inline, private]

Read the frames from the file and create ID3v2Frame objects from the data found.

Parameters:
rafthe open file to read from
Exceptions:
IOExceptionif an error occurs
ID3v2FormatExceptionif an error occurs

Definition at line 222 of file ID3v2Tag.java.

References pgbennett::id3::BinaryParser::convertToInt(), pgbennett::id3::BinaryParser::convertToSynchsafeInt(), ext_head, frames, pgbennett::id3::ID3v2Header::getExtendedHeader(), pgbennett::id3::ID3v2Header::getHeaderSize(), pgbennett::id3::ID3v2Header::getMajorVersion(), pgbennett::id3::ID3v2Header::getTagSize(), pgbennett::id3::ID3v2Header::getUnsynchronisation(), head, MP3EXT_BADID, mpegOffset, padding, and pgbennett::id3::ID3v2Frames::put().

                                             {
        
        int offset = head.getHeaderSize();
        // Actually length of frames + padding
        int framesLength = head.getTagSize();
        int bytesRead = 0;
        int curLength = 0;
        ID3v2Frame frame = null;
        String id = null;
        byte[] buf = new byte[0];
        byte[] flags = new byte [2];
        boolean done = false;
        
        if (head.getExtendedHeader()) {
            framesLength -= ext_head.getBytesRead();
            offset += ext_head.getBytesRead();
        }
        
        raf.seek(offset);
        int majorVersion = head.getMajorVersion();
        while ((bytesRead < framesLength||raf.getAlternative()!=null) && !done) {
            switch(majorVersion) {
                case 2:
                    buf = new byte[3];
                    bytesRead += raf.read(buf);
                    id = new String(buf);
                    if (id.startsWith(MP3EXT_BADID) || id.indexOf(0) != -1) {
                        done=true;
                        break;
                    }
                    if (v2Mappings==null) {
                        v2Mappings = new Properties();
                        v2Mappings.load(ClassLoader.getSystemResourceAsStream
                            ("pgbennett/id3/v2mapping.properties"));
                    }
                    if ("CDM".equals(id)) {
                        // Compressed data
                        bytesRead += raf.read(buf);
                        int compLength = BinaryParser.convertToInt(buf);
                        byte[] method = new byte[1];
                        bytesRead += raf.read(method);
                        byte[] buf2 = new byte[4];
                        bytesRead += raf.read(buf2);
                        int uncompLength = BinaryParser.convertToInt(buf2);
                        byte [] compData = new byte[compLength];
                        bytesRead += raf.read(compData);
                        if (method[0]!='z' && method[0]!='Z')
                            continue;
                        // Decompress
                        byte [] uncompData = new byte[uncompLength];
                        try {
                             java.util.zip.Inflater decompresser = new java.util.zip.Inflater();
                             decompresser.setInput(compData);
                             int resultLength = decompresser.inflate(uncompData);
                             decompresser.end();
                        }
                         catch (Exception ex) {
                             ex.printStackTrace();
                             continue;
                             // compressed=false;
                        }
                        ByteArrayInputStream in = new ByteArrayInputStream(uncompData);
                        raf.setAlternative(in);
                        continue;
                    }
                    id = v2Mappings.getProperty(id);
                    bytesRead += raf.read(buf);
                    curLength = BinaryParser.convertToInt(buf);
                    flags[0]=0;
                    flags[1]=0;
                    break;
                case 3:
                    buf = new byte[4];
                    bytesRead += raf.read(buf);
                    id = new String(buf);
                    if (id.startsWith(MP3EXT_BADID) || id.indexOf(0) != -1) {
                        done=true;
                        break;
                    }
//                    bytesRead += 4;
//                    curLength = raf.readInt();
                    
                    bytesRead += raf.read(buf);
                    curLength = BinaryParser.convertToInt(buf);

                    // Added by Reed
                    if (curLength < 0 || curLength > framesLength - bytesRead) {
                        throw new ID3v2FormatException("ID3v2Tag.parseFrames: "
                        + "Invalid frame size");
                    }

                    bytesRead += raf.read(flags);
                    // Empty the flags since v2.3 flags are not supported
                    // in hte ID3v2Frame class
//                    flags[0]=0;
//                    flags[1]=0;
                    break;
                case 4:
                    raf.setUnsynch(false);
                    buf = new byte[4];
                    bytesRead += raf.read(buf);
                    id = new String(buf);
                    if (id.startsWith(MP3EXT_BADID) || id.indexOf(0) != -1) {
                        done=true;
                        break;
                    }
                    bytesRead += raf.read(buf);
                    curLength = BinaryParser.convertToSynchsafeInt(buf);
                    bytesRead += raf.read(flags);
                    boolean unsynchFrame = ((flags[1]&2)!=0);
                    raf.setUnsynch(head.getUnsynchronisation()||unsynchFrame);
                    break;
            }
            if (!done) {
                buf = new byte[curLength];
                bytesRead += raf.read(buf);
                if (majorVersion==2 && "APIC".equals(id)) {
                    // Picture format difference in version 2
                    byte [] buf2 = new byte[curLength+7];
                    // text encoding
                    buf2[0] = buf[0];
                    // new mime type
                    byte[] nameBytes = {'i','m','a','g','e','/'};
                    System.arraycopy(nameBytes,0, buf2, 1, 6);
                    System.arraycopy(buf,1, buf2, 7, 3);
                    buf2[10]=0;
                    System.arraycopy(buf,4, buf2, 11, buf.length - 4);
                    buf=buf2;
                }
                    
                if (id!=null) {
                    frame = new ID3v2Frame(id, flags, buf, majorVersion);
                    frames.put(id, frame);
                }
            } else {
                // We've hit padding so stop reading
                done = true;
                bytesRead -= buf.length;
            }
        }
        
        // Get around the possible precision loss
        Long tmp = new Long(mpegOffset - offset - bytesRead);
        padding = tmp.intValue();
        raf.setUnsynch(false);
    }

Here is the call graph for this function:


Generated by  Doxygen 1.6.0   Back to index