Class ByteArrayReader

java.lang.Object
java.io.InputStream
java.io.ByteArrayInputStream
com.sshtools.common.util.ByteArrayReader
All Implemented Interfaces:
Closeable, AutoCloseable

public class ByteArrayReader extends ByteArrayInputStream

Utiltiy class to read common parameter types from a byte array.

Author:
Lee David Painter
  • 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

      public static void setCharsetEncoding(String charset)
      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

      public static String getCharsetEncoding()
      Get the current encoding being used for Strings variables.
      Returns:
    • readFully

      public void readFully(byte[] b, int off, int len) throws IOException
      Read until the buffer supplied is full.
      Parameters:
      b -
      off -
      len -
      Throws:
      IOException
    • readBoolean

      public boolean readBoolean() throws IOException
      Write a boolean value to the array.
      Parameters:
      b -
      Throws:
      IOException
    • readFully

      public void readFully(byte[] b) throws IOException
      Throws:
      IOException
    • readBigInteger

      public BigInteger readBigInteger() throws IOException
      Read a BigInteger from the array.
      Returns:
      the BigInteger value.
      Throws:
      IOException
    • readUINT64

      public UnsignedInteger64 readUINT64() throws IOException
      Throws:
      IOException
    • readUINT32

      public UnsignedInteger32 readUINT32() throws IOException
      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

      public byte[] readBinaryString() throws IOException
      Read a binary string from the array.
      Returns:
      the byte array.
      Throws:
      IOException
    • readInt

      public long readInt() throws IOException
      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

      public String readString() throws IOException
      Read a String from the array.
      Returns:
      the String value.
      Throws:
      IOException
    • readString

      public String readString(String charset) throws IOException
      Read a String from the array converting using the given character set.
      Parameters:
      charset -
      Returns:
      Throws:
      IOException
    • readShort

      public short readShort() throws IOException
      Throws:
      IOException
    • readMPINT32

      public BigInteger readMPINT32() throws IOException
      Reads an MPINT using the first 32 bits as the length prefix
      Returns:
      Throws:
      IOException
    • readMPINT

      public BigInteger readMPINT() throws IOException
      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:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class ByteArrayInputStream
    • decodeString

      public static String decodeString(byte[] data) throws IOException
      Throws:
      IOException