Class LittleEndianDataOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- java.io.FilterOutputStream
-
- com.google.common.io.LittleEndianDataOutputStream
-
- All Implemented Interfaces:
Closeable,DataOutput,Flushable,AutoCloseable
@Beta @GwtIncompatible public final class LittleEndianDataOutputStream extends FilterOutputStream implements DataOutput
An implementation ofDataOutputthat uses little-endian byte ordering for writingchar,short,int,float,double, andlongvalues.Note: This class intentionally violates the specification of its supertype
DataOutput, which explicitly requires big-endian byte order.- Since:
- 8.0
- Author:
- Chris Nokleberg, Keith Bottner
-
-
Field Summary
-
Fields inherited from class java.io.FilterOutputStream
out
-
-
Constructor Summary
Constructors Constructor Description LittleEndianDataOutputStream(OutputStream out)Creates aLittleEndianDataOutputStreamthat wraps the given stream.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidclose()Closes this output stream and releases any system resources associated with the stream.voidwrite(byte[] b, int off, int len)Writeslenbytes from the specifiedbytearray starting at offsetoffto this output stream.voidwriteBoolean(boolean v)Writes abooleanvalue to this output stream.voidwriteByte(int v)Writes to the output stream the eight low- order bits of the argumentv.voidwriteBytes(String s)Deprecated.The semantics ofwriteBytes(String s)are considered dangerous.voidwriteChar(int v)Writes a char as specified byDataOutputStream.writeChar(int), except using little-endian byte order.voidwriteChars(String s)Writes aStringas specified byDataOutputStream.writeChars(String), except each character is written using little-endian byte order.voidwriteDouble(double v)Writes adoubleas specified byDataOutputStream.writeDouble(double), except using little-endian byte order.voidwriteFloat(float v)Writes afloatas specified byDataOutputStream.writeFloat(float), except using little-endian byte order.voidwriteInt(int v)Writes anintas specified byDataOutputStream.writeInt(int), except using little-endian byte order.voidwriteLong(long v)Writes alongas specified byDataOutputStream.writeLong(long), except using little-endian byte order.voidwriteShort(int v)Writes ashortas specified byDataOutputStream.writeShort(int), except using little-endian byte order.voidwriteUTF(String str)Writes two bytes of length information to the output stream, followed by the modified UTF-8 representation of every character in the strings.-
Methods inherited from class java.io.FilterOutputStream
flush, write, 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 java.io.DataOutput
write, write
-
-
-
-
Constructor Detail
-
LittleEndianDataOutputStream
public LittleEndianDataOutputStream(OutputStream out)
Creates aLittleEndianDataOutputStreamthat wraps the given stream.- Parameters:
out- the stream to delegate to
-
-
Method Detail
-
write
public void write(byte[] b, int off, int len) throws IOException
Description copied from class:java.io.FilterOutputStreamWriteslenbytes from the specifiedbytearray starting at offsetoffto this output stream.The
writemethod ofFilterOutputStreamcalls thewritemethod of one argument on eachbyteto output.Note that this method does not call the
writemethod of its underlying output stream with the same arguments. Subclasses ofFilterOutputStreamshould provide a more efficient implementation of this method.- Specified by:
writein interfaceDataOutput- Overrides:
writein 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.- See Also:
FilterOutputStream.write(int)
-
writeBoolean
public void writeBoolean(boolean v) throws IOException
Description copied from interface:java.io.DataOutputWrites abooleanvalue to this output stream. If the argumentvistrue, the value(byte)1is written; ifvisfalse, the value(byte)0is written. The byte written by this method may be read by thereadBooleanmethod of interfaceDataInput, which will then return abooleanequal tov.- Specified by:
writeBooleanin interfaceDataOutput- Parameters:
v- the boolean to be written.- Throws:
IOException- if an I/O error occurs.
-
writeByte
public void writeByte(int v) throws IOException
Description copied from interface:java.io.DataOutputWrites to the output stream the eight low- order bits of the argumentv. The 24 high-order bits ofvare ignored. (This means thatwriteBytedoes exactly the same thing aswritefor an integer argument.) The byte written by this method may be read by thereadBytemethod of interfaceDataInput, which will then return abyteequal to(byte)v.- Specified by:
writeBytein interfaceDataOutput- Parameters:
v- the byte value to be written.- Throws:
IOException- if an I/O error occurs.
-
writeBytes
@Deprecated public void writeBytes(String s) throws IOException
Deprecated.The semantics ofwriteBytes(String s)are considered dangerous. Please usewriteUTF(String s),writeChars(String s)or another write method instead.Description copied from interface:java.io.DataOutputWrites a string to the output stream. For every character in the strings, taken in order, one byte is written to the output stream. Ifsisnull, aNullPointerExceptionis thrown.If
s.lengthis zero, then no bytes are written. Otherwise, the characters[0]is written first, thens[1], and so on; the last character written iss[s.length-1]. For each character, one byte is written, the low-order byte, in exactly the manner of thewriteBytemethod . The high-order eight bits of each character in the string are ignored.- Specified by:
writeBytesin interfaceDataOutput- Parameters:
s- the string of bytes to be written.- Throws:
IOException- if an I/O error occurs.
-
writeChar
public void writeChar(int v) throws IOException
Writes a char as specified byDataOutputStream.writeChar(int), except using little-endian byte order.- Specified by:
writeCharin interfaceDataOutput- Parameters:
v- thecharvalue to be written.- Throws:
IOException- if an I/O error occurs
-
writeChars
public void writeChars(String s) throws IOException
Writes aStringas specified byDataOutputStream.writeChars(String), except each character is written using little-endian byte order.- Specified by:
writeCharsin interfaceDataOutput- Parameters:
s- the string value to be written.- Throws:
IOException- if an I/O error occurs
-
writeDouble
public void writeDouble(double v) throws IOException
Writes adoubleas specified byDataOutputStream.writeDouble(double), except using little-endian byte order.- Specified by:
writeDoublein interfaceDataOutput- Parameters:
v- thedoublevalue to be written.- Throws:
IOException- if an I/O error occurs
-
writeFloat
public void writeFloat(float v) throws IOException
Writes afloatas specified byDataOutputStream.writeFloat(float), except using little-endian byte order.- Specified by:
writeFloatin interfaceDataOutput- Parameters:
v- thefloatvalue to be written.- Throws:
IOException- if an I/O error occurs
-
writeInt
public void writeInt(int v) throws IOException
Writes anintas specified byDataOutputStream.writeInt(int), except using little-endian byte order.- Specified by:
writeIntin interfaceDataOutput- Parameters:
v- theintvalue to be written.- Throws:
IOException- if an I/O error occurs
-
writeLong
public void writeLong(long v) throws IOException
Writes alongas specified byDataOutputStream.writeLong(long), except using little-endian byte order.- Specified by:
writeLongin interfaceDataOutput- Parameters:
v- thelongvalue to be written.- Throws:
IOException- if an I/O error occurs
-
writeShort
public void writeShort(int v) throws IOException
Writes ashortas specified byDataOutputStream.writeShort(int), except using little-endian byte order.- Specified by:
writeShortin interfaceDataOutput- Parameters:
v- theshortvalue to be written.- Throws:
IOException- if an I/O error occurs
-
writeUTF
public void writeUTF(String str) throws IOException
Description copied from interface:java.io.DataOutputWrites two bytes of length information to the output stream, followed by the modified UTF-8 representation of every character in the strings. Ifsisnull, aNullPointerExceptionis thrown. Each character in the stringsis converted to a group of one, two, or three bytes, depending on the value of the character.If a character
cis in the range\u0001through\u007f, it is represented by one byte:(byte)c
If a character
cis\u0000or is in the range\u0080through\u07ff, then it is represented by two bytes, to be written in the order shown:(byte)(0xc0 | (0x1f & (c >> 6))) (byte)(0x80 | (0x3f & c))If a character
cis in the range\u0800throughuffff, then it is represented by three bytes, to be written in the order shown:(byte)(0xe0 | (0x0f & (c >> 12))) (byte)(0x80 | (0x3f & (c >> 6))) (byte)(0x80 | (0x3f & c))First, the total number of bytes needed to represent all the characters of
sis calculated. If this number is larger than65535, then aUTFDataFormatExceptionis thrown. Otherwise, this length is written to the output stream in exactly the manner of thewriteShortmethod; after this, the one-, two-, or three-byte representation of each character in the stringsis written.The bytes written by this method may be read by the
readUTFmethod of interfaceDataInput, which will then return aStringequal tos.- Specified by:
writeUTFin interfaceDataOutput- Parameters:
str- the string value to be written.- Throws:
IOException- if an I/O error occurs.
-
close
public void close() throws IOException
Description copied from class:java.io.FilterOutputStreamCloses this output stream and releases any system resources associated with the stream.When not already closed, the
closemethod ofFilterOutputStreamcalls itsflushmethod, and then calls theclosemethod of its underlying output stream.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classFilterOutputStream- Throws:
IOException- if an I/O error occurs.- See Also:
FilterOutputStream.flush(),FilterOutputStream.out
-
-