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

void pgbennett::id3::ID3v2Tag::writeTag (  ) throws FileNotFoundException, IOException [inline]

Saves all the information in the tag to the file passed to the constructor. If a tag doesn't exist, a tag is prepended to the file. If the padding has not changed since the creation of this object and the size is less than the original size + the original padding, then the previous tag and part of the previous padding will be overwritten. Otherwise, a new tag will be prepended to the file.

Exceptions:
FileNotFoundExceptionif an error occurs
IOExceptionif an error occurs

Implements pgbennett::id3::ID3Tag.

Definition at line 382 of file ID3v2Tag.java.

References exists, getBytes(), getSize(), getTotalSize(), head, mp3, mpegOffset, pgbennett::id3::ID3v2Header::setExtendedHeader(), pgbennett::id3::ID3v2Header::setFooter(), pgbennett::id3::ID3v2Header::setTagSize(), pgbennett::id3::ID3v2Header::setUnsynchronisation(), and writtenTagSize.

Referenced by pgbennett::id3::MP3File::writeTags().

                                                                     {
        RandomAccessFile raf = null;
        // We write version id3v2.3.0
        head.setVersion(3,0);
        // We do not support unsynchronization
        head.setUnsynchronisation(false);
        // Do not write out any footers because then the tag is not allowed
        // to have padding - we would have to write out the whole
        // file again
        head.setFooter(false);
        // Do not support writing extended header
        head.setExtendedHeader(false);
        
        int curSize = getTotalSize();
        head.setTagSize(getSize());
        RandomAccessFile rafTemp = null;
        
        try {
            
            // This means that the file does not need to change size
            if (mpegOffset == curSize) {
                raf = new RandomAccessFile(mp3, "rw");
                byte[] out = getBytes();
                raf.seek(0);
                raf.write(out);
            } else {
                // Mar 2004 Peter Bennett - chnage to use a buffer
                // of 128000 and a new file instead of reading
                // the whole file into storage
                // START of Guillaume Techene's modification
                
                byte[] id3 = getBytes();
                //        long size = raf.length() - mpegOffset;
                //        byte[] previous_file = new byte[(int) size];
                File mp3Temp = new File(mp3.getAbsolutePath()+".XTEMPX");
                if (!mp3.renameTo(mp3Temp))
                    throw new IOException("Unable to rename " + mp3.getAbsolutePath() +
                    " to " + mp3Temp.getAbsolutePath());
                
                raf = new RandomAccessFile(mp3, "rw");
                rafTemp = new RandomAccessFile(mp3Temp, "rw");
                
                byte[] buffer = new byte [128000];
                int leng;
                rafTemp.seek(mpegOffset);
                
                //        if (raf.read(previous_file) != previous_file.length) {
                //          throw new IOException("ID3v2Tag.removeTag: unexpected"
                //                                + " end of file encountered");
                //        }
                
                //        raf.setLength(size + id3.length);
                //        raf.seek(0);
                raf.write(id3);
                //        raf.write(previous_file);
                for (;;) {
                    leng=rafTemp.read(buffer);
                    if (leng==-1)
                        break;
                    raf.write(buffer,0,leng);
                }
                raf.close();
                raf=null;
                rafTemp.close();
                rafTemp=null;
                if (!mp3Temp.delete())
                    throw new IOException("Unable to delete " + mp3Temp.getAbsolutePath());
                
                // END of Guillaume Techene's modification
            }
        }
        finally {
            if (raf != null)
                raf.close();
            if (rafTemp != null)
                rafTemp.close();
        }
        
        writtenTagSize = curSize;
        //    writtenPadding = padding;
        exists = true;
    }

Here is the call graph for this function:

Here is the caller graph for this function:


Generated by  Doxygen 1.6.0   Back to index