it.unimi.dsi.fastutil.io
Class FastBufferedOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by it.unimi.dsi.fastutil.io.MeasurableOutputStream
          extended by it.unimi.dsi.fastutil.io.FastBufferedOutputStream
All Implemented Interfaces:
MeasurableStream, RepositionableStream, Closeable, Flushable

public class FastBufferedOutputStream
extends MeasurableOutputStream
implements RepositionableStream

Lightweight, unsynchronized output stream buffering class with measurability and repositionability.

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. The main features follow.

Since:
4.4

Field Summary
protected  int avail
          The number of buffer bytes available starting from pos (it must be always equal to buffer.length - pos).
protected  byte[] buffer
          The internal buffer.
static int DEFAULT_BUFFER_SIZE
          The default size of the internal buffer in bytes (8Ki).
protected  OutputStream os
          The underlying output stream.
protected  int pos
          The current position in the buffer.
 
Constructor Summary
FastBufferedOutputStream(OutputStream os)
          Creates a new fast buffered ouptut stream by wrapping a given output stream with a buffer of DEFAULT_BUFFER_SIZE bytes.
FastBufferedOutputStream(OutputStream os, int bufferSize)
          Creates a new fast buffered output stream by wrapping a given output stream with a given buffer size.
 
Method Summary
 void close()
           
 void flush()
           
 long length()
          Returns the length of the underlying output stream, if it is measurable.
 long position()
          Returns the current stream position.
 void position(long newPosition)
          Repositions the stream.
 void write(byte[] b, int offset, int length)
           
 void write(int b)
           
 
Methods inherited from class java.io.OutputStream
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 (8Ki).

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 (it must be always equal to buffer.length - pos).


os

protected OutputStream os
The underlying output stream.

Constructor Detail

FastBufferedOutputStream

public FastBufferedOutputStream(OutputStream os,
                                int bufferSize)
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.
bufferSize - the size in bytes of the internal buffer.

FastBufferedOutputStream

public FastBufferedOutputStream(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 IOException
Specified by:
write in class OutputStream
Throws:
IOException

write

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

flush

public void flush()
           throws IOException
Specified by:
flush in interface Flushable
Overrides:
flush in class OutputStream
Throws:
IOException

close

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

position

public long position()
              throws IOException
Description copied from interface: RepositionableStream
Returns the current stream position.

Specified by:
position in interface MeasurableStream
Specified by:
position in interface RepositionableStream
Returns:
the current stream position.
Throws:
IOException

position

public void position(long newPosition)
              throws IOException
Repositions the stream.

Note that this method performs a flush() before changing the underlying stream position.

Specified by:
position in interface RepositionableStream
Parameters:
newPosition - the new stream position.
Throws:
IOException

length

public long length()
            throws IOException
Returns the length of the underlying output stream, if it is measurable.

Note that this method performs a flush() before detecting the length.

Specified by:
length in interface MeasurableStream
Returns:
the length of the underlying output stream.
Throws:
UnsupportedOperationException - if the underlying output stream is not measurable and cannot provide a FileChannel.
IOException


Copyright © 2011. All Rights Reserved.