- java.lang.Object
-
- java.io.InputStream
-
- com.pervasive.datarush.io.BinaryReader
-
- All Implemented Interfaces:
Closeable
,AutoCloseable
- Direct Known Subclasses:
SplitInputStreamImpl
public class BinaryReader extends InputStream
Provides extended data access methods on binary data flows. Beyond supporting a byte-oriented view of the concatenated tokens, a number of methods for reading primitive type binary formats are provided.BinaryInputStream
is also extended to provide support for reading from other "chunked" byte sources.- See Also:
BinaryBuilder
-
-
Field Summary
Fields Modifier and Type Field Description protected long
absolutePosition
The total number of bytes readprotected byte[]
buffer
The current token valueprotected int
position
The offset of the next byte to read in the tokenprotected int
size
The size of the current tokenprotected InputStream
source
The underlying byte stream
-
Constructor Summary
Constructors Constructor Description BinaryReader(byte[] bytes)
Wraps a byte array with a byte-oriented reader.BinaryReader(InputStream source, int bufferSize)
Wraps anInputStream
with a byte-oriented reader having extended function.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
available()
void
close()
Closes the wrapped port and releases any allocated resources.protected int
fillBuffer()
Fills the working bufferlong
getAbsolutePosition()
Gets the current position in the underlying data.boolean
isEmpty()
Indicates whether the byte stream for reading has been exhausted.protected boolean
loadNextBuffer()
void
mark(int readlimit)
Marking the stream is not a supported operation.boolean
markSupported()
int
read()
int
read(byte[] b)
int
read(byte[] b, int off, int len)
int
readBase128Int()
Reads a base-128 encoded int value from the stream.long
readBase128Long()
Reads a base-128 encoded long value from the stream.byte
readByte()
Reads a single byte from the stream.byte[]
readBytes(int len)
Reads a specified number of bytes from the stream.char
readChar()
Reads a character value from the stream.char[]
readChars(int count)
Reads a specified number of character values from the stream.double
readDouble()
Reads a double value from the stream.float
readFloat()
Reads a float value from the stream.int
readInt()
Reads an int value from the stream.long
readLong()
Reads a long value from the stream.char[]
readSingleByteChars(int count)
Reads a specified number of character values from the stream, assuming the bytes are the least-significant bytes of the values.int
readUnsignedBase128Int()
Reads a base-128 encoded int value from the stream, assuming an unsigned encoding.long
readUnsignedBase128Long()
Reads a base-128 encoded long value from the stream, assuming an unsigned encoding.void
reset()
Resetting the stream is not a supported operation.long
skip(long n)
void
skipBytes(long count)
Advances the read position in the stream the specified number of bytes.String
toString()
-
Methods inherited from class java.io.InputStream
nullInputStream, readAllBytes, readNBytes, readNBytes, transferTo
-
-
-
-
Field Detail
-
source
protected InputStream source
The underlying byte stream
-
buffer
protected byte[] buffer
The current token value
-
position
protected int position
The offset of the next byte to read in the token
-
size
protected int size
The size of the current token
-
absolutePosition
protected long absolutePosition
The total number of bytes read
-
-
Constructor Detail
-
BinaryReader
public BinaryReader(InputStream source, int bufferSize)
Wraps anInputStream
with a byte-oriented reader having extended function. If anIOException
is thrown by the wrapped source, the provided stream will be closed.The wrapped stream should not be accessed directly after creating the stream. Doing so may result in unexpected or otherwise unpredictable behavior in both the stream and the wrapper.
- Parameters:
source
- the stream to wrapbufferSize
- the size of the read buffer to use, in bytes. Data will be read in chunks of this size from the wrapped stream.
-
BinaryReader
public BinaryReader(byte[] bytes)
Wraps a byte array with a byte-oriented reader.- Parameters:
bytes
- the byte array to wrap.
-
-
Method Detail
-
fillBuffer
protected int fillBuffer() throws IOException
Fills the working buffer- Throws:
IOException
-
loadNextBuffer
protected boolean loadNextBuffer() throws IOException
- Throws:
IOException
-
read
public int read() throws IOException
- Specified by:
read
in classInputStream
- Throws:
IOException
-
read
public final int read(byte[] b) throws IOException
- Overrides:
read
in classInputStream
- Throws:
IOException
-
read
public final int read(byte[] b, int off, int len) throws IOException
- Overrides:
read
in classInputStream
- Throws:
IOException
-
skip
public long skip(long n) throws IOException
- Overrides:
skip
in classInputStream
- Throws:
IOException
-
available
public int available()
- Overrides:
available
in classInputStream
-
getAbsolutePosition
public final long getAbsolutePosition()
Gets the current position in the underlying data.- Returns:
- the number of bytes read so far
-
markSupported
public final boolean markSupported()
- Overrides:
markSupported
in classInputStream
-
mark
public final void mark(int readlimit)
Marking the stream is not a supported operation.- Overrides:
mark
in classInputStream
- Throws:
UnsupportedOperationException
- always
-
reset
public final void reset()
Resetting the stream is not a supported operation.- Overrides:
reset
in classInputStream
- Throws:
UnsupportedOperationException
- always
-
close
public final void close()
Closes the wrapped port and releases any allocated resources. Subsequent reads will behave as if end of data has been reached.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classInputStream
-
isEmpty
public boolean isEmpty()
Indicates whether the byte stream for reading has been exhausted.- Returns:
true
if no more bytes are available to be read,false
otherwise.
-
skipBytes
public void skipBytes(long count)
Advances the read position in the stream the specified number of bytes.- Parameters:
count
- the number of bytes to skip
-
readByte
public byte readByte()
Reads a single byte from the stream. The read position is advanced one byte as a result.- Returns:
- the byte at the current position
-
readBytes
public byte[] readBytes(int len)
Reads a specified number of bytes from the stream. The read position is advanced the specified number of bytes.- Parameters:
len
- the number of bytes to read- Returns:
- the next
len
bytes starting at the current position
-
readChar
public char readChar()
Reads a character value from the stream. The read position is advanced 2 bytes.- Returns:
- the character value encoded at the current position
-
readChars
public char[] readChars(int count)
Reads a specified number of character values from the stream. The read position is advanced 2 bytes for each character requested.- Parameters:
count
- the number of characters to read- Returns:
- the next
count
characters starting at the current position
-
readSingleByteChars
public char[] readSingleByteChars(int count)
Reads a specified number of character values from the stream, assuming the bytes are the least-significant bytes of the values. The read position is advanced only one byte for each character requested.- Parameters:
count
- the number of characters to read- Returns:
- the next
count
characters starting at the current position
-
readInt
public int readInt()
Reads an int value from the stream. The read position is advanced 4 bytes.- Returns:
- the int value encoded at the current position
-
readUnsignedBase128Int
public int readUnsignedBase128Int()
Reads a base-128 encoded int value from the stream, assuming an unsigned encoding. The read position is advanced to the position following the last byte of the encoding.- Returns:
- the int value base-128 encoded at the current position
- See Also:
BinaryBuilder.appendUnsignedBase128(int)
-
readBase128Int
public int readBase128Int()
Reads a base-128 encoded int value from the stream. The read position is advanced to the position following the last byte of the encoding.- Returns:
- the int value base-128 encoded at the current position
- See Also:
BinaryBuilder.appendBase128(int)
-
readLong
public long readLong()
Reads a long value from the stream. The read position is advanced 8 bytes.- Returns:
- the long value encoded at the current position
-
readUnsignedBase128Long
public long readUnsignedBase128Long()
Reads a base-128 encoded long value from the stream, assuming an unsigned encoding. The read position is advanced to the position following the last byte of the encoding.- Returns:
- the long value base-128 encoded at the current position
- See Also:
BinaryBuilder.appendUnsignedBase128(long)
-
readBase128Long
public long readBase128Long()
Reads a base-128 encoded long value from the stream. The read position is advanced to the position following the last byte of the encoding.- Returns:
- the long value base-128 encoded at the current position
- See Also:
BinaryBuilder.appendBase128(int)
-
readFloat
public float readFloat()
Reads a float value from the stream. The read position is advanced 4 bytes. The bytes are interpreted usingFloat.intBitsToFloat(int)
.- Returns:
- the float value encoded at the current position
-
readDouble
public double readDouble()
Reads a double value from the stream. The read position is advanced 8 bytes. The bytes are interpreted usingDouble.longBitsToDouble(long)
.- Returns:
- the double value encoded at the current position
-
-