Class AsyncBufferedOutputStream

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.AutoCloseable

    public final class AsyncBufferedOutputStream
    extends java.io.FilterOutputStream
    BufferedOutputStream that asynchronously flushes to disk, so callers don't have to wait until the flush happens. Buffers are put into a queue that is written asynchronously to disk once it is really available.

    This class is thread-safe.

    The error handling (as all stream ops are done asynchronously) is done during write and close. Exceptions on the asynchronous thread will be thrown to the caller either while writing or closing this stream.

    Author:
    thomas.jungblut
    • Field Summary

      • Fields inherited from class java.io.FilterOutputStream

        out
    • Constructor Summary

      Constructors 
      Constructor Description
      AsyncBufferedOutputStream​(java.io.OutputStream out)
      Creates an asynchronous buffered output stream with 8K buffer and 5 maximal buffers.
      AsyncBufferedOutputStream​(java.io.OutputStream out, int bufSize)
      Creates an asynchronous buffered output stream with defined buffersize and 5 maximal buffers.
      AsyncBufferedOutputStream​(java.io.OutputStream out, int bufSize, int maxBuffers)
      Creates an asynchronous buffered output stream.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()  
      void flush()
      Flushes this buffered output stream.
      void write​(byte[] b)  
      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.io.OutputStream

        nullOutputStream
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • AsyncBufferedOutputStream

        public AsyncBufferedOutputStream​(java.io.OutputStream out)
        Creates an asynchronous buffered output stream with 8K buffer and 5 maximal buffers.
      • AsyncBufferedOutputStream

        public AsyncBufferedOutputStream​(java.io.OutputStream out,
                                         int bufSize)
        Creates an asynchronous buffered output stream with defined buffersize and 5 maximal buffers.
      • AsyncBufferedOutputStream

        public AsyncBufferedOutputStream​(java.io.OutputStream out,
                                         int bufSize,
                                         int maxBuffers)
        Creates an asynchronous buffered output stream.
        Parameters:
        out - the outputstream to layer on.
        bufSize - the buffer size.
        maxBuffers - the number of buffers to keep in parallel.
    • Method Detail

      • write

        public void write​(int b)
                   throws java.io.IOException
        Writes the specified byte to this buffered output stream.
        Overrides:
        write in class java.io.FilterOutputStream
        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
        Overrides:
        write in class java.io.FilterOutputStream
        Throws:
        java.io.IOException
      • 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.
        Overrides:
        write in class java.io.FilterOutputStream
        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. It will enforce that the current buffer will be queue for asynchronous flushing no matter what size it has.
        Specified by:
        flush in interface java.io.Flushable
        Overrides:
        flush in class java.io.FilterOutputStream
        Throws:
        java.io.IOException - if an I/O error occurs.
      • 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.FilterOutputStream
        Throws:
        java.io.IOException