Class FastBufferedOutputStream

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

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.

  • All methods are unsychronized.

  • As an additional feature, this class implements the RepositionableStream and MeasurableStream interfaces. An instance of this class will try to cast the underlying byte stream to a RepositionableStream and to fetch by reflection the FileChannel underlying the given output stream, in this order. If either reference can be successfully fetched, you can use position(long) to reposition the stream. Much in the same way, an instance of this class will try to cast the the underlying byte stream to a MeasurableStream, and if this operation is successful, or if a FileChannel can be detected, then position() and length() will work as expected.

Since:
4.4
  • Field Summary

    Fields
    Modifier and Type Field Description
    static int DEFAULT_BUFFER_SIZE
    The default size of the internal buffer in bytes (8Ki).
  • Constructor Summary

    Constructors
    Constructor Description
    FastBufferedOutputStream​(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.
    FastBufferedOutputStream​(java.io.OutputStream os, byte[] buffer)
    Creates a new fast buffered output stream by wrapping a given output stream with a given buffer.
    FastBufferedOutputStream​(java.io.OutputStream os, int bufferSize)
    Creates a new fast buffered output stream by wrapping a given output stream with a given buffer size.
  • Method Summary

    Modifier and Type Method Description
    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

    nullOutputStream, write

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • 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
  • Constructor Details

    • FastBufferedOutputStream

      public FastBufferedOutputStream​(java.io.OutputStream os, byte[] buffer)
      Creates a new fast buffered output stream by wrapping a given output stream with a given buffer.
      Parameters:
      os - an output stream to wrap.
      buffer - a buffer of positive length.
    • FastBufferedOutputStream

      public FastBufferedOutputStream​(java.io.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​(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 Details

    • 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
    • flush

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

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

      public long position() throws java.io.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:
      java.io.IOException
    • position

      public void position​(long newPosition) throws java.io.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:
      java.io.IOException
    • length

      public long length() throws java.io.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:
      java.lang.UnsupportedOperationException - if the underlying output stream is not measurable and cannot provide a FileChannel.
      java.io.IOException