org.archive.io
Class RecyclingFastBufferedOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by org.archive.io.RecyclingFastBufferedOutputStream
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable

public class RecyclingFastBufferedOutputStream
extends java.io.OutputStream

Lightweight, unsynchronised, aligned output stream buffering class.

This class provides buffering for output streams, but it does so with purposes and an internal logic that are radically different from the ones adopted in BufferedOutputStream.

All methods are unsychronised. Moreover, it is guaranteed that all writes performed by this class will be multiples of the given buffer size. If, for instance, you use the default buffer size, writes will be performed on the underlying input stream in multiples of 16384 bytes. This is very important on operating systems that optimise disk reads on disk block boundaries.


Field Summary
protected  int avail
          The number of buffer bytes available starting from pos.
protected  byte[] buffer
          The internal buffer.
static int DEFAULT_BUFFER_SIZE
          The default size of the internal buffer in bytes (16Ki).
protected  java.io.OutputStream os
          The underlying output stream.
protected  int pos
          The current position in the buffer.
 
Constructor Summary
RecyclingFastBufferedOutputStream(java.io.OutputStream os)
          Creates a new fast buffered ouptut stream by wrapping a given output stream with a buffer of DEFAULT_BUFFER_SIZE bytes.
RecyclingFastBufferedOutputStream(java.io.OutputStream os, byte[] buffer)
          Creates a new fast buffered output stream by wrapping a given output stream, using a given buffer
RecyclingFastBufferedOutputStream(java.io.OutputStream os, int bufSize)
          Creates a new fast buffered output stream by wrapping a given output stream with a given buffer size.
 
Method Summary
 void close()
           
 void write(byte[] b, int offset, int length)
           
 void write(int b)
           
 
Methods inherited from class java.io.OutputStream
flush, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_BUFFER_SIZE

public static final int DEFAULT_BUFFER_SIZE
The default size of the internal buffer in bytes (16Ki).

See Also:
Constant Field Values

buffer

protected byte[] buffer
The internal buffer.


pos

protected int pos
The current position in the buffer.


avail

protected int avail
The number of buffer bytes available starting from pos.


os

protected java.io.OutputStream os
The underlying output stream.

Constructor Detail

RecyclingFastBufferedOutputStream

public RecyclingFastBufferedOutputStream(java.io.OutputStream os,
                                         byte[] buffer)
Creates a new fast buffered output stream by wrapping a given output stream, using a given buffer

Parameters:
os - an output stream to wrap.
buffer - buffer to use internally.

RecyclingFastBufferedOutputStream

public RecyclingFastBufferedOutputStream(java.io.OutputStream os,
                                         int bufSize)
Creates a new fast buffered output stream by wrapping a given output stream with a given buffer size.

Parameters:
os - an output stream to wrap.
bufSize - the size in bytes of the internal buffer.

RecyclingFastBufferedOutputStream

public RecyclingFastBufferedOutputStream(java.io.OutputStream os)
Creates a new fast buffered ouptut stream by wrapping a given output stream with a buffer of DEFAULT_BUFFER_SIZE bytes.

Parameters:
os - an output stream to wrap.
Method Detail

write

public void write(int b)
           throws java.io.IOException
Specified by:
write in class java.io.OutputStream
Throws:
java.io.IOException

write

public void write(byte[] b,
                  int offset,
                  int length)
           throws java.io.IOException
Overrides:
write in class java.io.OutputStream
Throws:
java.io.IOException

close

public void close()
           throws java.io.IOException
Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.OutputStream
Throws:
java.io.IOException


Copyright © 2003-2011 Internet Archive. All Rights Reserved.