Class Buffer
java.lang.Object
org.apache.sshd.common.util.buffer.Buffer
- All Implemented Interfaces:
Readable
- Direct Known Subclasses:
ByteArrayBuffer
Provides an abstract message buffer for encoding SSH messages
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract byte[]array()abstract intcapacity()clear()Reset read/write positions to zero - Note: zeroes any previously existing dataabstract Bufferclear(boolean wipeData) Reset read/write positions to zeroabstract voidcompact()"Shift" the internal data so that reading starts from position zero.protected abstract voidcopyRawBytes(int offset, byte[] buf, int pos, int len) voiddumpHex(SimplifiedLog logger, String prefix, PropertyResolver resolver) voiddumpHex(SimplifiedLog logger, Level level, String prefix, PropertyResolver resolver) intensureAvailable(int reqLen) Makes sure the buffer contains enough data to accommodate the requested lengthensureCapacity(int capacity) abstract BufferensureCapacity(int capacity, IntUnaryOperator growthFactor) protected KeyPairextractEC(String expectedCurveName, ECParameterSpec spec) getAvailableStrings(Charset charset) booleanbytegetByte()byte[]getBytes()abstract byte[]getCertificateOptions(Charset charset) According to PROTOCOL.certkeys: Critical Options is a set of bytes that is [overall length][name(string)][[length of buffer][[length of string][data(string)]]]...byte[]intgetInt()longgetLong()getMPInt()byte[]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.getNameList(char separator) getNameList(Charset charset) getNameList(Charset charset, char separator) Parses a string that contains values separated by a delimitergetPublicKey(BufferPublicKeyParser<? extends PublicKey> parser) voidgetRawBytes(byte[] buf) getRawPublicKey(BufferPublicKeyParser<? extends PublicKey> parser) shortgetShort()abstract StringReads a string using a given charset.getStringList(boolean usePrependedLength) getStringList(boolean usePrependedLength, Charset charset) getStringList(int count) getStringList(int count, Charset charset) intgetUByte()longgetUInt()intprivate static booleanisSensitiveData(int cmd) booleanisValidMessageStructure(Class<?>... fieldTypes) booleanisValidMessageStructure(Collection<Class<?>> fieldTypes) voidputAndWipeBytes(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 passwordvoidputAndWipeBytes(byte[] b, int off, int len) voidputAndWipeChars(char[] chars) Zeroes the input array after having put the characters in the buffer - useful for sensitive information such as passwordsvoidputAndWipeChars(char[] chars, int offset, int len) voidputAndWipeChars(char[] chars, int offset, int len, Charset charset) voidputAndWipeChars(char[] chars, Charset charset) voidputBoolean(boolean b) abstract voidputBuffer(ByteBuffer buffer) voidabstract intvoidputBufferedData(Object buffer) Checks if the buffer argument is an array of bytes, aReadableinstance or aByteBufferand invokes the appropriateputXXXmethod.voidputByte(byte b) voidputBytes(byte[] b) voidputBytes(byte[] b, int off, int len) voidvoidputCertificateOptions(List<OpenSshCertificate.CertificateOption> options, Charset charset) According to PROTOCOL.certkeys: Certificate Options is a set of bytes that is [overall length][name(string)][[length of buffer][[length of string][data(string)]]]...voidputChars(char[] chars) voidputChars(char[] chars, int offset, int len) voidvoidvoidputInt(long i) voidputKeyPair(KeyPair kp) voidputLong(long i) Writes 64 bitsvoidputMPInt(byte[] mpInt) voidputMPInt(BigInteger bigint) voidputNameList(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.voidputNameList(Collection<String> names, char separator) voidputNameList(Collection<String> names, Charset charset) voidputNameList(Collection<String> names, Charset charset, char separator) Adds a string that contains values separated by a delimitervoidputOptionalBufferedData(Object buffer) Checks if the buffer argument is an array of bytes, aReadableinstance or aByteBufferand invokes the appropriateputXXXmethod.voidputPublicKey(PublicKey key) voidputRawBytes(byte[] d) abstract voidputRawBytes(byte[] d, int off, int len) voidputRawPublicKey(PublicKey key) voidvoidputShort(int i) Writes 16 bitsvoidvoidvoidputStringList(Collection<?> objects, boolean prependLength) Encodes thetoStringvalue of each member.voidputStringList(Collection<?> objects, Charset charset, boolean prependLength) Encodes thetoStringvalue of each membervoidputUInt(long i) Writes 32 bitsbyterawByte(int pos) longrawUInt(int pos) abstract intrpos()abstract voidrpos(int rpos) protected abstract intsize()toHex()toString()abstract intwpos()abstract voidwpos(int wpos) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.apache.sshd.common.util.Readable
available, getRawBytes
-
Field Details
-
workBuf
protected final byte[] workBuf
-
-
Constructor Details
-
Buffer
protected Buffer()
-
-
Method Details
-
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
-
getBytesConsumed
public abstract byte[] getBytesConsumed()- Returns:
- The bytes consumed so far
-
rawByte
public byte rawByte(int pos) - Parameters:
pos- A position in the raw underlying data bytes- Returns:
- The byte at the specified position without changing the current
read position. Note: no validation is made whether the position lies within array boundaries
-
rawUInt
public long rawUInt(int pos) - Parameters:
pos- A position in the raw underlying data bytes- Returns:
- The unsigned 32 bit integer at the specified position without changing the current
read position. Note: no validation is made whether the position and the required extra 4 bytes lie within array boundaries
-
compact
public abstract void compact()"Shift" the internal data so that reading starts from position zero. -
getCompactData
public byte[] getCompactData() -
clear
Reset read/write positions to zero - Note: zeroes any previously existing data- Returns:
- Reference to this buffer
- See Also:
-
clear
Reset read/write positions to zero- Parameters:
wipeData- Whether to zero any previously existing data- Returns:
- Reference to this buffer
-
isValidMessageStructure
-
isValidMessageStructure
-
copyRawBytes
protected abstract void copyRawBytes(int offset, byte[] buf, int pos, int len) -
toHex
-
dumpHex
-
dumpHex
-
isSensitiveData
private static boolean isSensitiveData(int cmd) -
getUByte
public int getUByte() -
getByte
public byte getByte() -
getShort
public short getShort() -
getUShort
public int getUShort() -
getInt
public int getInt() -
getUInt
public long getUInt() -
getLong
public long getLong() -
getBoolean
public boolean getBoolean() -
getString
- Returns:
- Reads a UTF-8 encoded 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
-
getNameList
-
getNameList
Parses a string that contains values separated by a delimiter -
getCertificateOptions
-
getCertificateOptions
According to PROTOCOL.certkeys: Critical Options is a set of bytes that is [overall length][name(string)][[length of buffer][[length of string][data(string)]]]... Where each Certificate Option is encoded as a name (string) and buffer of data (string packed in a buffer) Then the entire name (string) + data (buffer) are added as bytes (which will get a length prefix)- Parameters:
charset-Charsetto use for converting bytes to characters- Returns:
- the parsed result, never
null, but possibly empty
-
getStringList
- Parameters:
usePrependedLength- Iftruethen 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
Collectionof the read strings - See Also:
-
getStringList
- Parameters:
usePrependedLength- Iftruethen 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- TheCharsetto use for the strings- Returns:
- A
Collectionof the read strings - See Also:
-
getAvailableStrings
- Returns:
- The remaining data as a list of strings
- See Also:
-
getAvailableStrings
- Parameters:
charset- TheCharsetto use for the strings- Returns:
- The remaining data as a list of strings
- See Also:
-
getStringList
- Parameters:
count- The exact number of strings to read - can be zero- Returns:
- A
Listwith the specified number of strings - See Also:
-
getStringList
-
getString
Reads a string using a given charset.- Parameters:
charset- TheCharsetto use for the string bytes- Returns:
- The read string
-
getMPInt
-
getMPIntAsBytes
public byte[] getMPIntAsBytes() -
getBytes
public byte[] getBytes() -
getRawBytes
public void getRawBytes(byte[] buf) -
getPublicKey
- Throws:
SshException
-
getPublicKey
public PublicKey getPublicKey(BufferPublicKeyParser<? extends PublicKey> parser) throws SshException - Parameters:
parser- ABufferPublicKeyParserto extract the key from the buffer - nevernull- Returns:
- The extracted
PublicKey- may benullif the parser so decided - Throws:
SshException- If failed to extract the key- See Also:
-
getRawPublicKey
- Throws:
SshException
-
getRawPublicKey
public PublicKey getRawPublicKey(BufferPublicKeyParser<? extends PublicKey> parser) throws SshException - Parameters:
parser- ABufferPublicKeyParserto extract the key from the buffer - nevernull- Returns:
- The extracted
PublicKey- may benullif the parser so decided - Throws:
SshException- If failed to extract the key
-
getKeyPair
- Throws:
SshException
-
extractEC
protected KeyPair extractEC(String expectedCurveName, ECParameterSpec spec) throws GeneralSecurityException - Throws:
GeneralSecurityException
-
ensureAvailable
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
Checks if the buffer argument is an array of bytes, aReadableinstance or aByteBufferand invokes the appropriateputXXXmethod. Ifnullthen puts an empty byte array value- Parameters:
buffer- The buffered data object to inspect- See Also:
-
putBufferedData
Checks if the buffer argument is an array of bytes, aReadableinstance or aByteBufferand invokes the appropriateputXXXmethod.- Parameters:
buffer- The (nevernull) buffer object to put- Throws:
IllegalArgumentException- If buffer is none of the supported types
-
putBuffer
-
putBuffer
-
putBuffer
-
putShort
public void putShort(int i) Writes 16 bits- Parameters:
i- The 16-bit value
-
putInt
public void putInt(long i) -
putUInt
public void putUInt(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 ifnull
-
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
Encodes thetoStringvalue of each member.- Parameters:
objects- The objects to be encoded in the buffer - OK ifnull/emptyprependLength- Iftruethen the list is preceded by a 32-bit count of the number of members in the list- See Also:
-
putStringList
Encodes thetoStringvalue of each member- Parameters:
objects- The objects to be encoded in the buffer - OK ifnull/emptycharset- TheCharsetto use for encodingprependLength- Iftruethen the list is preceded by a 32-bit count of the number of members in the list- See Also:
-
putCertificateOptions
-
putCertificateOptions
public void putCertificateOptions(List<OpenSshCertificate.CertificateOption> options, Charset charset) According to PROTOCOL.certkeys: Certificate Options is a set of bytes that is [overall length][name(string)][[length of buffer][[length of string][data(string)]]]... Where each Certificate Option is encoded as a name (string) and data (string packed in a buffer) Then the entire name (string) + data (buffer) are added as bytes (which will get a length prefix)- Parameters:
options- to write into the buffer, may benullor empty but must not containnullelementscharset- TheCharsetto use for string options
-
putNameList
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
-
putNameList
-
putNameList
Adds a string that contains values separated by a delimiter- Parameters:
names- The names to setcharset- TheCharsetto use to encode the stringseparator- The separator
-
putString
-
putString
-
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 benull/empty- See Also:
-
putAndWipeChars
public void putAndWipeChars(char[] chars, int offset, int len) -
putAndWipeChars
-
putAndWipeChars
-
putChars
public void putChars(char[] chars) -
putChars
public void putChars(char[] chars, int offset, int len) -
putChars
-
putChars
-
putMPInt
-
putMPInt
public void putMPInt(byte[] mpInt) -
putRawBytes
public void putRawBytes(byte[] d) -
putRawBytes
public abstract void putRawBytes(byte[] d, int off, int len) -
putPublicKey
-
putRawPublicKey
-
putRawPublicKeyBytes
-
putKeyPair
-
ensureCapacity
-
ensureCapacity
- Parameters:
capacity- The required capacitygrowthFactor- AnIntUnaryOperatorthat 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- Returns:
- This buffer instance
-
size
protected abstract int size()- Returns:
- Current size of underlying backing data bytes array
-
toString
-