- java.lang.Object
-
- java.io.InputStream
-
- net.morimekta.io.BinaryInputStream
-
- All Implemented Interfaces:
Closeable
,AutoCloseable
- Direct Known Subclasses:
BigEndianBinaryInputStream
,LittleEndianBinaryInputStream
public abstract class BinaryInputStream extends InputStream
IO-Optimized binary reader. This is somewhat similar to the native javaObjectInputStream
, but that has some specific properties.-
The stream never reads more from the enveloped input
stream than is consumed. This differs from
ObjectInputStream
which is also buffering bytes. -
Has methods designed to control the endian-ness of the
written numbers. See
BigEndianBinaryInputStream
andLittleEndianBinaryInputStream
for the two main variants. - Each basic value type has two reader methods, one that will fail if the entire value is not read, and one that passes and returns some default value on EOF.
-
-
Constructor Summary
Constructors Constructor Description BinaryInputStream(InputStream in)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description int
available()
void
close()
void
expect(byte[] out)
Read binary data from stream.byte
expectByte()
Read a byte from the input stream.byte[]
expectBytes(int bytes)
Read binary data from stream.double
expectDouble()
Read a double from the input stream.float
expectFloat()
Read a float from the input stream.int
expectInt()
Read an int from the input stream.int
expectIntBase128()
Read a signed number as base128 (integer with variable number of bytes, determined as part of the bytes themselves), using the endianness to determine order of 7-byte block assembly.int
expectIntZigzag()
Read a long number as zigzag encoded from the stream.long
expectLong()
Read a long int from the input stream.long
expectLongBase128()
Read a signed number as base128 (integer with variable number of bytes, determined as part of the bytes themselves), using the endianness to determine order of 7-byte block assembly.long
expectLongZigzag()
Read a long number as zigzag encoded from the stream.short
expectShort()
Read a short from the input stream.long
expectSigned(int bytes)
Read an signed number from the input stream.int
expectUInt16()
Read an unsigned short from the input stream.int
expectUInt24()
Read an unsigned short from the input stream.int
expectUInt32()
Read an unsigned int from the input stream.int
expectUInt8()
Read an unsigned byte from the input stream.long
expectULong32()
long
expectULong40()
long
expectULong48()
long
expectULong56()
long
expectULong64()
int
expectUnsigned(int bytes)
Read an unsigned number from the input stream.long
expectUnsignedLong(int bytes)
Read an unsigned number from the input stream.protected abstract int
internalReadIntBase128(int i)
protected abstract long
internalReadLongBase128(int i)
void
mark(int readLimit)
boolean
markSupported()
int
read()
Read a single byte.int
read(byte[] out)
Read binary data from stream.int
read(byte[] out, int off, int len)
Read binary data from stream.int
readIntBase128()
Read a signed number as base128 (integer with variable number of bytes, determined as part of the bytes themselves), using the endianness to determine order of 7-byte block assembly.int
readIntVarint()
Deprecated.int
readIntZigzag()
Read a long number as zigzag encoded from the stream.long
readLongBase128()
Read a signed number as base128 (integer with variable number of bytes, determined as part of the bytes themselves), using the endianness to determine order of 7-byte block assembly.long
readLongVarint()
Deprecated.long
readLongZigzag()
Read a long number as zigzag encoded from the stream.int
readUInt16()
Read an unsigned short from the input stream.void
reset()
protected abstract int
unshift2bytes(int b1, int b2)
protected abstract int
unshift3bytes(int b1, int b2, int b3)
protected abstract int
unshift4bytes(int b1, int b2, int b3, int b4)
protected abstract long
unshift8bytes(long b1, long b2, long b3, long b4, long b5, long b6, long b7, long b8)
protected abstract long
unshiftNBytes(byte[] bytes)
-
Methods inherited from class java.io.InputStream
nullInputStream, readAllBytes, readNBytes, readNBytes, skip, transferTo
-
-
-
-
Constructor Detail
-
BinaryInputStream
public BinaryInputStream(InputStream in)
-
-
Method Detail
-
read
public int read() throws IOException
Read a single byte.- Specified by:
read
in classInputStream
- Returns:
- the byte value, or -1 if end of stream.
- Throws:
IOException
- if unable to read from stream.
-
read
public int read(byte[] out) throws IOException
Read binary data from stream.- Overrides:
read
in classInputStream
- Parameters:
out
- The output buffer to read into.- Throws:
IOException
- if unable to read from stream.
-
read
public int read(byte[] out, int off, int len) throws IOException
Read binary data from stream.- Overrides:
read
in classInputStream
- Parameters:
out
- The output buffer to read into.off
- Offset in out array to writeBinary to.len
- Number of bytes to read.- Throws:
IOException
- if unable to read from stream.
-
close
public void close() throws IOException
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classInputStream
- Throws:
IOException
-
available
public int available() throws IOException
- Overrides:
available
in classInputStream
- Throws:
IOException
-
markSupported
public boolean markSupported()
- Overrides:
markSupported
in classInputStream
-
mark
public void mark(int readLimit)
- Overrides:
mark
in classInputStream
-
reset
public void reset() throws IOException
- Overrides:
reset
in classInputStream
- Throws:
IOException
-
expect
public void expect(byte[] out) throws IOException
Read binary data from stream.- Parameters:
out
- The output buffer to read into.- Throws:
IOException
- if unable to read from stream.
-
expectByte
public byte expectByte() throws IOException
Read a byte from the input stream.- Returns:
- The number read.
- Throws:
IOException
- If no byte to read.
-
expectShort
public short expectShort() throws IOException
Read a short from the input stream.- Returns:
- The number read.
- Throws:
IOException
- if unable to read from stream.
-
expectInt
public int expectInt() throws IOException
Read an int from the input stream.- Returns:
- The number read.
- Throws:
IOException
- if unable to read from stream.
-
expectLong
public long expectLong() throws IOException
Read a long int from the input stream.- Returns:
- The number read.
- Throws:
IOException
- if unable to read from stream.
-
expectFloat
public float expectFloat() throws IOException
Read a float from the input stream.- Returns:
- The number read.
- Throws:
IOException
- if unable to read from stream.
-
expectDouble
public double expectDouble() throws IOException
Read a double from the input stream.- Returns:
- The number read.
- Throws:
IOException
- if unable to read from stream.
-
expectBytes
public byte[] expectBytes(int bytes) throws IOException
Read binary data from stream.- Parameters:
bytes
- Number of bytes to read.- Returns:
- The binary wrapper.
- Throws:
IOException
- if unable to read from stream.
-
expectUInt8
public int expectUInt8() throws IOException
Read an unsigned byte from the input stream.- Returns:
- Unsigned byte.
- Throws:
IOException
- If no number to read.
-
expectUInt16
public int expectUInt16() throws IOException
Read an unsigned short from the input stream.- Returns:
- The number read.
- Throws:
IOException
- If no number to read.
-
readUInt16
public int readUInt16() throws IOException
Read an unsigned short from the input stream.- Returns:
- The number read.
- Throws:
IOException
- If no number to read.
-
expectUInt24
public int expectUInt24() throws IOException
Read an unsigned short from the input stream.- Returns:
- The number read.
- Throws:
IOException
- If no number to read.
-
expectUInt32
public int expectUInt32() throws IOException
Read an unsigned int from the input stream.- Returns:
- The number read.
- Throws:
IOException
- If no number to read.
-
expectULong32
public long expectULong32() throws IOException
- Throws:
IOException
-
expectULong40
public long expectULong40() throws IOException
- Throws:
IOException
-
expectULong48
public long expectULong48() throws IOException
- Throws:
IOException
-
expectULong56
public long expectULong56() throws IOException
- Throws:
IOException
-
expectULong64
public long expectULong64() throws IOException
- Throws:
IOException
-
expectUnsigned
public int expectUnsigned(int bytes) throws IOException
Read an unsigned number from the input stream.- Parameters:
bytes
- Number of bytes to read.- Returns:
- The number read.
- Throws:
IOException
- if unable to read from stream.
-
expectUnsignedLong
public long expectUnsignedLong(int bytes) throws IOException
Read an unsigned number from the input stream.- Parameters:
bytes
- Number of bytes to read.- Returns:
- The number read.
- Throws:
IOException
- if unable to read from stream.
-
expectSigned
public long expectSigned(int bytes) throws IOException
Read an signed number from the input stream.- Parameters:
bytes
- Number of bytes to read.- Returns:
- The number read.
- Throws:
IOException
- if unable to read from stream.
-
readIntZigzag
public int readIntZigzag() throws IOException
Read a long number as zigzag encoded from the stream. The least significant bit becomes the sign, and the actual value is absolute and shifted one bit. This makes it maximum compressed both when positive and negative.- Returns:
- The zigzag decoded value.
- Throws:
IOException
- if unable to read from stream.
-
expectIntZigzag
public int expectIntZigzag() throws IOException
Read a long number as zigzag encoded from the stream. The least significant bit becomes the sign, and the actual value is absolute and shifted one bit. This makes it maximum compressed both when positive and negative.- Returns:
- The zigzag decoded value.
- Throws:
IOException
- if unable to read from stream.
-
readLongZigzag
public long readLongZigzag() throws IOException
Read a long number as zigzag encoded from the stream. The least significant bit becomes the sign, and the actual value is absolute and shifted one bit. This makes it maximum compressed both when positive and negative.- Returns:
- The zigzag decoded value.
- Throws:
IOException
- if unable to read from stream.
-
expectLongZigzag
public long expectLongZigzag() throws IOException
Read a long number as zigzag encoded from the stream. The least significant bit becomes the sign, and the actual value is absolute and shifted one bit. This makes it maximum compressed both when positive and negative.- Returns:
- The zigzag decoded value.
- Throws:
IOException
- if unable to read from stream.
-
readIntBase128
public int readIntBase128() throws IOException
Read a signed number as base128 (integer with variable number of bytes, determined as part of the bytes themselves), using the endianness to determine order of 7-byte block assembly.NOTE: Reading base128 accepts end of stream as '0'.
- Returns:
- The base128 number read from stream.
- Throws:
IOException
- if unable to read from stream.
-
expectIntBase128
public int expectIntBase128() throws IOException
Read a signed number as base128 (integer with variable number of bytes, determined as part of the bytes themselves), using the endianness to determine order of 7-byte block assembly.- Returns:
- The base128 number read from stream.
- Throws:
IOException
- if unable to read from stream.
-
readIntVarint
@Deprecated public int readIntVarint() throws IOException
Deprecated.- Throws:
IOException
-
readLongBase128
public long readLongBase128() throws IOException
Read a signed number as base128 (integer with variable number of bytes, determined as part of the bytes themselves), using the endianness to determine order of 7-byte block assembly.NOTE: Reading base128 accepts end of stream as '0'.
- Returns:
- The base128 number read from stream.
- Throws:
IOException
- if unable to read from stream.
-
expectLongBase128
public long expectLongBase128() throws IOException
Read a signed number as base128 (integer with variable number of bytes, determined as part of the bytes themselves), using the endianness to determine order of 7-byte block assembly.- Returns:
- The base128 number read from stream.
- Throws:
IOException
- if unable to read from stream.
-
readLongVarint
@Deprecated public long readLongVarint() throws IOException
Deprecated.- Throws:
IOException
-
internalReadIntBase128
protected abstract int internalReadIntBase128(int i) throws IOException
- Throws:
IOException
-
internalReadLongBase128
protected abstract long internalReadLongBase128(int i) throws IOException
- Throws:
IOException
-
unshift2bytes
protected abstract int unshift2bytes(int b1, int b2)
-
unshift3bytes
protected abstract int unshift3bytes(int b1, int b2, int b3)
-
unshift4bytes
protected abstract int unshift4bytes(int b1, int b2, int b3, int b4)
-
unshift8bytes
protected abstract long unshift8bytes(long b1, long b2, long b3, long b4, long b5, long b6, long b7, long b8)
-
unshiftNBytes
protected abstract long unshiftNBytes(byte[] bytes)
-
-