Package com.tccc.kos.commons.util
Class ByteOrderOutputStream
java.lang.Object
java.io.OutputStream
java.io.FilterOutputStream
com.tccc.kos.commons.util.ByteOrderOutputStream
- All Implemented Interfaces:
ByteOrderOutput,Closeable,Flushable,AutoCloseable
- Direct Known Subclasses:
BinaryMsgOutputStream
An output stream that writes objects in little or big endian format.
- Since:
- 1.0
- Version:
- 2022-08-31
-
Constructor Summary
ConstructorsConstructorDescriptionByteOrderOutputStream(ByteOrder endian, OutputStream out) Creates a new wrapper around the specified output stream. -
Method Summary
Modifier and TypeMethodDescriptionvoidflush()Return the inner output stream.voidSet the inner output stream.intsize()Return the number of bytes written so far.voidwrite(byte[] b, int len) Write the specified array of bytes and then pad with zeros to the specified length.voidwrite(byte[] b, int off, int len) Write the specified number of bytes from the array starting at the specified offset.voidwrite(int b) Write a byte.voidwriteBoolean(boolean v) Write a boolean as a one byte value of 0 or 1.voidwriteByte(int v) Write a single byte to the output stream.voidwriteCString(String str) Write a variable length string as an ascii encoded C string.voidwriteCString(String str, int maxLen) Write a string as an ascii encoded, null terminated C string.voidwriteCString(String str, int maxLen, byte pad) Write a string as an ascii encoded C string.voidwriteDouble(double v) Write a double to the output stream.voidwriteFloat(float v) Write a float to the output stream.voidwriteInt(int v) Writes an int to the output stream.voidwriteInt24(int v) Writes the lower 24 bits of an int to the output stream.voidwriteLong(long v) Writes a long to the output stream.voidwriteShort(int v) Writes a short to the output stream.voidWrite a short utf-8 string preceded by an 8 bit length in bytes (not characters).voidwriteShortStringWithLen(String str, int maxChars) Write a short utf-8 string preceded by an 8 bit length in bytes (not characters), limited to the specified number of characters (not bytes).voidwriteStringWithLen(String str) Write a utf-8 string preceded by a 16 bit length in bytes (not characters).Methods inherited from class java.io.FilterOutputStream
close, writeMethods inherited from class java.io.OutputStream
nullOutputStreamMethods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.tccc.kos.commons.util.ByteOrderOutput
write
-
Constructor Details
-
ByteOrderOutputStream
Creates a new wrapper around the specified output stream.- Parameters:
endian- the endian of the streamout- the output stream to wrap
-
-
Method Details
-
getInnerStream
Return the inner output stream.- Returns:
- the inner output stream this stream wraps
-
setInnerStream
Set the inner output stream. Useful if the output stream wasn't known at the time the constructor was called.- Parameters:
out- the output stream
-
write
Description copied from interface:ByteOrderOutputWrite a byte.- Specified by:
writein interfaceByteOrderOutput- Overrides:
writein classFilterOutputStream- Parameters:
b- the byte to write- Throws:
IOException
-
write
Description copied from interface:ByteOrderOutputWrite the specified number of bytes from the array starting at the specified offset.- Specified by:
writein interfaceByteOrderOutput- Overrides:
writein classFilterOutputStream- Parameters:
b- the data to writeoff- offset to start fromlen- number of bytes to write- Throws:
IOException
-
flush
- Specified by:
flushin interfaceFlushable- Overrides:
flushin classFilterOutputStream- Throws:
IOException
-
writeBoolean
Description copied from interface:ByteOrderOutputWrite a boolean as a one byte value of 0 or 1.- Specified by:
writeBooleanin interfaceByteOrderOutput- Parameters:
v- the value to write- Throws:
IOException- if there is an underlying error
-
writeByte
Description copied from interface:ByteOrderOutputWrite a single byte to the output stream.- Specified by:
writeBytein interfaceByteOrderOutput- Parameters:
v- the value to write- Throws:
IOException- if there is an underlying error
-
writeShort
Description copied from interface:ByteOrderOutputWrites a short to the output stream.- Specified by:
writeShortin interfaceByteOrderOutput- Parameters:
v- the value to write- Throws:
IOException- if there is an underlying error
-
writeInt
Description copied from interface:ByteOrderOutputWrites an int to the output stream.- Specified by:
writeIntin interfaceByteOrderOutput- Parameters:
v- the value to write- Throws:
IOException- if there is an underlying error
-
writeInt24
Description copied from interface:ByteOrderOutputWrites the lower 24 bits of an int to the output stream.- Specified by:
writeInt24in interfaceByteOrderOutput- Parameters:
v- the value to write- Throws:
IOException- if there is an underlying error
-
writeLong
Description copied from interface:ByteOrderOutputWrites a long to the output stream.- Specified by:
writeLongin interfaceByteOrderOutput- Parameters:
v- the value to write- Throws:
IOException- if there is an underlying error
-
writeFloat
Description copied from interface:ByteOrderOutputWrite a float to the output stream.- Specified by:
writeFloatin interfaceByteOrderOutput- Parameters:
v- the value to write- Throws:
IOException- if there is an underlying error
-
writeDouble
Description copied from interface:ByteOrderOutputWrite a double to the output stream.- Specified by:
writeDoublein interfaceByteOrderOutput- Parameters:
v- the value to write- Throws:
IOException- if there is an underlying error
-
writeCString
Description copied from interface:ByteOrderOutputWrite a string as an ascii encoded, null terminated C string. If the string is longer than maxLen then an exception will be thrown. If the string is shorter than the string, the difference will be zero padded. This makes it easy to write strings to fixed size buffers on the native side of things.- Specified by:
writeCStringin interfaceByteOrderOutput- Parameters:
str- the string to writemaxLen- the max length of the string- Throws:
IOException
-
writeCString
Description copied from interface:ByteOrderOutputWrite a string as an ascii encoded C string. The string is NOT null terminated. If the string is longer than maxLen then an exception will be thrown. If the string is shorter than the string, the difference will be padded with desired padding byte. This makes it easy to write strings to fixed size buffers on the native side of things.- Specified by:
writeCStringin interfaceByteOrderOutput- Parameters:
str- the string to writemaxLen- the max length of the stringpad- the padding byte to use- Throws:
IOException
-
writeCString
Description copied from interface:ByteOrderOutputWrite a variable length string as an ascii encoded C string. The string is null terminated. It is up to the caller to make sure the length is not a problem.- Specified by:
writeCStringin interfaceByteOrderOutput- Parameters:
str- the string to write- Throws:
IOException
-
writeStringWithLen
Description copied from interface:ByteOrderOutputWrite a utf-8 string preceded by a 16 bit length in bytes (not characters).- Specified by:
writeStringWithLenin interfaceByteOrderOutput- Parameters:
str- the string to write- Throws:
IOException
-
writeShortStringWithLen
Description copied from interface:ByteOrderOutputWrite a short utf-8 string preceded by an 8 bit length in bytes (not characters).- Specified by:
writeShortStringWithLenin interfaceByteOrderOutput- Parameters:
str- the string to write- Throws:
IOException
-
writeShortStringWithLen
Description copied from interface:ByteOrderOutputWrite a short utf-8 string preceded by an 8 bit length in bytes (not characters), limited to the specified number of characters (not bytes).- Specified by:
writeShortStringWithLenin interfaceByteOrderOutput- Parameters:
str- the string to write- Throws:
IOException
-
write
Description copied from interface:ByteOrderOutputWrite the specified array of bytes and then pad with zeros to the specified length.- Specified by:
writein interfaceByteOrderOutput- Parameters:
b- the bytes to writelen- the length to pad to- Throws:
IOException
-
size
public int size()Return the number of bytes written so far.- Returns:
- the number of bytes written so far
-