Package io.permazen.util
Class ByteWriter
java.lang.Object
io.permazen.util.ByteWriter
Writes bytes to a buffer.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionReturn a view of this instance as anOutputStream
.byte[]
getBytes()
Retrieve all of the bytes that have been written to this instance.byte[]
getBytes
(int off) Retrieve the bytes that have been written to this instance, starting at the given offset.byte[]
getBytes
(int off, int len) Retrieve a sub-range of the bytes that have been written to this instance.int
Get the current buffer length.int
mark()
Mark current position.void
reset
(int mark) Reset write position to a previously marked position.void
write
(byte[] data) Write an array of bytes to this instance.void
write
(byte[] data, int off, int len) Write a sub-range from an array of bytes.void
write
(ByteReader reader) Read all remaining content from the givenByteReader
and write it to this instance.void
writeByte
(int value) Write a single byte to this instance.
-
Constructor Details
-
ByteWriter
public ByteWriter()Default constructor. -
ByteWriter
public ByteWriter(int capacity) Constructor.- Parameters:
capacity
- initial capacity of buffer
-
-
Method Details
-
getBytes
public byte[] getBytes()Retrieve all of the bytes that have been written to this instance.- Returns:
- byte content written so far (not necessarily a copy; caller must not modify)
-
getBytes
public byte[] getBytes(int off) Retrieve the bytes that have been written to this instance, starting at the given offset.- Parameters:
off
- offset into written bytes- Returns:
- byte content written so far starting at
off
(not necessarily a copy; caller must not modify) - Throws:
IndexOutOfBoundsException
- ifoff
orlen
is out of bounds
-
getBytes
public byte[] getBytes(int off, int len) Retrieve a sub-range of the bytes that have been written to this instance.- Parameters:
off
- offset into written byteslen
- desired length- Returns:
len
bytes written so far starting fromoff
(not necessarily a copy; caller must not modify)- Throws:
IndexOutOfBoundsException
- ifoff
orlen
is out of bounds
-
writeByte
public void writeByte(int value) Write a single byte to this instance.- Parameters:
value
- byte to write; all but the lower 8 bits are ignored
-
write
Read all remaining content from the givenByteReader
and write it to this instance.- Parameters:
reader
- source for bytes to write
-
write
public void write(byte[] data) Write an array of bytes to this instance.- Parameters:
data
- bytes to write
-
write
public void write(byte[] data, int off, int len) Write a sub-range from an array of bytes.- Parameters:
data
- bytes to writeoff
- offset intodata
len
- the number of bytes to write- Throws:
IndexOutOfBoundsException
- ifoff
orlen
is out of bounds
-
getLength
public int getLength()Get the current buffer length. Returns the same value as#mark
.- Returns:
- number of bytes written so far
-
mark
public int mark()Mark current position. Returns the same value as#getLength
.- Returns:
- number of bytes written so far
-
reset
public void reset(int mark) Reset write position to a previously marked position.- Parameters:
mark
- value previously returned bymark()
- Throws:
IndexOutOfBoundsException
- ifmark
is out of bounds
-
asOutputStream
Return a view of this instance as anOutputStream
.- Returns:
- streaming view of this instance
-