Package io.permazen.util
Class ByteReader
java.lang.Object
io.permazen.util.ByteReader
Reads bytes from a buffer.
-
Constructor Summary
ConstructorDescriptionByteReader
(byte[] buf) Constructor.ByteReader
(byte[] buf, int off) Constructor.ByteReader
(byte[] buf, int off, int len) Constructor.ByteReader
(ByteWriter writer) Constructor.ByteReader
(ByteWriter writer, int mark) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionReturn a view of this instance as anInputStream
.byte[]
getBytes()
Copy all the of bytes in the buffer.byte[]
getBytes
(int off) Copy a range of bytes from the given offset to the end of the buffer.byte[]
getBytes
(int off, int len) Copy a range of bytes from the buffer.int
getMax()
Get maximum offset into buffer.int
Get current offset into buffer.int
mark()
Mark current read position.int
peek()
Peek at next byte, if any.int
readByte()
Read the next byte.byte[]
readBytes
(int len) Read the specified number of bytes.byte[]
Read all the of remaining bytes and advance the read position to the end.int
remain()
Get the number of bytes remaining.void
reset
(int mark) Reset read position to a previously marked position.void
skip
(int num) Skip over bytes.void
unread()
Unread the previously read byte.void
unread
(int len) Unread the given number of previously read bytes.
-
Constructor Details
-
ByteReader
public ByteReader(byte[] buf) Constructor. The provided array is read from directly; no copy is made.- Parameters:
buf
- array to read from- Throws:
NullPointerException
- ifbuf
is null
-
ByteReader
public ByteReader(byte[] buf, int off) Constructor. The provided array is read from directly; no copy is made.- Parameters:
buf
- array to read fromoff
- starting offset intobuf
- Throws:
IndexOutOfBoundsException
- ifoff
is out of boundsNullPointerException
- ifbuf
is null
-
ByteReader
public ByteReader(byte[] buf, int off, int len) Constructor. The provided array is read from directly; no copy is made.- Parameters:
buf
- array to read fromoff
- offset intobuf
len
- number of bytes to read- Throws:
IndexOutOfBoundsException
- ifoff
orlen
are out of boundsNullPointerException
- ifbuf
is null
-
ByteReader
Constructor. Takes a snapshot of the given writer's entire content.- Parameters:
writer
-ByteWriter
to read data from- Throws:
NullPointerException
- ifwriter
is null
-
ByteReader
Constructor. Takes a snapshot of the given writer's content starting at the specified position.- Parameters:
writer
-ByteWriter
to read data frommark
- position previously returned byByteWriter.mark()
- Throws:
IndexOutOfBoundsException
- ifmark
is out of boundsNullPointerException
- ifwriter
is null
-
-
Method Details
-
peek
public int peek()Peek at next byte, if any.- Returns:
- next byte (0-255)
- Throws:
IndexOutOfBoundsException
- if there are no more bytes
-
readByte
public int readByte()Read the next byte.- Returns:
- next byte (0-255)
- Throws:
IndexOutOfBoundsException
- if there are no more bytes
-
unread
public void unread()Unread the previously read byte. Equivalent tounread(1)
.- Throws:
IndexOutOfBoundsException
- if there are no more bytes to unread
-
unread
public void unread(int len) Unread the given number of previously read bytes.- Parameters:
len
- the number of bytes to unread- Throws:
IndexOutOfBoundsException
- if there are no more bytes to unread
-
readBytes
public byte[] readBytes(int len) Read the specified number of bytes.- Parameters:
len
- number of bytes to read- Returns:
- bytes read
- Throws:
IndexOutOfBoundsException
- if there are not enough bytesIllegalArgumentException
- iflen
is negative
-
readRemaining
public byte[] readRemaining()Read all the of remaining bytes and advance the read position to the end.- Returns:
- copy of the remaining data
-
remain
public int remain()Get the number of bytes remaining.- Returns:
- bytes remaining
-
skip
public void skip(int num) Skip over bytes.- Parameters:
num
- the number of bytes to skip- Throws:
IndexOutOfBoundsException
- ifnum
is negativeIndexOutOfBoundsException
- if less thannum
bytes remain
-
getOffset
public int getOffset()Get current offset into buffer.- Returns:
- current offset
-
getMax
public int getMax()Get maximum offset into buffer.- Returns:
- maximum offset
-
getBytes
public byte[] getBytes(int off, int len) Copy a range of bytes from the buffer. Does not change the read position.- Parameters:
off
- offset into bufferlen
- number of bytes- Returns:
- copy of the specified byte range
- Throws:
IndexOutOfBoundsException
- ifoff
and/orlen
is out of bounds
-
getBytes
public byte[] getBytes(int off) Copy a range of bytes from the given offset to the end of the buffer. Does not change the read position.- Parameters:
off
- offset into buffer- Returns:
- copy of the specified byte range
- Throws:
IndexOutOfBoundsException
- ifoff
is out of bounds
-
getBytes
public byte[] getBytes()Copy all the of bytes in the buffer. Does not change the read position.- Returns:
- copy of the entire buffer
-
mark
public int mark()Mark current read position.- Returns:
- the current offset
-
reset
public void reset(int mark) Reset read position to a previously marked position.- Parameters:
mark
- value previously returned bymark()
- Throws:
IndexOutOfBoundsException
- ifmark
is out of bounds
-
asInputStream
Return a view of this instance as anInputStream
.- Returns:
- streaming view of this instance
-