Package com.sshtools.common.util
Class ByteArrayWriter
java.lang.Object
java.io.OutputStream
java.io.ByteArrayOutputStream
com.sshtools.common.util.ByteArrayWriter
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
Utility class to write common parameter types to a byte array.
- Author:
- Lee David Painter
-
Field Summary
Fields inherited from class java.io.ByteArrayOutputStream
buf, count -
Constructor Summary
ConstructorsConstructorDescriptionContruct an empty writer.ByteArrayWriter(int length) Construct a writer with an array size of the length supplied. -
Method Summary
Modifier and TypeMethodDescriptionbyte[]array()Get the underlying byte arrayvoiddispose()static voidencodeInt(byte[] buf, int off, int i) static byte[]encodeInt(int i) Encode an integer into a 4 byte array.static byte[]encodeInt(long i) static byte[]static byte[]encodeString(String memo) voidmove(int numBytes) Move the position of the next byte to be written.voidstatic byte[]stripLeadingZeros(byte[] data) voidWrite a BigInteger to the array.voidwriteBinaryString(byte[] data) Write a binary string to the array.voidwriteBinaryString(byte[] data, int offset, int len) Write a binary string to the array.voidwriteBoolean(boolean b) Write a boolean value to the array.voidwriteInt(int i) Write an integer to the array.voidwriteInt(long i) Write an integer to the arrayvoidvoidwriteShort(short s) voidwriteString(String str) Write a string to the byte array.voidwriteString(String str, String charset) Write a String to the byte array converting the bytes using the given character set.voidwriteUINT32(UnsignedInteger32 value) voidwriteUINT64(long value) voidwriteUINT64(UnsignedInteger64 value) Methods inherited from class java.io.ByteArrayOutputStream
close, reset, size, toByteArray, toString, toString, toString, toString, write, write, writeBytes, writeToMethods inherited from class java.io.OutputStream
flush, nullOutputStream, write
-
Constructor Details
-
ByteArrayWriter
public ByteArrayWriter()Contruct an empty writer. -
ByteArrayWriter
public ByteArrayWriter(int length) Construct a writer with an array size of the length supplied.- Parameters:
length-
-
-
Method Details
-
array
public byte[] array()Get the underlying byte array- Returns:
- the underlying byte array.
-
move
public void move(int numBytes) Move the position of the next byte to be written.- Parameters:
numBytes-
-
writeBigInteger
Write a BigInteger to the array.- Parameters:
bi-- Throws:
IOException
-
writeBoolean
public void writeBoolean(boolean b) Write a boolean value to the array.- Parameters:
b-- Throws:
IOException
-
writeBinaryString
Write a binary string to the array.- Parameters:
data-- Throws:
IOException
-
writeBinaryString
Write a binary string to the array.- Parameters:
data-offset-len-- Throws:
IOException
-
writeMPINT
-
writeShort
public void writeShort(short s) -
writeInt
Write an integer to the array- Parameters:
i-- Throws:
IOException
-
writeInt
Write an integer to the array.- Parameters:
i-- Throws:
IOException
-
encodeInt
public static byte[] encodeInt(int i) Encode an integer into a 4 byte array.- Parameters:
i-- Returns:
- a byte[4] containing the encoded integer.
-
encodeInt
public static byte[] encodeInt(long i) -
encodeInt
-
encodeInt
public static void encodeInt(byte[] buf, int off, int i) -
writeUINT32
- Throws:
IOException
-
writeUINT64
- Throws:
IOException
-
stripLeadingZeros
public static byte[] stripLeadingZeros(byte[] data) -
writeUINT64
- Throws:
IOException
-
writeString
Write a string to the byte array.- Parameters:
str-- Throws:
IOException
-
writeString
Write a String to the byte array converting the bytes using the given character set.- Parameters:
str-charset-- Throws:
IOException
-
silentClose
public void silentClose() -
dispose
public void dispose() -
encodeString
- Throws:
IOException
-