Package java.io
Class DataOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- java.io.FilterOutputStream
-
- java.io.DataOutputStream
-
- All Implemented Interfaces:
Closeable,DataOutput,Flushable,AutoCloseable
public class DataOutputStream extends FilterOutputStream implements DataOutput
Wraps an existingOutputStreamand writes big-endian typed data to it. Typically, this stream can be read in by DataInputStream. Types that can be written include byte, 16-bit short, 32-bit int, 32-bit float, 64-bit long, 64-bit double, byte strings, andMUTF-8encoded strings.- See Also:
DataInputStream
-
-
Field Summary
Fields Modifier and Type Field Description protected intwrittenThe number of bytes written out so far.-
Fields inherited from class java.io.FilterOutputStream
out
-
-
Constructor Summary
Constructors Constructor Description DataOutputStream(OutputStream out)Constructs a newDataOutputStreamon theOutputStreamout.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidflush()Flushes this stream to ensure all pending data is sent out to the target stream.intsize()Returns the total number of bytes written to the target stream so far.voidwrite(byte[] buffer, int offset, int count)Writescountbytes from the byte arraybufferstarting atoffsetto the target stream.voidwrite(int oneByte)Writes a byte to the target stream.voidwriteBoolean(boolean val)Writes a boolean to the target stream.voidwriteByte(int val)Writes an 8-bit byte to the target stream.voidwriteBytes(String str)Writes the low order 8-bit bytes from the specified string.voidwriteChar(int val)Writes the specified 16-bit character in big-endian order.voidwriteChars(String str)Writes the 16-bit characters contained instrin big-endian order.voidwriteDouble(double val)Writes the specified 64-bit double in big-endian order.voidwriteFloat(float val)Writes the specified 32-bit float in big-endian order.voidwriteInt(int val)Writes the specified 32-bit int in big-endian order.voidwriteLong(long val)Writes the specified 64-bit long in big-endian order.voidwriteShort(int val)Writes the specified 16-bit short in big-endian order.voidwriteUTF(String str)Writes the specified string encoded inmodified UTF-8.-
Methods inherited from class java.io.FilterOutputStream
close
-
Methods inherited from class java.io.OutputStream
write
-
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
-
-
-
-
Constructor Detail
-
DataOutputStream
public DataOutputStream(OutputStream out)
Constructs a newDataOutputStreamon theOutputStreamout. Note that data written by this stream is not in a human readable form but can be reconstructed by using aDataInputStreamon the resulting output.- Parameters:
out- the target stream for writing.
-
-
Method Detail
-
flush
public void flush() throws IOExceptionFlushes this stream to ensure all pending data is sent out to the target stream. This implementation then also flushes the target stream.- Specified by:
flushin interfaceFlushable- Overrides:
flushin classFilterOutputStream- Throws:
IOException- if an error occurs attempting to flush this stream.
-
size
public final int size()
Returns the total number of bytes written to the target stream so far.- Returns:
- the number of bytes written to the target stream.
-
write
public void write(byte[] buffer, int offset, int count) throws IOExceptionWritescountbytes from the byte arraybufferstarting atoffsetto the target stream.- Specified by:
writein interfaceDataOutput- Overrides:
writein classFilterOutputStream- Parameters:
buffer- the buffer to write to the target stream.offset- the index of the first byte inbufferto write.count- the number of bytes from thebufferto write.- Throws:
IOException- if an error occurs while writing to the target stream.NullPointerException- ifbufferisnull.
-
write
public void write(int oneByte) throws IOExceptionWrites a byte to the target stream. Only the least significant byte of the integeroneByteis written.- Specified by:
writein interfaceDataOutput- Overrides:
writein classFilterOutputStream- Parameters:
oneByte- the byte to write to the target stream.- Throws:
IOException- if an error occurs while writing to the target stream.- See Also:
DataInputStream.readByte()
-
writeBoolean
public final void writeBoolean(boolean val) throws IOExceptionWrites a boolean to the target stream.- Specified by:
writeBooleanin interfaceDataOutput- Parameters:
val- the boolean value to write to the target stream.- Throws:
IOException- if an error occurs while writing to the target stream.- See Also:
DataInputStream.readBoolean()
-
writeByte
public final void writeByte(int val) throws IOExceptionWrites an 8-bit byte to the target stream. Only the least significant byte of the integervalis written.- Specified by:
writeBytein interfaceDataOutput- Parameters:
val- the byte value to write to the target stream.- Throws:
IOException- if an error occurs while writing to the target stream.- See Also:
DataInputStream.readByte(),DataInputStream.readUnsignedByte()
-
writeBytes
public final void writeBytes(String str) throws IOException
Description copied from interface:DataOutputWrites the low order 8-bit bytes from the specified string.- Specified by:
writeBytesin interfaceDataOutput- Parameters:
str- the string containing the bytes to write.- Throws:
IOException- if an I/O error occurs while writing.
-
writeChar
public final void writeChar(int val) throws IOExceptionDescription copied from interface:DataOutputWrites the specified 16-bit character in big-endian order. Only the two least significant bytes of the integeroneByteare written.- Specified by:
writeCharin interfaceDataOutput- Parameters:
val- the character to write.- Throws:
IOException- if an I/O error occurs while writing.- See Also:
DataInput.readChar()
-
writeChars
public final void writeChars(String str) throws IOException
Description copied from interface:DataOutputWrites the 16-bit characters contained instrin big-endian order.- Specified by:
writeCharsin interfaceDataOutput- Parameters:
str- the string that contains the characters to write.- Throws:
IOException- if an I/O error occurs while writing.- See Also:
DataInput.readChar()
-
writeDouble
public final void writeDouble(double val) throws IOExceptionDescription copied from interface:DataOutputWrites the specified 64-bit double in big-endian order. The resulting output is the eight bytes returned byDouble.doubleToLongBits(double).- Specified by:
writeDoublein interfaceDataOutput- Parameters:
val- the double to write.- Throws:
IOException- if an I/O error occurs while writing.- See Also:
DataInput.readDouble()
-
writeFloat
public final void writeFloat(float val) throws IOExceptionDescription copied from interface:DataOutputWrites the specified 32-bit float in big-endian order. The resulting output is the four bytes returned byFloat.floatToIntBits(float).- Specified by:
writeFloatin interfaceDataOutput- Parameters:
val- the float to write.- Throws:
IOException- if an I/O error occurs while writing.- See Also:
DataInput.readFloat()
-
writeInt
public final void writeInt(int val) throws IOExceptionDescription copied from interface:DataOutputWrites the specified 32-bit int in big-endian order.- Specified by:
writeIntin interfaceDataOutput- Parameters:
val- the int to write.- Throws:
IOException- if an I/O error occurs while writing.- See Also:
DataInput.readInt()
-
writeLong
public final void writeLong(long val) throws IOExceptionDescription copied from interface:DataOutputWrites the specified 64-bit long in big-endian order.- Specified by:
writeLongin interfaceDataOutput- Parameters:
val- the long to write.- Throws:
IOException- if an I/O error occurs while writing.- See Also:
DataInput.readLong()
-
writeShort
public final void writeShort(int val) throws IOExceptionDescription copied from interface:DataOutputWrites the specified 16-bit short in big-endian order. Only the lower two bytes ofvalare written.- Specified by:
writeShortin interfaceDataOutput- Parameters:
val- the short to write.- Throws:
IOException- if an I/O error occurs while writing.- See Also:
DataInput.readShort(),DataInput.readUnsignedShort()
-
writeUTF
public final void writeUTF(String str) throws IOException
Description copied from interface:DataOutputWrites the specified string encoded inmodified UTF-8.- Specified by:
writeUTFin interfaceDataOutput- Parameters:
str- the string to write encoded inmodified UTF-8.- Throws:
IOException- if an I/O error occurs while writing.- See Also:
DataInput.readUTF()
-
-