it.geosolutions.io.output
Class FastBufferedOutputStream

Object
  extended by OutputStream
      extended by FastBufferedOutputStream
All Implemented Interfaces:
Closeable, Flushable

public class FastBufferedOutputStream
extends 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. If you flush() the stream, the buffer will be emptied, but it will realign again as soon as possible.

Since:
4.4
Author:
Simone Giannecchini, GeoSolutions SAS

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 (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 bufSize)
          Creates a new fast buffered output stream by wrapping a given output stream with a given buffer size.
 
Method Summary
 void close()
           
 void flush()
           
 void write(byte[] b, int offset, int length)
           
 void write(int b)
           
 
Methods inherited from class OutputStream
write
 
Methods inherited from class 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. Note that in case flush() has been called, the number of available buffer bytes might be less than buffer.lengthpos.


os

protected OutputStream os
The underlying output stream.

Constructor Detail

FastBufferedOutputStream

public FastBufferedOutputStream(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.

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


Copyright © 2006-2010 GeoSolutions. All Rights Reserved.