Logo Search packages:      
Sourcecode: f-spot version File versions  Download package

void TagLib::File::Insert ( ByteVector  data,
long  start,
long  replace 
) [inline, inherited]

Inserts a specifed block of data into the file repesented by the current instance at a specified location, replacing a specified number of bytes.

Parameters:
data A ByteVector object containing the data to insert into the file.
start A long value specifying at which point to insert the data.
replace A long value specifying the number of bytes to replace. Typically this is the original size of the data block so that a new block will replace the old one.
Exceptions:
ArgumentNullException data is .

Definition at line 1050 of file File.cs.

References TagLib::File::buffer_size, TagLib::File::BufferSize, TagLib::ByteVector::Count, TagLib::ByteVector::Data, TagLib::File::file_stream, TagLib::File::Mode, TagLib::File::ReadBlock(), TagLib::File::RemoveBlock(), and TagLib::File::WriteBlock().

Referenced by TagLib::File::Insert(), TagLib::Mpeg4::IsoChunkOffsetBox::Overwrite(), TagLib::Mpeg4::IsoChunkLargeOffsetBox::Overwrite(), TagLib::Riff::File::Save(), TagLib::Ogg::File::Save(), TagLib::Mpeg4::File::Save(), TagLib::Flac::File::Save(), TagLib::Asf::File::Save(), TagLib::Aiff::File::Save(), TagLib::Gif::File::SaveMetadata(), and TagLib::Tiff::File::WriteFile().

            {
                  if (data == null)
                        throw new ArgumentNullException ("data");
                  
                  Mode = AccessMode.Write;
                  
                  if (data.Count == replace) {
                        file_stream.Position = start;
                        WriteBlock (data);
                        return;
                  } else if(data.Count < replace) {
                        file_stream.Position = start;
                        WriteBlock (data);
                        RemoveBlock (start + data.Count,
                              replace - data.Count);
                        return;
                  }
                  
                  // Woohoo!  Faster (about 20%) than id3lib at last. I
                  // had to get hardcore and avoid TagLib's high level API
                  // for rendering just copying parts of the file that
                  // don't contain tag data.
                  //
                  // Now I'll explain the steps in this ugliness:
                  
                  // First, make sure that we're working with a buffer
                  // that is longer than the *differnce* in the tag sizes.
                  // We want to avoid overwriting parts that aren't yet in
                  // memory, so this is necessary.
                  
                  int buffer_length = buffer_size;
                  
                  while (data.Count - replace > buffer_length)
                        buffer_length += (int) BufferSize;
                  
                  // Set where to start the reading and writing.
                  
                  long read_position = start + replace;
                  long write_position = start;
                  
                  byte [] buffer;
                  byte [] about_to_overwrite;
                  
                  // This is basically a special case of the loop below.  
                  // Here we're just doing the same steps as below, but 
                  // since we aren't using the same buffer size -- instead
                  // we're using the tag size -- this has to be handled as
                  // a special case.  We're also using File::writeBlock()
                  // just for the tag. That's a bit slower than using char
                  // *'s so, we're only doing it here.
                  
                  file_stream.Position = read_position;
                  about_to_overwrite = ReadBlock (buffer_length).Data;
                  read_position += buffer_length;
                  
                  file_stream.Position = write_position;
                  WriteBlock (data);
                  write_position += data.Count;
                  
                  buffer = new byte [about_to_overwrite.Length];
                  System.Array.Copy (about_to_overwrite, 0, buffer, 0,
                        about_to_overwrite.Length);
                  
                  // Ok, here's the main loop.  We want to loop until the
                  // read fails, which means that we hit the end of the 
                  // file.
                  
                  while (buffer_length != 0) {
                        // Seek to the current read position and read
                        // the data that we're about to overwrite. 
                        // Appropriately increment the readPosition.
                        
                        file_stream.Position = read_position;
                        int bytes_read = file_stream.Read (
                              about_to_overwrite, 0, buffer_length <
                              about_to_overwrite.Length ?
                                    buffer_length :
                                    about_to_overwrite.Length);
                        read_position += buffer_length;
                        
                        // Seek to the write position and write our
                        // buffer. Increment the writePosition.
                        
                        file_stream.Position = write_position;
                        file_stream.Write (buffer, 0,
                              buffer_length < buffer.Length ?
                                    buffer_length : buffer.Length);
                        write_position += buffer_length;
                        
                        // Make the current buffer the data that we read
                        // in the beginning.
                        
                        System.Array.Copy (about_to_overwrite, 0,
                              buffer, 0, bytes_read);
                        
                        // Again, we need this for the last write.  We
                        // don't want to write garbage at the end of our
                        // file, so we need to set the buffer size to
                        // the amount that we actually read.
                        
                        buffer_length = bytes_read;
                  }
            }


Generated by  Doxygen 1.6.0   Back to index