Package net.schmizz.sshj.common
Class Buffer<T extends Buffer<T>>
- java.lang.Object
-
- net.schmizz.sshj.common.Buffer<T>
-
- Direct Known Subclasses:
Buffer.PlainBuffer,SFTPPacket,SSHPacket
public class Buffer<T extends Buffer<T>> extends java.lang.Object
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classBuffer.BufferExceptionstatic classBuffer.PlainBuffer
-
Field Summary
Fields Modifier and Type Field Description protected byte[]datastatic intDEFAULT_SIZEThe default size for aBuffer(256 bytes)static intMAX_SIZEThe maximum valid size of buffer (i.e.static java.math.BigIntegerMAX_UINT64_VALUEMaximum size of a uint64protected intrposprotected intwpos
-
Method Summary
Modifier and Type Method Description byte[]array()intavailable()voidclear()Resets this buffer.voidcompact()Compact thisSSHPacketprotected voidensureAvailable(int a)voidensureCapacity(int capacity)byte[]getCompactData()protected static intgetNextPowerOf2(int i)java.lang.StringprintHex()Gives a readable snapshot of the buffer in hex.TputBoolean(boolean b)Puts an SSH boolean valueTputBuffer(Buffer<? extends Buffer<?>> buffer)Copies the contents of provided buffer into this bufferTputByte(byte b)Writes a single byte into this bufferTputBytes(byte[] b)Writes Java byte-array as an SSH byte-arrayTputBytes(byte[] b, int off, int len)Writes Java byte-array as an SSH byte-arrayTputMPInt(java.math.BigInteger bi)TputPublicKey(java.security.PublicKey key)TputRawBytes(byte[] d)TputRawBytes(byte[] d, int off, int len)TputSensitiveString(char[] str)Writes a char-array as an SSH string and then blanks it out.TputSignature(java.lang.String sigFormat, byte[] sigData)TputString(byte[] str)TputString(byte[] str, int offset, int len)TputString(java.lang.String string)TputString(java.lang.String string, java.nio.charset.Charset cs)TputUInt32(long uint32)Writes a uint32 integerTputUInt32FromInt(int uint32)Writes a uint32 integerTputUInt64(long uint64)TputUInt64(java.math.BigInteger uint64)booleanreadBoolean()Read an SSH boolean bytebytereadByte()Read a byte from the bufferbyte[]readBytes()Read an SSH byte-arrayjava.math.BigIntegerreadMPInt()Read an SSH multiple-precision integerjava.security.PublicKeyreadPublicKey()voidreadRawBytes(byte[] buf)voidreadRawBytes(byte[] buf, int off, int len)java.lang.StringreadString()Reads an SSH string usingUTF8java.lang.StringreadString(java.nio.charset.Charset cs)Reads an SSH stringbyte[]readStringAsBytes()Reads an SSH stringlongreadUInt32()intreadUInt32AsInt()longreadUInt64()java.math.BigIntegerreadUInt64AsBigInteger()intrpos()voidrpos(int rpos)java.lang.StringtoString()intwpos()voidwpos(int wpos)
-
-
-
Field Detail
-
DEFAULT_SIZE
public static final int DEFAULT_SIZE
The default size for aBuffer(256 bytes)- See Also:
- Constant Field Values
-
MAX_SIZE
public static final int MAX_SIZE
The maximum valid size of buffer (i.e. biggest power of two that can be represented as an int - 2^30)- See Also:
- Constant Field Values
-
MAX_UINT64_VALUE
public static final java.math.BigInteger MAX_UINT64_VALUE
Maximum size of a uint64
-
data
protected byte[] data
-
rpos
protected int rpos
-
wpos
protected int wpos
-
-
Constructor Detail
-
Buffer
public Buffer()
- See Also:
DEFAULT_SIZE
-
Buffer
public Buffer(Buffer<?> from)
-
Buffer
public Buffer(byte[] data)
-
Buffer
public Buffer(int size)
-
-
Method Detail
-
getNextPowerOf2
protected static int getNextPowerOf2(int i)
-
array
public byte[] array()
-
available
public int available()
-
clear
public void clear()
Resets this buffer. The object becomes ready for reuse.
-
rpos
public int rpos()
-
rpos
public void rpos(int rpos)
-
wpos
public int wpos()
-
wpos
public void wpos(int wpos)
-
ensureAvailable
protected void ensureAvailable(int a) throws Buffer.BufferException- Throws:
Buffer.BufferException
-
ensureCapacity
public void ensureCapacity(int capacity)
-
compact
public void compact()
Compact thisSSHPacket
-
getCompactData
public byte[] getCompactData()
-
readBoolean
public boolean readBoolean() throws Buffer.BufferExceptionRead an SSH boolean byte- Returns:
- the
trueorfalsevalue read - Throws:
Buffer.BufferException
-
putBoolean
public T putBoolean(boolean b)
Puts an SSH boolean value- Parameters:
b- the value- Returns:
- this
-
readByte
public byte readByte() throws Buffer.BufferExceptionRead a byte from the buffer- Returns:
- the byte read
- Throws:
Buffer.BufferException
-
putByte
public T putByte(byte b)
Writes a single byte into this buffer- Parameters:
b-- Returns:
- this
-
readBytes
public byte[] readBytes() throws Buffer.BufferExceptionRead an SSH byte-array- Returns:
- the byte-array read
- Throws:
Buffer.BufferException
-
putBytes
public T putBytes(byte[] b)
Writes Java byte-array as an SSH byte-array- Parameters:
b- Java byte-array- Returns:
- this
-
putBytes
public T putBytes(byte[] b, int off, int len)
Writes Java byte-array as an SSH byte-array- Parameters:
b- Java byte-arrayoff- offsetlen- length- Returns:
- this
-
readRawBytes
public void readRawBytes(byte[] buf) throws Buffer.BufferException- Throws:
Buffer.BufferException
-
readRawBytes
public void readRawBytes(byte[] buf, int off, int len) throws Buffer.BufferException- Throws:
Buffer.BufferException
-
putRawBytes
public T putRawBytes(byte[] d)
-
putRawBytes
public T putRawBytes(byte[] d, int off, int len)
-
putBuffer
public T putBuffer(Buffer<? extends Buffer<?>> buffer)
Copies the contents of provided buffer into this buffer- Parameters:
buffer- theBufferto copy- Returns:
- this
-
readUInt32AsInt
public int readUInt32AsInt() throws Buffer.BufferException- Throws:
Buffer.BufferException
-
readUInt32
public long readUInt32() throws Buffer.BufferException- Throws:
Buffer.BufferException
-
putUInt32FromInt
public T putUInt32FromInt(int uint32)
Writes a uint32 integer- Parameters:
uint32-- Returns:
- this
-
putUInt32
public T putUInt32(long uint32)
Writes a uint32 integer- Parameters:
uint32-- Returns:
- this
-
readMPInt
public java.math.BigInteger readMPInt() throws Buffer.BufferExceptionRead an SSH multiple-precision integer- Returns:
- the MP integer as a
BigInteger - Throws:
Buffer.BufferException
-
putMPInt
public T putMPInt(java.math.BigInteger bi)
-
readUInt64
public long readUInt64() throws Buffer.BufferException- Throws:
Buffer.BufferException
-
readUInt64AsBigInteger
public java.math.BigInteger readUInt64AsBigInteger() throws Buffer.BufferException- Throws:
Buffer.BufferException
-
putUInt64
public T putUInt64(long uint64)
-
putUInt64
public T putUInt64(java.math.BigInteger uint64)
-
readString
public java.lang.String readString(java.nio.charset.Charset cs) throws Buffer.BufferExceptionReads an SSH string- Parameters:
cs- the charset to use for decoding- Returns:
- the string as a Java
String - Throws:
Buffer.BufferException
-
readString
public java.lang.String readString() throws Buffer.BufferExceptionReads an SSH string usingUTF8- Returns:
- the string as a Java
String - Throws:
Buffer.BufferException
-
readStringAsBytes
public byte[] readStringAsBytes() throws Buffer.BufferExceptionReads an SSH string- Returns:
- the string as a byte-array
- Throws:
Buffer.BufferException
-
putString
public T putString(byte[] str)
-
putString
public T putString(byte[] str, int offset, int len)
-
putString
public T putString(java.lang.String string, java.nio.charset.Charset cs)
-
putString
public T putString(java.lang.String string)
-
putSensitiveString
public T putSensitiveString(char[] str)
Writes a char-array as an SSH string and then blanks it out. This is useful when a plaintext password needs to be sent. Ifstrisnull, an empty string is written.- Parameters:
str- (null-ok) the string as a character array- Returns:
- this
-
readPublicKey
public java.security.PublicKey readPublicKey() throws Buffer.BufferException- Throws:
Buffer.BufferException
-
putPublicKey
public T putPublicKey(java.security.PublicKey key)
-
putSignature
public T putSignature(java.lang.String sigFormat, byte[] sigData)
-
printHex
public java.lang.String printHex()
Gives a readable snapshot of the buffer in hex. This is useful for debugging.- Returns:
- snapshot of the buffer as a hex string with each octet delimited by a space
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-