Package com.aurumsmods.littlebigio
Class BinaryOutputStream
java.lang.Object
java.io.OutputStream
java.io.FilterOutputStream
com.aurumsmods.littlebigio.BinaryOutputStream
- All Implemented Interfaces:
ByteOrdered
,Closeable
,DataOutput
,Flushable
,AutoCloseable
A binary output stream provides methods to write primitive data types from an
underlying input stream. The order in which the bytes are interpreted can be
either little endian or big endian (see
ByteOrder
).
This implements DataOutput
but intentionally violates the
specifications prescribed by the contract. This is because DataInput
explicitly states the implementation of big endian byte ordering.
BinaryOutputStream
is not necessarily thread-safe due to it using a
buffer to write multiple bytes at once.
-
Field Summary
Modifier and TypeFieldDescriptionprotected int
Number of bytes written to the output stream.Fields inherited from class java.io.FilterOutputStream
out
-
Constructor Summary
ConstructorDescriptionCreates a newBinaryOutputStream
that uses the given underlyingOutputStream
.BinaryOutputStream
(OutputStream out, ByteOrder byteOrder) Creates a newBinaryOutputStream
that uses the given underlyingOutputStream
. -
Method Summary
Modifier and TypeMethodDescriptiongetOrder()
Returns theByteOrder
.protected void
incWritten
(int value) Increases the counter of written bytes by the specified value.void
Sets theByteOrder
to the specified value.final int
size()
Returns the number of bytes written to this stream so far.void
write
(byte[] b, int off, int len) Writeslen
bytes from the specifiedbyte
array starting at offsetoff
to this output stream.void
write
(int b) Writes the specifiedbyte
to this output stream.final void
writeBoolean
(boolean v) Writes aboolean
to the output stream.final void
writeByte
(int v) Writes abyte
value to the output stream.final void
writeBytes
(String s) Writes a string to the output stream as a sequence of bytes.final void
writeChar
(int v) Writes achar
value to the output stream.final void
writeChars
(String s) Writes a string to the output stream as a sequence of characters.final void
writeDouble
(double v) Writes adouble
value to the output stream.final void
writeFloat
(float v) Writes afloat
value to the output stream.final void
writeInt
(int v) Writes aint
value to the output stream.final void
writeLong
(long v) Writes along
value to the output stream.final void
writeShort
(int v) Writes ashort
value to the output stream.final void
Writes a string to the output stream using modified UTF-8 encoding.Methods inherited from class java.io.FilterOutputStream
close, flush, write
Methods inherited from class java.io.OutputStream
nullOutputStream
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.aurumsmods.littlebigio.ByteOrdered
isBigEndian, isLittleEndian, isNativeOrder, swapOrder
Methods inherited from interface java.io.DataOutput
write
-
Field Details
-
written
protected int writtenNumber of bytes written to the output stream. Caps at Integer.MAX_VALUE
-
-
Constructor Details
-
BinaryOutputStream
Creates a newBinaryOutputStream
that uses the given underlyingOutputStream
. The system's native byte order will be used when writing data.- Parameters:
out
- theOutputStream
to write to.- See Also:
-
BinaryOutputStream
Creates a newBinaryOutputStream
that uses the given underlyingOutputStream
. The specified byte order will be used when writing data.- Parameters:
out
- theOutputStream
to write to.byteOrder
- theByteOrder
that specifies how to write data.- Throws:
NullPointerException
- ifbyteOrder
isnull
.
-
-
Method Details
-
getOrder
Returns theByteOrder
.- Specified by:
getOrder
in interfaceByteOrdered
- Returns:
- the
ByteOrder
.
-
setOrder
- Specified by:
setOrder
in interfaceByteOrdered
- Parameters:
byteOrder
- theByteOrder
.- Throws:
NullPointerException
- ifbyteOrder
isnull
.
-
incWritten
protected void incWritten(int value) Increases the counter of written bytes by the specified value. If the counter overflows, the value will be capped at Integer.MAX_VALUE.- Parameters:
value
- the number to increase the counter by.
-
size
public final int size()Returns the number of bytes written to this stream so far. If the value overflows, it will be capped atInteger.MAX_VALUE
.- Returns:
- the number of bytes written to this stream so far.
-
write
Writes the specifiedbyte
to this output stream. This is done by calling thewrite
method of its underlying output stream with the same argument.- Specified by:
write
in interfaceDataOutput
- Overrides:
write
in classFilterOutputStream
- Parameters:
b
- thebyte
.- Throws:
IOException
- if an I/O error occurs.
-
write
Writeslen
bytes from the specifiedbyte
array starting at offsetoff
to this output stream. This is done by calling thewrite
method of its underlying output stream with the same arguments.- Specified by:
write
in interfaceDataOutput
- Overrides:
write
in classFilterOutputStream
- Parameters:
b
- the data.off
- the start offset in the data.len
- the number of bytes to write.- Throws:
IOException
- if an I/O error occurs.
-
writeBoolean
Writes aboolean
to the output stream. The valuetrue
is written out as(byte) 1
; the valuefalse
is written out as(byte) 0
. If no exception is thrown,written
is incremented by 1.- Specified by:
writeBoolean
in interfaceDataOutput
- Parameters:
v
- theboolean
value to be written.- Throws:
IOException
- if an I/O error occurs.
-
writeByte
Writes abyte
value to the output stream. If no exception is thrown,written
is incremented by 1.- Specified by:
writeByte
in interfaceDataOutput
- Parameters:
v
- thebyte
value.- Throws:
IOException
- if an I/O error occurs.
-
writeShort
Writes ashort
value to the output stream. If no exception is thrown,written
is incremented by 2.- Specified by:
writeShort
in interfaceDataOutput
- Parameters:
v
- theshort
value.- Throws:
IOException
- if an I/O error occurs.- See Also:
-
writeChar
Writes achar
value to the output stream. If no exception is thrown,written
is incremented by 2.- Specified by:
writeChar
in interfaceDataOutput
- Parameters:
v
- thechar
value.- Throws:
IOException
- if an I/O error occurs.- See Also:
-
writeInt
Writes aint
value to the output stream. If no exception is thrown,written
is incremented by 4.- Specified by:
writeInt
in interfaceDataOutput
- Parameters:
v
- theint
value.- Throws:
IOException
- if an I/O error occurs.- See Also:
-
writeLong
Writes along
value to the output stream. If no exception is thrown,written
is incremented by 8.- Specified by:
writeLong
in interfaceDataOutput
- Parameters:
v
- thelong
value.- Throws:
IOException
- if an I/O error occurs.- See Also:
-
writeFloat
Writes afloat
value to the output stream. If no exception is thrown,written
is incremented by 4.- Specified by:
writeFloat
in interfaceDataOutput
- Parameters:
v
- thefloat
value.- Throws:
IOException
- if an I/O error occurs.- See Also:
-
writeDouble
Writes adouble
value to the output stream. If no exception is thrown,written
is incremented by 8.- Specified by:
writeDouble
in interfaceDataOutput
- Parameters:
v
- thedouble
value.- Throws:
IOException
- if an I/O error occurs.- See Also:
-
writeBytes
Writes a string to the output stream as a sequence of bytes. For every character in the string the low eight bits are written. If no exception is thrown, the counterwritten
is incremented by the length ofs
.- Specified by:
writeBytes
in interfaceDataOutput
- Parameters:
s
- aString
value to be written.- Throws:
IOException
- if an I/O error occurs.
-
writeChars
Writes a string to the output stream as a sequence of characters. Each character is written to the data output stream usingwriteChar(int)
. If no exception is thrown,written
is incremented by twice the length ofs
.- Specified by:
writeChars
in interfaceDataOutput
- Parameters:
s
- aString
value to be written.- Throws:
IOException
- if an I/O error occurs.
-
writeUTF
Writes a string to the output stream using modified UTF-8 encoding. This callsDataOutputStream.writeUTF(String)
. See the class documentation forDataInput
for details on the modified UTF-8 format. If no exception is thrown,written
is incremented by the amount of encoded bytes written.- Specified by:
writeUTF
in interfaceDataOutput
- Parameters:
s
- theString
value.- Throws:
IOException
- if an I/O error occurs.
-