com.mucommander.io
Class BufferedRandomOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by com.mucommander.io.RandomAccessOutputStream
          extended by com.mucommander.io.BufferedRandomOutputStream
All Implemented Interfaces:
RandomAccess, java.io.Closeable, java.io.Flushable

public class BufferedRandomOutputStream
extends RandomAccessOutputStream

BufferedRandomOutputStream is a buffered output stream for RandomAccessOutputStream which, unlike a regular java.io.BufferedOutputStream, makes it safe to seek in the underlying RandomAccessOutputStream.

This class uses BufferPool to create the internal buffer, to avoid excessive memory allocation and garbage collection. The buffer is released when this stream is closed.

Author:
Maxence Bernard

Field Summary
static int DEFAULT_BUFFER_SIZE
          The default buffer size if none is specified
 
Constructor Summary
BufferedRandomOutputStream(RandomAccessOutputStream raos)
          Creates a new BufferedRandomOutputStream on top of the given RandomAccessOutputStream.
BufferedRandomOutputStream(RandomAccessOutputStream raos, int size)
          Creates a new BufferedRandomOutputStream on top of the given RandomAccessOutputStream.
 
Method Summary
 void close()
          This method is overridden to release the internal buffer when this stream is closed.
protected  void finalize()
          This method is overridden to release the internal buffer if close() has not been called, to avoid any memory leak.
 void flush()
          Flushes this buffered output stream.
 long getLength()
          Returns the length of the file, in bytes.
 long getOffset()
          Returns the offset (in bytes) from the beginning of the file at which the next read or write occurs.
 void seek(long offset)
          Sets the offset, measured from the beginning of the file, at which the next read or write occurs.
 void setLength(long newLength)
          Sets the length of the file.
 void write(byte[] b)
          Writes the specified byte array to this buffered output stream.
 void write(byte[] b, int off, int len)
          Writes len bytes from the specified byte array starting at offset off to this buffered output stream.
 void write(int b)
          Writes the specified byte to this buffered output stream.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_BUFFER_SIZE

public static final int DEFAULT_BUFFER_SIZE
The default buffer size if none is specified

See Also:
Constant Field Values
Constructor Detail

BufferedRandomOutputStream

public BufferedRandomOutputStream(RandomAccessOutputStream raos)
Creates a new BufferedRandomOutputStream on top of the given RandomAccessOutputStream. An internal buffer of DEFAULT_BUFFER_SIZE bytes is created.

Parameters:
raos - the underlying RandomAccessOutputStream used by this buffered output stream

BufferedRandomOutputStream

public BufferedRandomOutputStream(RandomAccessOutputStream raos,
                                  int size)
Creates a new BufferedRandomOutputStream on top of the given RandomAccessOutputStream. An internal buffer of the specified size is created.

Parameters:
raos - the underlying RandomAccessOutputStream used by this buffered output stream
size - size of the buffer in bytes
Method Detail

write

public void write(int b)
           throws java.io.IOException
Writes the specified byte to this buffered output stream.

Specified by:
write in class java.io.OutputStream
Parameters:
b - the byte to be written
Throws:
java.io.IOException - if an I/O error occurs

write

public void write(byte[] b)
           throws java.io.IOException
Writes the specified byte array to this buffered output stream.

Specified by:
write in class RandomAccessOutputStream
Parameters:
b - the bytes to be written
Throws:
java.io.IOException - if an I/O error occurs

write

public void write(byte[] b,
                  int off,
                  int len)
           throws java.io.IOException
Writes len bytes from the specified byte array starting at offset off to this buffered output stream.

Usually this method stores bytes from the given array into this stream's buffer, flushing the buffer to the underlying output stream as needed. However, if the requested data length is equal or larger than this stream's buffer, then this method will flush the buffer and write the bytes directly to the underlying output stream. Thus redundant RandomBufferedOutputStreams will not copy data unnecessarily.

Specified by:
write in class RandomAccessOutputStream
Parameters:
b - the data.
off - the start offset in the data.
len - the number of bytes to write.
Throws:
java.io.IOException - if an I/O error occurs.

flush

public void flush()
           throws java.io.IOException
Flushes this buffered output stream. This forces any buffered output bytes to be written out to the underlying output stream.

Specified by:
flush in interface java.io.Flushable
Overrides:
flush in class java.io.OutputStream
Throws:
java.io.IOException - if an I/O error occurs.

getOffset

public long getOffset()
               throws java.io.IOException
Description copied from interface: RandomAccess
Returns the offset (in bytes) from the beginning of the file at which the next read or write occurs.

Returns:
the offset (in bytes) from the beginning of the file at which the next read or write occurs
Throws:
java.io.IOException - if an I/O error occurs.

seek

public void seek(long offset)
          throws java.io.IOException
Description copied from interface: RandomAccess
Sets the offset, measured from the beginning of the file, at which the next read or write occurs. The offset may be set beyond the end of the file. Setting the offset beyond the end of the file does not change the file length. The file length will change only by writing after the offset has been set beyond the end of the file.

Parameters:
offset - the new offset position, measured in bytes from the beginning of the file
Throws:
java.io.IOException - if an I/O error occurs

getLength

public long getLength()
               throws java.io.IOException
Description copied from interface: RandomAccess
Returns the length of the file, in bytes.

Returns:
the length of the file, in bytes
Throws:
java.io.IOException - if an I/O error occurs

setLength

public void setLength(long newLength)
               throws java.io.IOException
Description copied from class: RandomAccessOutputStream
Sets the length of the file.

If the present length of the file as returned by the RandomAccess.getLength() method is greater than the newLength argument then the file will be truncated. In this case, if the file offset as returned by the RandomAccess.getOffset() method is greater than newLength then the offset will be equal to newLength after this method returns .

If the present length of the file as returned by the RandomAccess.getLength() method is smaller than the newLength argument then the file will be extended. In this case, the contents of the extended portion of the file are not defined.

Specified by:
setLength in class RandomAccessOutputStream
Parameters:
newLength - the new file's length
Throws:
java.io.IOException - If an I/O error occurred while trying to change the file's length

close

public void close()
           throws java.io.IOException
This method is overridden to release the internal buffer when this stream is closed.

Specified by:
close in interface RandomAccess
Specified by:
close in interface java.io.Closeable
Specified by:
close in class RandomAccessOutputStream
Throws:
java.io.IOException - if an I/O error occurs.

finalize

protected void finalize()
                 throws java.lang.Throwable
This method is overridden to release the internal buffer if close() has not been called, to avoid any memory leak.

Overrides:
finalize in class java.lang.Object
Throws:
java.lang.Throwable


This file is part of muCommander - Copyright (C) 2002-2008 Maxence Bernard