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,Closeable,Flushable,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
RepositionableStreamandMeasurableStreaminterfaces. An instance of this class will try to cast the underlying byte stream to aRepositionableStreamand to fetch by reflection theFileChannelunderlying the given output stream, in this order. If either reference can be successfully fetched, you can useposition(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 aMeasurableStream, and if this operation is successful, or if aFileChannelcan be detected, thenposition()andlength()will work as expected.
- Since:
- 4.4
-
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_BUFFER_SIZEThe default size of the internal buffer in bytes (8Ki).
-
Constructor Summary
Constructors Constructor Description FastBufferedOutputStream(OutputStream os)Creates a new fast buffered ouptut stream by wrapping a given output stream with a buffer ofDEFAULT_BUFFER_SIZEbytes.FastBufferedOutputStream(OutputStream os, byte[] buffer)Creates a new fast buffered output stream by wrapping a given output stream with a given buffer.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
Modifier and Type Method Description voidclose()voidflush()longlength()Returns the length of the underlying output stream, if it is measurable.longposition()Returns the current stream position.voidposition(long newPosition)Repositions the stream.voidwrite(byte[] b, int offset, int length)voidwrite(int b)-
Methods inherited from class java.io.OutputStream
nullOutputStream, write
-
-
-
-
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
-
-
Constructor Detail
-
FastBufferedOutputStream
public FastBufferedOutputStream(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(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 ofDEFAULT_BUFFER_SIZEbytes.- Parameters:
os- an output stream to wrap.
-
-
Method Detail
-
write
public void write(int b) throws IOException- Specified by:
writein classOutputStream- Throws:
IOException
-
write
public void write(byte[] b, int offset, int length) throws IOException- Overrides:
writein classOutputStream- Throws:
IOException
-
flush
public void flush() throws IOException- Specified by:
flushin interfaceFlushable- Overrides:
flushin classOutputStream- Throws:
IOException
-
close
public void close() throws IOException- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classOutputStream- Throws:
IOException
-
position
public long position() throws IOExceptionDescription copied from interface:RepositionableStreamReturns the current stream position.- Specified by:
positionin interfaceMeasurableStream- Specified by:
positionin interfaceRepositionableStream- Returns:
- the current stream position.
- Throws:
IOException
-
position
public void position(long newPosition) throws IOExceptionRepositions the stream.Note that this method performs a
flush()before changing the underlying stream position.- Specified by:
positionin interfaceRepositionableStream- Parameters:
newPosition- the new stream position.- Throws:
IOException
-
length
public long length() throws IOExceptionReturns the length of the underlying output stream, if it is measurable.Note that this method performs a
flush()before detecting the length.- Specified by:
lengthin interfaceMeasurableStream- Returns:
- the length of the underlying output stream.
- Throws:
UnsupportedOperationException- if the underlying output stream is not measurable and cannot provide aFileChannel.IOException
-
-