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 existing
OutputStream and 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, and MUTF-8 encoded 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
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
closeMethods inherited from class java.io.OutputStream
writeMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.io.DataOutput
write
-
Field Details
-
written
protected int writtenThe number of bytes written out so far.
-
-
Constructor Details
-
DataOutputStream
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 Details
-
flush
Flushes 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
Writescountbytes 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
Writes 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
Writes 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
Writes 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
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
Description 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
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
Description 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
Description 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
Description 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
Description 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
Description 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
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()
-