Package com.sshtools.common.util
Class ByteArrayReader
java.lang.Object
java.io.InputStream
java.io.ByteArrayInputStream
com.sshtools.common.util.ByteArrayReader
- All Implemented Interfaces:
Closeable,AutoCloseable
Utiltiy class to read common parameter types from a byte array.
- Author:
- Lee David Painter
-
Field Summary
FieldsFields inherited from class java.io.ByteArrayInputStream
buf, count, mark, pos -
Constructor Summary
ConstructorsConstructorDescriptionByteArrayReader(byte[] buffer) ByteArrayReader(byte[] buffer, int start, int len) Construct a reader. -
Method Summary
Modifier and TypeMethodDescriptionbyte[]array()Provides access to the underlying arrayvoidclose()static StringdecodeString(byte[] data) voiddispose()static StringGet the current encoding being used for Strings variables.intGet the current position within the array.Read a BigInteger from the array.byte[]Read a binary string from the array.booleanWrite a boolean value to the array.voidreadFully(byte[] b) voidreadFully(byte[] b, int off, int len) Read until the buffer supplied is full.longreadInt()Read an integer (4 bytes) from the array.static longreadInt(byte[] data, int start) Read an integer (4 bytes) from the array.Reads a standard SSH1 MPINT using the first 16 bits as the length prefixReads an MPINT using the first 32 bits as the length prefixshortstatic shortreadShort(byte[] data, int start) Read a String from the array.readString(String charset) Read a String from the array converting using the given character set.static voidsetCharsetEncoding(String charset) Allows the default encoding to be overriden for String variables processed by the class.voidMethods inherited from class java.io.ByteArrayInputStream
available, mark, markSupported, read, read, readAllBytes, readNBytes, reset, skip, transferToMethods inherited from class java.io.InputStream
nullInputStream, read, readNBytes, skipNBytes
-
Field Details
-
encode
public static boolean encode
-
-
Constructor Details
-
ByteArrayReader
public ByteArrayReader(byte[] buffer, int start, int len) Construct a reader.- Parameters:
buffer-start-len-
-
ByteArrayReader
public ByteArrayReader(byte[] buffer)
-
-
Method Details
-
array
public byte[] array()Provides access to the underlying array- Returns:
- byte[]
-
setCharsetEncoding
Allows the default encoding to be overriden for String variables processed by the class. This currently defaults to UTF-8.- Parameters:
charset-- Throws:
UnsupportedEncodingException
-
getCharsetEncoding
Get the current encoding being used for Strings variables.- Returns:
-
readFully
Read until the buffer supplied is full.- Parameters:
b-off-len-- Throws:
IOException
-
readBoolean
Write a boolean value to the array.- Parameters:
b-- Throws:
IOException
-
readFully
- Throws:
IOException
-
readBigInteger
Read a BigInteger from the array.- Returns:
- the BigInteger value.
- Throws:
IOException
-
readUINT64
- Throws:
IOException
-
readUINT32
- Throws:
IOException
-
readInt
public static long readInt(byte[] data, int start) Read an integer (4 bytes) from the array. This is returned as a long as we deal with unsigned ints so the value may be higher than the standard java int.- Parameters:
data-start-- Returns:
- the value represent by a long.
-
readShort
public static short readShort(byte[] data, int start) -
readBinaryString
Read a binary string from the array.- Returns:
- the byte array.
- Throws:
IOException
-
readInt
Read an integer (4 bytes) from the array. This is returned as a long as we deal with unsigned ints so the value may be higher than the standard java int.- Returns:
- the integer value as a long.
- Throws:
IOException
-
readString
Read a String from the array.- Returns:
- the String value.
- Throws:
IOException
-
readString
Read a String from the array converting using the given character set.- Parameters:
charset-- Returns:
- Throws:
IOException
-
readShort
- Throws:
IOException
-
readMPINT32
Reads an MPINT using the first 32 bits as the length prefix- Returns:
- Throws:
IOException
-
readMPINT
Reads a standard SSH1 MPINT using the first 16 bits as the length prefix- Returns:
- Throws:
IOException
-
getPosition
public int getPosition()Get the current position within the array.- Returns:
- the current position within the array
-
silentClose
public void silentClose() -
dispose
public void dispose() -
close
public void close()- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classByteArrayInputStream
-
decodeString
- Throws:
IOException
-