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 TypeMethodDescriptionvoid
flush()
Return the inner output stream.void
Set the inner output stream.int
size()
Return the number of bytes written so far.void
write
(byte[] b, int len) Write the specified array of bytes and then pad with zeros to the specified length.void
write
(byte[] b, int off, int len) Write the specified number of bytes from the array starting at the specified offset.void
write
(int b) Write a byte.void
writeBoolean
(boolean v) Write a boolean as a one byte value of 0 or 1.void
writeByte
(int v) Write a single byte to the output stream.void
writeCString
(String str) Write a variable length string as an ascii encoded C string.void
writeCString
(String str, int maxLen) Write a string as an ascii encoded, null terminated C string.void
writeCString
(String str, int maxLen, byte pad) Write a string as an ascii encoded C string.void
writeDouble
(double v) Write a double to the output stream.void
writeFloat
(float v) Write a float to the output stream.void
writeInt
(int v) Writes an int to the output stream.void
writeInt24
(int v) Writes the lower 24 bits of an int to the output stream.void
writeLong
(long v) Writes a long to the output stream.void
writeShort
(int v) Writes a short to the output stream.void
Write a short utf-8 string preceded by an 8 bit length in bytes (not characters).void
writeShortStringWithLen
(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).void
writeStringWithLen
(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, write
Methods inherited from class java.io.OutputStream
nullOutputStream
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods 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:ByteOrderOutput
Write a byte.- Specified by:
write
in interfaceByteOrderOutput
- Overrides:
write
in classFilterOutputStream
- Parameters:
b
- the byte to write- Throws:
IOException
-
write
Description copied from interface:ByteOrderOutput
Write the specified number of bytes from the array starting at the specified offset.- Specified by:
write
in interfaceByteOrderOutput
- Overrides:
write
in classFilterOutputStream
- Parameters:
b
- the data to writeoff
- offset to start fromlen
- number of bytes to write- Throws:
IOException
-
flush
- Specified by:
flush
in interfaceFlushable
- Overrides:
flush
in classFilterOutputStream
- Throws:
IOException
-
writeBoolean
Description copied from interface:ByteOrderOutput
Write a boolean as a one byte value of 0 or 1.- Specified by:
writeBoolean
in interfaceByteOrderOutput
- Parameters:
v
- the value to write- Throws:
IOException
- if there is an underlying error
-
writeByte
Description copied from interface:ByteOrderOutput
Write a single byte to the output stream.- Specified by:
writeByte
in interfaceByteOrderOutput
- Parameters:
v
- the value to write- Throws:
IOException
- if there is an underlying error
-
writeShort
Description copied from interface:ByteOrderOutput
Writes a short to the output stream.- Specified by:
writeShort
in interfaceByteOrderOutput
- Parameters:
v
- the value to write- Throws:
IOException
- if there is an underlying error
-
writeInt
Description copied from interface:ByteOrderOutput
Writes an int to the output stream.- Specified by:
writeInt
in interfaceByteOrderOutput
- Parameters:
v
- the value to write- Throws:
IOException
- if there is an underlying error
-
writeInt24
Description copied from interface:ByteOrderOutput
Writes the lower 24 bits of an int to the output stream.- Specified by:
writeInt24
in interfaceByteOrderOutput
- Parameters:
v
- the value to write- Throws:
IOException
- if there is an underlying error
-
writeLong
Description copied from interface:ByteOrderOutput
Writes a long to the output stream.- Specified by:
writeLong
in interfaceByteOrderOutput
- Parameters:
v
- the value to write- Throws:
IOException
- if there is an underlying error
-
writeFloat
Description copied from interface:ByteOrderOutput
Write a float to the output stream.- Specified by:
writeFloat
in interfaceByteOrderOutput
- Parameters:
v
- the value to write- Throws:
IOException
- if there is an underlying error
-
writeDouble
Description copied from interface:ByteOrderOutput
Write a double to the output stream.- Specified by:
writeDouble
in interfaceByteOrderOutput
- Parameters:
v
- the value to write- Throws:
IOException
- if there is an underlying error
-
writeCString
Description copied from interface:ByteOrderOutput
Write 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:
writeCString
in interfaceByteOrderOutput
- Parameters:
str
- the string to writemaxLen
- the max length of the string- Throws:
IOException
-
writeCString
Description copied from interface:ByteOrderOutput
Write 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:
writeCString
in 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:ByteOrderOutput
Write 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:
writeCString
in interfaceByteOrderOutput
- Parameters:
str
- the string to write- Throws:
IOException
-
writeStringWithLen
Description copied from interface:ByteOrderOutput
Write a utf-8 string preceded by a 16 bit length in bytes (not characters).- Specified by:
writeStringWithLen
in interfaceByteOrderOutput
- Parameters:
str
- the string to write- Throws:
IOException
-
writeShortStringWithLen
Description copied from interface:ByteOrderOutput
Write a short utf-8 string preceded by an 8 bit length in bytes (not characters).- Specified by:
writeShortStringWithLen
in interfaceByteOrderOutput
- Parameters:
str
- the string to write- Throws:
IOException
-
writeShortStringWithLen
Description copied from interface:ByteOrderOutput
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).- Specified by:
writeShortStringWithLen
in interfaceByteOrderOutput
- Parameters:
str
- the string to write- Throws:
IOException
-
write
Description copied from interface:ByteOrderOutput
Write the specified array of bytes and then pad with zeros to the specified length.- Specified by:
write
in 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
-