Package org.pipecraft.infra.io
Class Coding
- java.lang.Object
-
- org.pipecraft.infra.io.Coding
-
public final class Coding extends Object
General encoding/decoding utility functions- Author:
- Roman Gershman, Eyal Schneider
-
-
Constructor Summary
Constructors Constructor Description Coding()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static bytecomputeVarint32Size(int value)Compute the number of bytes that would be needed to encode a varint.static byte[]fromHex(String hex)convert hex to byte arraystatic byte[]getBigEndian16AsBytes(int value)static byte[]getBigEndian32AsBytes(int value)static byte[]getBigEndian64AsBytes(long value)static byte[]getLittleEndian16AsBytes(int value)static byte[]getLittleEndian32AsBytes(int value)static byte[]getLittleEndian64AsBytes(long value)static byteread(InputStream is)Reads a single mandatory byte from a given streamstatic voidread(InputStream is, byte[] buffer, int offset, int bytesCount)Reads a given number of bytes from the stream into a given bufferstatic byte[]read(InputStream is, int bytesCount)Reads a given number of bytes from the stream into a new bufferstatic intreadBigEndian16(byte[] src, int offset)static intreadBigEndian16(InputStream is)Reads a 16 bit int from the input stream.static intreadBigEndian32(byte[] src, int offset)static intreadBigEndian32(InputStream is)Reads a 32 bit int from the input stream.static longreadBigEndian64(byte[] src, int offset)static longreadBigEndian64(InputStream is)Reads a 64 bit int from the input stream.static intreadLittleEndian16(byte[] src, int offset)static intreadLittleEndian16(InputStream is)Reads a 16 bit int from the input stream.static intreadLittleEndian32(byte[] src, int offset)static intreadLittleEndian32(InputStream is)Reads a 32 bit int from the input stream.static longreadLittleEndian64(byte[] src, int offset)static longreadLittleEndian64(InputStream is)Reads a 64 bit int from the input stream.static intreadVarint32(byte[] src, org.apache.commons.lang3.mutable.MutableInt offset)Reads a varint from a given byte arraystatic intreadVarint32(InputStream is, org.apache.commons.lang3.mutable.MutableInt length)Reads a varint from a given input stream.static StringtoHex(byte[] raw)convert byte array to hexstatic booleantryRead(InputStream is, byte[] buffer, int offset, int bytesCount)Tries to read a given number of bytes from the stream into a given buffer.static voidwriteBigEndian16(int value, byte[] dest, int offset)static voidwriteBigEndian16(int v, OutputStream os)Writes a 16 bit int into the output stream.static voidwriteBigEndian32(int value, byte[] dest, int offset)static voidwriteBigEndian32(int v, OutputStream os)Writes a 32 bit int into the output stream.static voidwriteBigEndian64(long value, byte[] dest, int offset)static voidwriteBigEndian64(long v, OutputStream os)Writes a 64 bit int into the output stream.static voidwriteLittleEndian16(int value, byte[] dest, int offset)static voidwriteLittleEndian16(int v, OutputStream os)Writes a 16 bit int into the output stream.static voidwriteLittleEndian32(int value, byte[] dest, int offset)static voidwriteLittleEndian32(int v, OutputStream os)Writes a 32 bit int into the output stream.static voidwriteLittleEndian64(long value, byte[] dest, int offset)static voidwriteLittleEndian64(long v, OutputStream os)Writes a 64 bit int into the output stream.static intwriteVarint32(int value, byte[] dest, int offset)
-
-
-
Method Detail
-
read
public static byte[] read(InputStream is, int bytesCount) throws IOException
Reads a given number of bytes from the stream into a new buffer- Parameters:
is- The input streambytesCount- The number of bytes to read from the stream- Returns:
- The byte array that has been read
- Throws:
IOException- In case of a read errorEOFException- if the stream doesn't contain the requested number of bytes
-
read
public static void read(InputStream is, byte[] buffer, int offset, int bytesCount) throws IOException
Reads a given number of bytes from the stream into a given buffer- Parameters:
is- The input streambuffer- The buffer to read intooffset- The offset in the given buffer to write frombytesCount- The number of bytes to read from the stream- Throws:
IOException- In case of a read errorEOFException- if the stream doesn't contain the requested number of bytes
-
tryRead
public static boolean tryRead(InputStream is, byte[] buffer, int offset, int bytesCount) throws IOException
Tries to read a given number of bytes from the stream into a given buffer. If EOF is encountered, returns false to indicate it. However, if only part of the expected bytes are available, throws EOFException.- Parameters:
is- The input streambuffer- The buffer to read intooffset- The offset in the given buffer to write frombytesCount- The number of bytes to try to read from the stream- Returns:
- true if the buffer has been filled, and false if the input stream has no more bytes to read.
- Throws:
IOException- In case of a read errorEOFException- if the stream isn't in EOF state prior to the call, and doesn't contain requested number of bytes either.
-
read
public static byte read(InputStream is) throws IOException
Reads a single mandatory byte from a given stream- Parameters:
is- The input stream to read from- Returns:
- The read byte
- Throws:
IOException- in case of IO errorEOFException- if the stream doesn't contain any more bytes
-
readLittleEndian16
public static int readLittleEndian16(InputStream is) throws IOException
Reads a 16 bit int from the input stream. LSB are assumed to comes first.- Parameters:
is- The input stream to read from- Returns:
- The 16 bit int read from the input stream
- Throws:
IOException
-
readLittleEndian16
public static int readLittleEndian16(byte[] src, int offset)
-
getLittleEndian16AsBytes
public static byte[] getLittleEndian16AsBytes(int value)
-
writeLittleEndian16
public static void writeLittleEndian16(int v, OutputStream os) throws IOExceptionWrites a 16 bit int into the output stream. LSB comes first.- Parameters:
v- The value to writeos- the output stream to write to- Throws:
IOException
-
writeLittleEndian16
public static void writeLittleEndian16(int value, byte[] dest, int offset)
-
readBigEndian16
public static int readBigEndian16(InputStream is) throws IOException
Reads a 16 bit int from the input stream. MSB are assumed to comes first.- Parameters:
is- The input stream to read from- Returns:
- The 16 bit int read from the input stream
- Throws:
IOException
-
readBigEndian16
public static int readBigEndian16(byte[] src, int offset)
-
getBigEndian16AsBytes
public static byte[] getBigEndian16AsBytes(int value)
-
writeBigEndian16
public static void writeBigEndian16(int v, OutputStream os) throws IOExceptionWrites a 16 bit int into the output stream. MSB comes first.- Parameters:
v- The value to writeos- the output stream to write to- Throws:
IOException
-
writeBigEndian16
public static void writeBigEndian16(int value, byte[] dest, int offset)
-
readLittleEndian32
public static int readLittleEndian32(InputStream is) throws IOException
Reads a 32 bit int from the input stream. LSB are assumed to comes first.- Parameters:
is- The input stream to read from- Returns:
- The 32 bit int read from the input stream
- Throws:
IOException
-
readLittleEndian32
public static int readLittleEndian32(byte[] src, int offset)
-
getLittleEndian32AsBytes
public static byte[] getLittleEndian32AsBytes(int value)
-
writeLittleEndian32
public static void writeLittleEndian32(int v, OutputStream os) throws IOExceptionWrites a 32 bit int into the output stream. LSB comes first.- Parameters:
v- The value to writeos- the output stream to write to- Throws:
IOException
-
writeLittleEndian32
public static void writeLittleEndian32(int value, byte[] dest, int offset)
-
readBigEndian32
public static int readBigEndian32(InputStream is) throws IOException
Reads a 32 bit int from the input stream. MSB are assumed to comes first.- Parameters:
is- The input stream to read from- Returns:
- The 32 bit int read from the input stream
- Throws:
IOException
-
readBigEndian32
public static int readBigEndian32(byte[] src, int offset)
-
getBigEndian32AsBytes
public static byte[] getBigEndian32AsBytes(int value)
-
writeBigEndian32
public static void writeBigEndian32(int v, OutputStream os) throws IOExceptionWrites a 32 bit int into the output stream. MSB comes first.- Parameters:
v- The value to writeos- the output stream to write to- Throws:
IOException
-
writeBigEndian32
public static void writeBigEndian32(int value, byte[] dest, int offset)
-
readLittleEndian64
public static long readLittleEndian64(InputStream is) throws IOException
Reads a 64 bit int from the input stream. LSB are assumed to comes first.- Parameters:
is- The input stream to read from- Returns:
- The 64 bit int read from the input stream
- Throws:
IOException
-
readLittleEndian64
public static long readLittleEndian64(byte[] src, int offset)
-
getLittleEndian64AsBytes
public static byte[] getLittleEndian64AsBytes(long value)
-
writeLittleEndian64
public static void writeLittleEndian64(long v, OutputStream os) throws IOExceptionWrites a 64 bit int into the output stream. LSB comes first.- Parameters:
v- The value to writeos- the output stream to write to- Throws:
IOException
-
writeLittleEndian64
public static void writeLittleEndian64(long value, byte[] dest, int offset)
-
readBigEndian64
public static long readBigEndian64(InputStream is) throws IOException
Reads a 64 bit int from the input stream. MSB are assumed to comes first.- Parameters:
is- The input stream to read from- Returns:
- The 64 bit long read from the input stream
- Throws:
IOException- In case of IO error or if no 8 bytes were found
-
readBigEndian64
public static long readBigEndian64(byte[] src, int offset)
-
getBigEndian64AsBytes
public static byte[] getBigEndian64AsBytes(long value)
-
writeBigEndian64
public static void writeBigEndian64(long v, OutputStream os) throws IOExceptionWrites a 64 bit int into the output stream. MSB comes first.- Parameters:
v- The value to writeos- the output stream to write to- Throws:
IOException
-
writeBigEndian64
public static void writeBigEndian64(long value, byte[] dest, int offset)
-
computeVarint32Size
public static byte computeVarint32Size(int value)
Compute the number of bytes that would be needed to encode a varint.valueis treated as unsigned, so it won't be sign-extended if negative.
-
writeVarint32
public static int writeVarint32(int value, byte[] dest, int offset)- Parameters:
value-dest-offset-- Returns:
- new offset to the dest after the value was written.
- Throws:
IOException
-
readVarint32
public static int readVarint32(InputStream is, org.apache.commons.lang3.mutable.MutableInt length) throws IOException
Reads a varint from a given input stream.- Parameters:
is- The input stream to read fromlength- Output parameter - The number of bytes composing the varint- Returns:
- the varint value
- Throws:
IOException- in case of an illegal varint encoding or IO error while reading from stream
-
readVarint32
public static int readVarint32(byte[] src, org.apache.commons.lang3.mutable.MutableInt offset) throws IOExceptionReads a varint from a given byte array- Parameters:
src- The array to read fromoffset- The offset to read from. This value is updated according to the actual number of bytes read.- Returns:
- the varint value
- Throws:
IOException- in case of an illegal varint encoding
-
fromHex
public static byte[] fromHex(String hex) throws org.apache.commons.codec.DecoderException
convert hex to byte array- Parameters:
hex-- Returns:
- hex as byte array
- Throws:
org.apache.commons.codec.DecoderException
-
toHex
public static String toHex(byte[] raw)
convert byte array to hex- Parameters:
raw-- Returns:
- String
-
-