Class FastBufferedOutputStream
- All Implemented Interfaces:
MeasurableStream
,RepositionableStream
,Closeable
,Flushable
,AutoCloseable
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
andMeasurableStream
interfaces. An instance of this class will try to cast the underlying byte stream to aRepositionableStream
and to fetch by reflection theFileChannel
underlying 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 aFileChannel
can be detected, thenposition()
andlength()
will work as expected.
- Since:
- 4.4
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
The default size of the internal buffer in bytes (8Ki). -
Constructor Summary
ConstructorDescriptionCreates a new fast buffered ouptut stream by wrapping a given output stream with a buffer ofDEFAULT_BUFFER_SIZE
bytes.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 TypeMethodDescriptionvoid
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
-
Field Details
-
DEFAULT_BUFFER_SIZE
public static final int DEFAULT_BUFFER_SIZEThe default size of the internal buffer in bytes (8Ki).- See Also:
-
-
Constructor Details
-
FastBufferedOutputStream
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
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
Creates a new fast buffered ouptut stream by wrapping a given output stream with a buffer ofDEFAULT_BUFFER_SIZE
bytes.- Parameters:
os
- an output stream to wrap.
-
-
Method Details
-
write
- Specified by:
write
in classOutputStream
- Throws:
IOException
-
write
- Overrides:
write
in classOutputStream
- Throws:
IOException
-
flush
- Specified by:
flush
in interfaceFlushable
- Overrides:
flush
in classOutputStream
- Throws:
IOException
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classOutputStream
- Throws:
IOException
-
position
Description copied from interface:RepositionableStream
Returns the current stream position.- Specified by:
position
in interfaceMeasurableStream
- Specified by:
position
in interfaceRepositionableStream
- Returns:
- the current stream position.
- Throws:
IOException
-
position
Repositions the stream.Note that this method performs a
flush()
before changing the underlying stream position.- Specified by:
position
in interfaceRepositionableStream
- Parameters:
newPosition
- the new stream position.- Throws:
IOException
-
length
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 interfaceMeasurableStream
- Returns:
- the length of the underlying output stream.
- Throws:
UnsupportedOperationException
- if the underlying output stream is not measurable and cannot provide aFileChannel
.IOException
-