Class Buffer

    • Field Detail

      • workBuf

        protected final byte[] workBuf
    • Constructor Detail

      • Buffer

        protected Buffer()
    • Method Detail

      • rpos

        public abstract int rpos()
        Returns:
        Current reading position
      • rpos

        public abstract void rpos​(int rpos)
        Parameters:
        rpos - Set current reading position
      • wpos

        public abstract int wpos()
        Returns:
        Current writing position
      • wpos

        public abstract void wpos​(int wpos)
        Parameters:
        wpos - Set current writing position - Note: if necessary, the underlying data buffer will be increased so as to allow writing from the new position
      • capacity

        public abstract int capacity()
        Returns:
        Number of bytes that can still be written without re-sizing the internal buffer
      • array

        public abstract byte[] array()
        Returns:
        The raw underlying data bytes
      • rawByte

        public byte rawByte​(int pos)
        Parameters:
        pos - A position in the raw underlying data bytes
        Returns:
        The byte at that position
      • compact

        public abstract void compact()
        "Shift" the internal data so that reading starts from position zero.
      • getCompactData

        public byte[] getCompactData()
      • clear

        public void clear()
        Reset read/write positions to zero - Note: zeroes any previously existing data
        See Also:
        clear(boolean)
      • clear

        public abstract void clear​(boolean wipeData)
        Reset read/write positions to zero
        Parameters:
        wipeData - Whether to zero any previously existing data
      • isValidMessageStructure

        public boolean isValidMessageStructure​(Class<?>... fieldTypes)
      • isValidMessageStructure

        public boolean isValidMessageStructure​(Collection<Class<?>> fieldTypes)
      • copyRawBytes

        protected abstract void copyRawBytes​(int offset,
                                             byte[] buf,
                                             int pos,
                                             int len)
      • toHex

        public String toHex()
      • getUByte

        public int getUByte()
      • getByte

        public byte getByte()
      • getShort

        public short getShort()
      • getInt

        public int getInt()
      • getUInt

        public long getUInt()
      • getLong

        public long getLong()
      • getBoolean

        public boolean getBoolean()
      • getString

        public String getString()
        Returns:
        Reads a UTF-8 encoded string
      • getNameList

        public List<String> getNameList()
        According to RFC 4251: A name-list is represented as a uint32 containing its length (number of bytes that follow) followed by a comma-separated list of zero or more names.
        Returns:
        The parsed result
      • getNameList

        public List<String> getNameList​(char separator)
      • getNameList

        public List<String> getNameList​(Charset charset,
                                        char separator)
        Parses a string that contains values separated by a delimiter
        Parameters:
        charset - The Charset to use to read the string
        separator - The separator
        Returns:
        A List of the parsed values
      • getStringList

        public Collection<String> getStringList​(boolean usePrependedLength)
        Parameters:
        usePrependedLength - If true then there is a 32-bit value indicating the number of strings to read. Otherwise, the method will use a "greedy" reading of strings while more data available.
        Returns:
        A Collection of the read strings
        See Also:
        getStringList(boolean, Charset)
      • getStringList

        public Collection<String> getStringList​(boolean usePrependedLength,
                                                Charset charset)
        Parameters:
        usePrependedLength - If true then there is a 32-bit value indicating the number of strings to read. Otherwise, the method will use a "greedy" reading of strings while more data available.
        charset - The Charset to use for the strings
        Returns:
        A Collection of the read strings
        See Also:
        getStringList(int, Charset), getAvailableStrings()
      • getStringList

        public List<String> getStringList​(int count)
        Parameters:
        count - The exact number of strings to read - can be zero
        Returns:
        A List with the specified number of strings
        See Also:
        getStringList(int, Charset)
      • getStringList

        public List<String> getStringList​(int count,
                                          Charset charset)
        Parameters:
        count - The exact number of strings to read - can be zero
        charset - The Charset of the strings
        Returns:
        A List with the specified number of strings
        See Also:
        getString(Charset)
      • getString

        public abstract String getString​(Charset charset)
        Reads a string using a given charset.
        Parameters:
        charset - The Charset to use for the string bytes
        Returns:
        The read string
      • getMPIntAsBytes

        public byte[] getMPIntAsBytes()
      • getBytes

        public byte[] getBytes()
      • getRawBytes

        public void getRawBytes​(byte[] buf)
      • ensureAvailable

        public int ensureAvailable​(int reqLen)
                            throws BufferException
        Makes sure the buffer contains enough data to accommodate the requested length
        Parameters:
        reqLen - Requested data in bytes
        Returns:
        Same as input if validation successful
        Throws:
        BufferException - If negative length or beyond available requested
      • putByte

        public void putByte​(byte b)
      • putOptionalBufferedData

        public void putOptionalBufferedData​(Object buffer)
        Checks if the buffer argument is an array of bytes, a Readable instance or a ByteBuffer and invokes the appropriate putXXX method. If null then puts an empty byte array value
        Parameters:
        buffer - The buffered data object to inspect
        See Also:
        putBufferedData(Object)
      • putBufferedData

        public void putBufferedData​(Object buffer)
        Checks if the buffer argument is an array of bytes, a Readable instance or a ByteBuffer and invokes the appropriate putXXX method.
        Parameters:
        buffer - The (never null) buffer object to put
        Throws:
        IllegalArgumentException - If buffer is none of the supported types
      • putBuffer

        public void putBuffer​(Readable buffer)
      • putBuffer

        public abstract int putBuffer​(Readable buffer,
                                      boolean expand)
      • putBuffer

        public abstract void putBuffer​(ByteBuffer buffer)
      • putShort

        public void putShort​(int i)
        Writes 16 bits
        Parameters:
        i - The 16-bit value
      • putInt

        public void putInt​(long i)
        Writes 32 bits
        Parameters:
        i - The 32-bit value
      • putLong

        public void putLong​(long i)
        Writes 64 bits
        Parameters:
        i - The 64-bit value
      • putBoolean

        public void putBoolean​(boolean b)
      • putAndWipeBytes

        public void putAndWipeBytes​(byte[] b)
        Adds the bytes to the buffer and wipes the data from the input buffer after having added it - useful for sensitive information such as password
        Parameters:
        b - The buffer to add - OK if null
      • putAndWipeBytes

        public void putAndWipeBytes​(byte[] b,
                                    int off,
                                    int len)
      • putBytes

        public void putBytes​(byte[] b)
      • putBytes

        public void putBytes​(byte[] b,
                             int off,
                             int len)
      • putStringList

        public void putStringList​(Collection<?> objects,
                                  boolean prependLength)
        Encodes the toString value of each member.
        Parameters:
        objects - The objects to be encoded in the buffer - OK if null/empty
        prependLength - If true then the list is preceded by a 32-bit count of the number of members in the list
        See Also:
        putStringList(Collection, Charset, boolean)
      • putStringList

        public void putStringList​(Collection<?> objects,
                                  Charset charset,
                                  boolean prependLength)
        Encodes the toString value of each member
        Parameters:
        objects - The objects to be encoded in the buffer - OK if null/empty
        charset - The Charset to use for encoding
        prependLength - If true then the list is preceded by a 32-bit count of the number of members in the list
        See Also:
        putString(String, Charset)
      • putNameList

        public void putNameList​(Collection<String> names)
        According to RFC 4251: A name-list is represented as a uint32 containing its length (number of bytes that follow) followed by a comma-separated list of zero or more names.
        Parameters:
        names - The name list to put
      • putNameList

        public void putNameList​(Collection<String> names,
                                char separator)
      • putNameList

        public void putNameList​(Collection<String> names,
                                Charset charset,
                                char separator)
        Adds a string that contains values separated by a delimiter
        Parameters:
        names - The names to set
        charset - The Charset to use to encode the string
        separator - The separator
      • putString

        public void putString​(String string)
      • putString

        public void putString​(String string,
                              Charset charset)
      • putAndWipeChars

        public void putAndWipeChars​(char[] chars)
        Zeroes the input array after having put the characters in the buffer - useful for sensitive information such as passwords
        Parameters:
        chars - The characters to put in the buffer - may be null/empty
        See Also:
        putAndWipeChars(char[], Charset), putChars(char[], Charset)
      • putAndWipeChars

        public void putAndWipeChars​(char[] chars,
                                    int offset,
                                    int len)
      • putAndWipeChars

        public void putAndWipeChars​(char[] chars,
                                    Charset charset)
      • putAndWipeChars

        public void putAndWipeChars​(char[] chars,
                                    int offset,
                                    int len,
                                    Charset charset)
      • putChars

        public void putChars​(char[] chars)
      • putChars

        public void putChars​(char[] chars,
                             int offset,
                             int len)
      • putChars

        public void putChars​(char[] chars,
                             Charset charset)
      • putChars

        public void putChars​(char[] chars,
                             int offset,
                             int len,
                             Charset charset)
      • putMPInt

        public void putMPInt​(BigInteger bi)
      • putMPInt

        public void putMPInt​(byte[] foo)
      • putRawBytes

        public void putRawBytes​(byte[] d)
      • putRawBytes

        public abstract void putRawBytes​(byte[] d,
                                         int off,
                                         int len)
      • putPublicKey

        public void putPublicKey​(PublicKey key)
      • putRawPublicKey

        public void putRawPublicKey​(PublicKey key)
      • putKeyPair

        public void putKeyPair​(KeyPair kp)
      • ensureCapacity

        protected void ensureCapacity​(int capacity)
      • ensureCapacity

        public abstract void ensureCapacity​(int capacity,
                                            IntUnaryOperator growthFactor)
        Parameters:
        capacity - The required capacity
        growthFactor - An IntUnaryOperator that is invoked if the current capacity is insufficient. The argument is the minimum required new data length, the function result should be the effective new data length to be allocated - if less than minimum then an exception is thrown
      • size

        protected abstract int size()
        Returns:
        Current size of underlying backing data bytes array