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 class
Buffer.BufferException
static class
Buffer.PlainBuffer
-
Field Summary
Fields Modifier and Type Field Description protected byte[]
data
static int
DEFAULT_SIZE
The default size for aBuffer
(256 bytes)static int
MAX_SIZE
The maximum valid size of buffer (i.e.static java.math.BigInteger
MAX_UINT64_VALUE
Maximum size of a uint64protected int
rpos
protected int
wpos
-
Method Summary
Modifier and Type Method Description byte[]
array()
int
available()
void
clear()
Resets this buffer.void
compact()
Compact thisSSHPacket
protected void
ensureAvailable(int a)
void
ensureCapacity(int capacity)
byte[]
getCompactData()
protected static int
getNextPowerOf2(int i)
java.lang.String
printHex()
Gives a readable snapshot of the buffer in hex.T
putBoolean(boolean b)
Puts an SSH boolean valueT
putBuffer(Buffer<? extends Buffer<?>> buffer)
Copies the contents of provided buffer into this bufferT
putByte(byte b)
Writes a single byte into this bufferT
putBytes(byte[] b)
Writes Java byte-array as an SSH byte-arrayT
putBytes(byte[] b, int off, int len)
Writes Java byte-array as an SSH byte-arrayT
putMPInt(java.math.BigInteger bi)
T
putPublicKey(java.security.PublicKey key)
T
putRawBytes(byte[] d)
T
putRawBytes(byte[] d, int off, int len)
T
putSensitiveString(char[] str)
Writes a char-array as an SSH string and then blanks it out.T
putSignature(java.lang.String sigFormat, byte[] sigData)
T
putString(byte[] str)
T
putString(byte[] str, int offset, int len)
T
putString(java.lang.String string)
T
putString(java.lang.String string, java.nio.charset.Charset cs)
T
putUInt32(long uint32)
Writes a uint32 integerT
putUInt32FromInt(int uint32)
Writes a uint32 integerT
putUInt64(long uint64)
T
putUInt64(java.math.BigInteger uint64)
boolean
readBoolean()
Read an SSH boolean bytebyte
readByte()
Read a byte from the bufferbyte[]
readBytes()
Read an SSH byte-arrayjava.math.BigInteger
readMPInt()
Read an SSH multiple-precision integerjava.security.PublicKey
readPublicKey()
void
readRawBytes(byte[] buf)
void
readRawBytes(byte[] buf, int off, int len)
java.lang.String
readString()
Reads an SSH string usingUTF8
java.lang.String
readString(java.nio.charset.Charset cs)
Reads an SSH stringbyte[]
readStringAsBytes()
Reads an SSH stringlong
readUInt32()
int
readUInt32AsInt()
long
readUInt64()
java.math.BigInteger
readUInt64AsBigInteger()
int
rpos()
void
rpos(int rpos)
java.lang.String
toString()
int
wpos()
void
wpos(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.BufferException
Read an SSH boolean byte- Returns:
- the
true
orfalse
value 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.BufferException
Read 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.BufferException
Read 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
- theBuffer
to 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.BufferException
Read 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.BufferException
Reads 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.BufferException
Reads an SSH string usingUTF8
- Returns:
- the string as a Java
String
- Throws:
Buffer.BufferException
-
readStringAsBytes
public byte[] readStringAsBytes() throws Buffer.BufferException
Reads 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. Ifstr
isnull
, 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:
toString
in classjava.lang.Object
-
-