Package com.twelvemonkeys.io
Class LittleEndianDataOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- java.io.FilterOutputStream
-
- com.twelvemonkeys.io.LittleEndianDataOutputStream
-
- All Implemented Interfaces:
java.io.Closeable
,java.io.DataOutput
,java.io.Flushable
,java.lang.AutoCloseable
public class LittleEndianDataOutputStream extends java.io.FilterOutputStream implements java.io.DataOutput
A little endian output stream writes primitive Java numbers and characters to an output stream in a little endian format.The standard
java.io.DataOutputStream
class which this class imitates uses big endian integers.Warning: The
DataInput
andDataOutput
interfaces specifies big endian byte order in their documentation. This means that this class is, strictly speaking, not a proper implementation. However, I don't see a reason for the these interfaces to specify the byte order of their underlying representations.- Version:
- 1.0.1, 19 May 1999
- Author:
- Elliotte Rusty Harold
- See Also:
LittleEndianRandomAccessFile
,DataOutputStream
,DataInput
,DataOutput
-
-
Field Summary
Fields Modifier and Type Field Description protected int
bytesWritten
The number of bytes written so far to the little endian output stream.
-
Constructor Summary
Constructors Constructor Description LittleEndianDataOutputStream(java.io.OutputStream pStream)
Creates a new little endian output stream and chains it to the output stream specified by thepStream
argument.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
size()
Returns the number of bytes written to this little endian output stream.void
write(byte[] pBytes, int pOffset, int pLength)
WritespLength
bytes from the specified byte array starting atpOffset
to the underlying output stream.void
write(int pByte)
Writes the specified byte value to the underlying output stream.void
writeBoolean(boolean pBoolean)
Writes aboolean
to the underlying output stream as a single byte.void
writeByte(int pByte)
Writes out abyte
to the underlying output streamvoid
writeBytes(java.lang.String pString)
Writes a string to the underlying output stream as a sequence of bytes.void
writeChar(int pChar)
Writes a two bytechar
to the underlying output stream in little endian order, low byte first.void
writeChars(java.lang.String pString)
Writes a string to the underlying output stream as a sequence of characters.void
writeDouble(double d)
Writes an 8 byte Java double to the underlying output stream in little endian order.void
writeFloat(float f)
Writes a 4 byte Java float to the underlying output stream in little endian order.void
writeInt(int pInt)
Writes a four-byteint
to the underlying output stream in little endian order, low byte first, high byte lastvoid
writeLong(long pLong)
Writes an eight-bytelong
to the underlying output stream in little endian order, low byte first, high byte lastvoid
writeShort(int pShort)
Writes a two byteshort
to the underlying output stream in little endian order, low byte first.void
writeUTF(java.lang.String pString)
Writes a string of no more than 65,535 characters to the underlying output stream using UTF-8 encoding.
-
-
-
Constructor Detail
-
LittleEndianDataOutputStream
public LittleEndianDataOutputStream(java.io.OutputStream pStream)
Creates a new little endian output stream and chains it to the output stream specified by thepStream
argument.- Parameters:
pStream
- the underlying output stream.- See Also:
FilterOutputStream.out
-
-
Method Detail
-
write
public void write(int pByte) throws java.io.IOException
Writes the specified byte value to the underlying output stream.- Specified by:
write
in interfacejava.io.DataOutput
- Overrides:
write
in classjava.io.FilterOutputStream
- Parameters:
pByte
- thebyte
value to be written.- Throws:
java.io.IOException
- if the underlying stream throws an IOException.
-
write
public void write(byte[] pBytes, int pOffset, int pLength) throws java.io.IOException
WritespLength
bytes from the specified byte array starting atpOffset
to the underlying output stream.- Specified by:
write
in interfacejava.io.DataOutput
- Overrides:
write
in classjava.io.FilterOutputStream
- Parameters:
pBytes
- the data.pOffset
- the start offset in the data.pLength
- the number of bytes to write.- Throws:
java.io.IOException
- if the underlying stream throws an IOException.
-
writeBoolean
public void writeBoolean(boolean pBoolean) throws java.io.IOException
Writes aboolean
to the underlying output stream as a single byte. If the argument is true, the byte value 1 is written. If the argument is false, the byte value0
in written.- Specified by:
writeBoolean
in interfacejava.io.DataOutput
- Parameters:
pBoolean
- theboolean
value to be written.- Throws:
java.io.IOException
- if the underlying stream throws an IOException.
-
writeByte
public void writeByte(int pByte) throws java.io.IOException
Writes out abyte
to the underlying output stream- Specified by:
writeByte
in interfacejava.io.DataOutput
- Parameters:
pByte
- thebyte
value to be written.- Throws:
java.io.IOException
- if the underlying stream throws an IOException.
-
writeShort
public void writeShort(int pShort) throws java.io.IOException
Writes a two byteshort
to the underlying output stream in little endian order, low byte first.- Specified by:
writeShort
in interfacejava.io.DataOutput
- Parameters:
pShort
- theshort
to be written.- Throws:
java.io.IOException
- if the underlying stream throws an IOException.
-
writeChar
public void writeChar(int pChar) throws java.io.IOException
Writes a two bytechar
to the underlying output stream in little endian order, low byte first.- Specified by:
writeChar
in interfacejava.io.DataOutput
- Parameters:
pChar
- thechar
value to be written.- Throws:
java.io.IOException
- if the underlying stream throws an IOException.
-
writeInt
public void writeInt(int pInt) throws java.io.IOException
Writes a four-byteint
to the underlying output stream in little endian order, low byte first, high byte last- Specified by:
writeInt
in interfacejava.io.DataOutput
- Parameters:
pInt
- theint
to be written.- Throws:
java.io.IOException
- if the underlying stream throws an IOException.
-
writeLong
public void writeLong(long pLong) throws java.io.IOException
Writes an eight-bytelong
to the underlying output stream in little endian order, low byte first, high byte last- Specified by:
writeLong
in interfacejava.io.DataOutput
- Parameters:
pLong
- thelong
to be written.- Throws:
java.io.IOException
- if the underlying stream throws an IOException.
-
writeFloat
public final void writeFloat(float f) throws java.io.IOException
Writes a 4 byte Java float to the underlying output stream in little endian order.- Specified by:
writeFloat
in interfacejava.io.DataOutput
- Parameters:
f
- thefloat
value to be written.- Throws:
java.io.IOException
- if an I/O error occurs.
-
writeDouble
public final void writeDouble(double d) throws java.io.IOException
Writes an 8 byte Java double to the underlying output stream in little endian order.- Specified by:
writeDouble
in interfacejava.io.DataOutput
- Parameters:
d
- thedouble
value to be written.- Throws:
java.io.IOException
- if an I/O error occurs.
-
writeBytes
public void writeBytes(java.lang.String pString) throws java.io.IOException
Writes a string to the underlying output stream as a sequence of bytes. Each character is written to the data output stream as if by thewriteByte(int)
method.- Specified by:
writeBytes
in interfacejava.io.DataOutput
- Parameters:
pString
- theString
value to be written.- Throws:
java.io.IOException
- if the underlying stream throws an IOException.- See Also:
writeByte(int)
,FilterOutputStream.out
-
writeChars
public void writeChars(java.lang.String pString) throws java.io.IOException
Writes a string to the underlying output stream as a sequence of characters. Each character is written to the data output stream as if by thewriteChar
method.- Specified by:
writeChars
in interfacejava.io.DataOutput
- Parameters:
pString
- aString
value to be written.- Throws:
java.io.IOException
- if the underlying stream throws an IOException.- See Also:
writeChar(int)
,FilterOutputStream.out
-
writeUTF
public void writeUTF(java.lang.String pString) throws java.io.IOException
Writes a string of no more than 65,535 characters to the underlying output stream using UTF-8 encoding. This method first writes a two byte short in big endian order as required by the UTF-8 specification. This gives the number of bytes in the UTF-8 encoded version of the string, not the number of characters in the string. Next each character of the string is written using the UTF-8 encoding for the character.- Specified by:
writeUTF
in interfacejava.io.DataOutput
- Parameters:
pString
- the string to be written.- Throws:
java.io.UTFDataFormatException
- if the string is longer than 65,535 characters.java.io.IOException
- if the underlying stream throws an IOException.
-
size
public int size()
Returns the number of bytes written to this little endian output stream. (This class is not thread-safe with respect to this method. It is possible that this number is temporarily less than the actual number of bytes written.)- Returns:
- the value of the
written
field. - See Also:
bytesWritten
-
-