Package io.permazen.util
Class ByteData.Writer
java.lang.Object
java.io.OutputStream
io.permazen.util.ByteData.Writer
- All Implemented Interfaces:
Closeable
,Flushable
,AutoCloseable
- Enclosing class:
- ByteData
Gathers data from which to build a
ByteData
instance.
If any write operation would cause the total size to exceed Integer.MAX_VALUE
, then
an IndexOutOfBoundsException
is thrown.
Instances are thread safe.
-
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Close this instance.void
flush()
Flush this instance.void
makeRoom
(int len) Expand the internal buffer as needed to ensure there is room to add the specified number of additional bytes.void
reset()
Reset this instance so that it contains zero bytes.int
size()
Get the number of bytes written to this instance so far.Return aByteData
containing the data written to this instance so far.void
truncate
(int size) Truncate this instance to discard all but the firstsize
bytes previously written.void
write
(byte[] data) void
write
(byte[] data, int off, int len) void
write
(int b) void
Write the given byte data to this instance.Methods inherited from class java.io.OutputStream
nullOutputStream
-
Method Details
-
write
public void write(int b) - Specified by:
write
in classOutputStream
-
write
public void write(byte[] data) - Overrides:
write
in classOutputStream
-
write
public void write(byte[] data, int off, int len) - Overrides:
write
in classOutputStream
-
flush
public void flush()Flush this instance.The implementation in
ByteData.Writer
does nothing.- Specified by:
flush
in interfaceFlushable
- Overrides:
flush
in classOutputStream
-
close
public void close()Close this instance.The implementation in
ByteData.Writer
does nothing.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classOutputStream
-
write
Write the given byte data to this instance.- Parameters:
data
- data to write- Throws:
IllegalArgumentException
- ifdata
is null
-
toByteData
Return aByteData
containing the data written to this instance so far.- Returns:
- the data written to this writer
-
size
public int size()Get the number of bytes written to this instance so far.- Returns:
- size of this instance
-
reset
public void reset()Reset this instance so that it contains zero bytes.Equivalent to:
truncate(0)
. -
truncate
public void truncate(int size) Truncate this instance to discard all but the firstsize
bytes previously written.- Parameters:
size
- new truncated size- Throws:
IndexOutOfBoundsException
- ifsize
is negative or greater than this instance's current size
-
makeRoom
public void makeRoom(int len) Expand the internal buffer as needed to ensure there is room to add the specified number of additional bytes.- Parameters:
len
- number of additional bytes to make room for- Throws:
IndexOutOfBoundsException
- iflen
is negative or would cause the buffer to exceedInteger.MAX_VALUE
bytes
-