Package de.jungblut.datastructure
Class AsyncBufferedOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- java.io.FilterOutputStream
-
- de.jungblut.datastructure.AsyncBufferedOutputStream
-
- All Implemented Interfaces:
java.io.Closeable,java.io.Flushable,java.lang.AutoCloseable
public final class AsyncBufferedOutputStream extends java.io.FilterOutputStreamBufferedOutputStream 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
-
-
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 voidclose()voidflush()Flushes this buffered output stream.voidwrite(byte[] b)voidwrite(byte[] b, int off, int len)Writeslenbytes from the specified byte array starting at offsetoffto this buffered output stream.voidwrite(int b)Writes the specified byte to this buffered output stream.
-
-
-
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.IOExceptionWrites the specified byte to this buffered output stream.- Overrides:
writein classjava.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:
writein classjava.io.FilterOutputStream- Throws:
java.io.IOException
-
write
public void write(byte[] b, int off, int len) throws java.io.IOExceptionWriteslenbytes from the specified byte array starting at offsetoffto this buffered output stream.- Overrides:
writein classjava.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.IOExceptionFlushes 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:
flushin interfacejava.io.Flushable- Overrides:
flushin classjava.io.FilterOutputStream- Throws:
java.io.IOException- if an I/O error occurs.
-
close
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classjava.io.FilterOutputStream- Throws:
java.io.IOException
-
-