Package net.morimekta.util.io
Class BinaryReader
- java.lang.Object
-
- java.io.InputStream
-
- net.morimekta.util.io.BinaryReader
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
- Direct Known Subclasses:
BigEndianBinaryReader
,LittleEndianBinaryReader
public abstract class BinaryReader extends java.io.InputStream
IO-Optimized binary reader.
-
-
Constructor Summary
Constructors Constructor Description BinaryReader(java.io.InputStream in)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
close()
void
expect(byte[] out)
Read binary data from stream.Binary
expectBinary(int bytes)
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.int
expectInt()
Read an int from the input stream.long
expectLong()
Read a long int from the input 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.int
expectUnsigned(int bytes)
Read an unsigned number from the input stream.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
readIntVarint()
Write a signed number as varint (integer with variable number of bytes, determined as part of the bytes themselves.int
readIntZigzag()
Read a long number as zigzag encoded from the stream.long
readLongVarint()
Write a signed number as varint (integer with variable number of bytes, determined as part of the bytes themselves.long
readLongZigzag()
Read a long number as zigzag encoded from the stream.int
readUInt16()
Read an unsigned short from the input stream.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)
-
-
-
Method Detail
-
read
public int read() throws java.io.IOException
Read a single byte.- Specified by:
read
in classjava.io.InputStream
- Returns:
- the byte value, or -1 if end of stream.
- Throws:
java.io.IOException
- if unable to read from stream.
-
read
public int read(byte[] out) throws java.io.IOException
Read binary data from stream.- Overrides:
read
in classjava.io.InputStream
- Parameters:
out
- The output buffer to read into.- Throws:
java.io.IOException
- if unable to read from stream.
-
read
public int read(byte[] out, int off, int len) throws java.io.IOException
Read binary data from stream.- Overrides:
read
in classjava.io.InputStream
- Parameters:
out
- The output buffer to read into.off
- Offset in out array to writeBinary to.len
- Number of bytes to read.- Throws:
java.io.IOException
- if unable to read from stream.
-
expect
public void expect(byte[] out) throws java.io.IOException
Read binary data from stream.- Parameters:
out
- The output buffer to read into.- Throws:
java.io.IOException
- if unable to read from stream.
-
close
public void close()
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Overrides:
close
in classjava.io.InputStream
-
expectByte
public byte expectByte() throws java.io.IOException
Read a byte from the input stream.- Returns:
- The number read.
- Throws:
java.io.IOException
- If no byte to read.
-
expectShort
public short expectShort() throws java.io.IOException
Read a short from the input stream.- Returns:
- The number read.
- Throws:
java.io.IOException
- if unable to read from stream.
-
expectInt
public int expectInt() throws java.io.IOException
Read an int from the input stream.- Returns:
- The number read.
- Throws:
java.io.IOException
- if unable to read from stream.
-
expectLong
public long expectLong() throws java.io.IOException
Read a long int from the input stream.- Returns:
- The number read.
- Throws:
java.io.IOException
- if unable to read from stream.
-
expectDouble
public double expectDouble() throws java.io.IOException
Read a double from the input stream.- Returns:
- The number read.
- Throws:
java.io.IOException
- if unable to read from stream.
-
expectBinary
public Binary expectBinary(int bytes) throws java.io.IOException
Read binary data from stream.- Parameters:
bytes
- Number of bytes to read.- Returns:
- The binary wrapper.
- Throws:
java.io.IOException
- if unable to read from stream.
-
expectBytes
public byte[] expectBytes(int bytes) throws java.io.IOException
Read binary data from stream.- Parameters:
bytes
- Number of bytes to read.- Returns:
- The binary wrapper.
- Throws:
java.io.IOException
- if unable to read from stream.
-
expectUInt8
public int expectUInt8() throws java.io.IOException
Read an unsigned byte from the input stream.- Returns:
- Unsigned byte.
- Throws:
java.io.IOException
- If no number to read.
-
expectUInt16
public int expectUInt16() throws java.io.IOException
Read an unsigned short from the input stream.- Returns:
- The number read.
- Throws:
java.io.IOException
- If no number to read.
-
readUInt16
public int readUInt16() throws java.io.IOException
Read an unsigned short from the input stream.- Returns:
- The number read.
- Throws:
java.io.IOException
- If no number to read.
-
expectUInt24
public int expectUInt24() throws java.io.IOException
Read an unsigned short from the input stream.- Returns:
- The number read.
- Throws:
java.io.IOException
- If no number to read.
-
expectUInt32
public int expectUInt32() throws java.io.IOException
Read an unsigned int from the input stream.- Returns:
- The number read.
- Throws:
java.io.IOException
- If no number to read.
-
expectUnsigned
public int expectUnsigned(int bytes) throws java.io.IOException
Read an unsigned number from the input stream.- Parameters:
bytes
- Number of bytes to read.- Returns:
- The number read.
- Throws:
java.io.IOException
- if unable to read from stream.
-
expectSigned
public long expectSigned(int bytes) throws java.io.IOException
Read an signed number from the input stream.- Parameters:
bytes
- Number of bytes to read.- Returns:
- The number read.
- Throws:
java.io.IOException
- if unable to read from stream.
-
readIntZigzag
public int readIntZigzag() throws java.io.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:
java.io.IOException
- if unable to read from stream.
-
readLongZigzag
public long readLongZigzag() throws java.io.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:
java.io.IOException
- if unable to read from stream.
-
readIntVarint
public int readIntVarint() throws java.io.IOException
Write a signed number as varint (integer with variable number of bytes, determined as part of the bytes themselves. NOTE: Reading varint accepts end of stream as '0'.- Returns:
- The varint read from stream.
- Throws:
java.io.IOException
- if unable to read from stream.
-
readLongVarint
public long readLongVarint() throws java.io.IOException
Write a signed number as varint (integer with variable number of bytes, determined as part of the bytes themselves. NOTE: Reading varint accepts end of stream as '0'.- Returns:
- The varint read from stream.
- Throws:
java.io.IOException
- if unable to read from stream.
-
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)
-
-