Top Description Fields Constructors Methods
java.io

public Class DataOutputStream

extends FilterOutputStream
implements DataOutput
Class Inheritance
All Implemented Interfaces
java.io.DataOutput
Imports
jdk.internal.access.JavaLangAccess, .SharedSecrets, jdk.internal.util.ByteArray

A data output stream lets an application write primitive Java data types to an output stream in a portable way. An application can then use a data input stream to read the data back in. A data output stream wraps another output stream and delegates writing bytes to the write methods of that output stream. Writing data consisting of more than a single byte may cause several writes to the underlying output stream.

A DataOutputStream is not safe for use by multiple concurrent threads. If a DataOutputStream is to be used by more than one thread then access to the data output stream should be controlled by appropriate synchronization.

Since
1.0
See Also
java.io.DataInputStream

Field Summary

Modifier and TypeField and Description
private byte[]
bytearr

bytearr is initialized on demand by writeUTF

private static final JavaLangAccess
private final byte[]
protected int
written

The number of bytes written to the data output stream so far.

Inherited from java.io.FilterOutputStream:
out

Constructor Summary

AccessConstructor and Description
public
DataOutputStream(OutputStream
the underlying output stream, to be saved for later use.
out
)

Creates a new data output stream to write data to the specified underlying output stream.

Method Summary

Modifier and TypeMethod and Description
public void
flush()

Overrides java.io.FilterOutputStream.flush.

Implements java.io.Flushable.flush.

Flushes this data output stream.

private void
incCount(int value)

Increases the written counter by the specified value until it reaches Integer.MAX_VALUE.

public final int

Returns:

the value of the written field.
size
()

Returns the current value of the counter written, the number of bytes written to this data output stream so far.

private static String
tooLongMsg(String s, int bits32)

public synchronized void
write(int
the byte to be written.
b
)

Overrides java.io.FilterOutputStream.write.

Implements java.io.DataOutput.write.

Writes the specified byte (the low eight bits of the argument b) to the underlying output stream.

public synchronized void
write(byte[]
the data.
b
,
int
the start offset in the data.
off
,
int
the number of bytes to write.
len
)

Overrides java.io.FilterOutputStream.write.

Implements java.io.DataOutput.write.

Writes len bytes from the specified byte array starting at offset off to the underlying output stream.

public final void
writeBoolean(boolean
a boolean value to be written.
v
)

Implements java.io.DataOutput.writeBoolean.

Writes a boolean to the underlying output stream as a 1-byte value.

public final void
writeByte(int
a byte value to be written.
v
)

Implements java.io.DataOutput.writeByte.

Writes out a byte to the underlying output stream as a 1-byte value.

public final void
writeBytes(String
a string of bytes to be written.
s
)

Implements java.io.DataOutput.writeBytes.

Writes out the string to the underlying output stream as a sequence of bytes.

public final void
writeChar(int
a char value to be written.
v
)

Implements java.io.DataOutput.writeChar.

Writes a char to the underlying output stream as a 2-byte value, high byte first.

public final void
writeChars(String
a String value to be written.
s
)

Implements java.io.DataOutput.writeChars.

Writes a string to the underlying output stream as a sequence of characters.

public final void
writeDouble(double
a double value to be written.
v
)

Implements java.io.DataOutput.writeDouble.

Converts the double argument to a long using the doubleToLongBits method in class Double, and then writes that long value to the underlying output stream as an 8-byte quantity, high byte first.

public final void
writeFloat(float
a float value to be written.
v
)

Implements java.io.DataOutput.writeFloat.

Converts the float argument to an int using the floatToIntBits method in class Float, and then writes that int value to the underlying output stream as a 4-byte quantity, high byte first.

public final void
writeInt(int
an int to be written.
v
)

Implements java.io.DataOutput.writeInt.

Writes an int to the underlying output stream as four bytes, high byte first.

public final void
writeLong(long
a long to be written.
v
)

Implements java.io.DataOutput.writeLong.

Writes a long to the underlying output stream as eight bytes, high byte first.

public final void
writeShort(int
a short to be written.
v
)

Implements java.io.DataOutput.writeShort.

Writes a short to the underlying output stream as two bytes, high byte first.

public final void
writeUTF(String
a string to be written.
str
)

Implements java.io.DataOutput.writeUTF.

Writes a string to the underlying output stream using modified UTF-8 encoding in a machine-independent manner.

pack-priv static int

Returns:

The number of bytes written out.
writeUTF
(String
a string to be written.
str
,
DataOutput
destination to write to
out
)

Writes a string to the specified DataOutput using modified UTF-8 encoding in a machine-independent manner.

Inherited from java.io.FilterOutputStream:
closewrite

Field Detail

bytearrback to summary
private byte[] bytearr

bytearr is initialized on demand by writeUTF

JLAback to summary
private static final JavaLangAccess JLA
writeBufferback to summary
private final byte[] writeBuffer
writtenback to summary
protected int written

The number of bytes written to the data output stream so far. If this counter overflows, it will be wrapped to Integer.MAX_VALUE.

Constructor Detail

DataOutputStreamback to summary
public DataOutputStream(OutputStream out)

Creates a new data output stream to write data to the specified underlying output stream. The counter written is set to zero.

Parameters
out:OutputStream

the underlying output stream, to be saved for later use.

See Also
java.io.FilterOutputStream#out

Method Detail

flushback to summary
public void flush() throws IOException

Overrides java.io.FilterOutputStream.flush.

Implements java.io.Flushable.flush.

Flushes this data output stream. This forces any buffered output bytes to be written out to the stream.

The flush method of DataOutputStream calls the flush method of its underlying output stream.

Exceptions
IOException:
if an I/O error occurs.
See Also
java.io.FilterOutputStream#out, java.io.OutputStream#flush()
incCountback to summary
private void incCount(int value)

Increases the written counter by the specified value until it reaches Integer.MAX_VALUE.

sizeback to summary
public final int size()

Returns the current value of the counter written, the number of bytes written to this data output stream so far. If the counter overflows, it will be wrapped to Integer.MAX_VALUE.

Returns:int

the value of the written field.

See Also
java.io.DataOutputStream#written
tooLongMsgback to summary
private static String tooLongMsg(String s, int bits32)
writeback to summary
public synchronized void write(int b) throws IOException

Overrides java.io.FilterOutputStream.write.

Implements java.io.DataOutput.write.

Writes the specified byte (the low eight bits of the argument b) to the underlying output stream. If no exception is thrown, the counter written is incremented by 1.

Implements the write method of OutputStream.

Parameters
b:int

the byte to be written.

Exceptions
IOException:
if an I/O error occurs.
See Also
java.io.FilterOutputStream#out
writeback to summary
public synchronized void write(byte[] b, int off, int len) throws IOException

Overrides java.io.FilterOutputStream.write.

Implements java.io.DataOutput.write.

Writes len bytes from the specified byte array starting at offset off to the underlying output stream. If no exception is thrown, the counter written is incremented by len.

Parameters
b:byte[]

the data.

off:int

the start offset in the data.

len:int

the number of bytes to write.

Exceptions
IOException:
if an I/O error occurs.
IndexOutOfBoundsException:
If off is negative, len is negative, or len is greater than b.length - off
See Also
java.io.FilterOutputStream#out
writeBooleanback to summary
public final void writeBoolean(boolean v) throws IOException

Implements java.io.DataOutput.writeBoolean.

Writes a boolean to the underlying output stream as a 1-byte value. The value true is written out as the value (byte)1; the value false is written out as the value (byte)0. If no exception is thrown, the counter written is incremented by 1.

Parameters
v:boolean

a boolean value to be written.

Exceptions
IOException:
if an I/O error occurs.
See Also
java.io.FilterOutputStream#out
writeByteback to summary
public final void writeByte(int v) throws IOException

Implements java.io.DataOutput.writeByte.

Writes out a byte to the underlying output stream as a 1-byte value. If no exception is thrown, the counter written is incremented by 1.

Parameters
v:int

a byte value to be written.

Exceptions
IOException:
if an I/O error occurs.
See Also
java.io.FilterOutputStream#out
writeBytesback to summary
public final void writeBytes(String s) throws IOException

Implements java.io.DataOutput.writeBytes.

Writes out the string to the underlying output stream as a sequence of bytes. Each character in the string is written out, in sequence, by discarding its high eight bits. If no exception is thrown, the counter written is incremented by the length of s.

Parameters
s:String

a string of bytes to be written.

Exceptions
IOException:
if an I/O error occurs.
See Also
java.io.FilterOutputStream#out
writeCharback to summary
public final void writeChar(int v) throws IOException

Implements java.io.DataOutput.writeChar.

Writes a char to the underlying output stream as a 2-byte value, high byte first. If no exception is thrown, the counter written is incremented by 2.

Parameters
v:int

a char value to be written.

Exceptions
IOException:
if an I/O error occurs.
See Also
java.io.FilterOutputStream#out
writeCharsback to summary
public final void writeChars(String s) throws IOException

Implements java.io.DataOutput.writeChars.

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 the writeChar method. If no exception is thrown, the counter written is incremented by twice the length of s.

Parameters
s:String

a String value to be written.

Exceptions
IOException:
if an I/O error occurs.
See Also
java.io.DataOutputStream#writeChar(int), java.io.FilterOutputStream#out
writeDoubleback to summary
public final void writeDouble(double v) throws IOException

Implements java.io.DataOutput.writeDouble.

Converts the double argument to a long using the doubleToLongBits method in class Double, and then writes that long value to the underlying output stream as an 8-byte quantity, high byte first. If no exception is thrown, the counter written is incremented by 8.

Parameters
v:double

a double value to be written.

Exceptions
IOException:
if an I/O error occurs.
See Also
java.io.FilterOutputStream#out, java.lang.Double#doubleToLongBits(double)
writeFloatback to summary
public final void writeFloat(float v) throws IOException

Implements java.io.DataOutput.writeFloat.

Converts the float argument to an int using the floatToIntBits method in class Float, and then writes that int value to the underlying output stream as a 4-byte quantity, high byte first. If no exception is thrown, the counter written is incremented by 4.

Parameters
v:float

a float value to be written.

Exceptions
IOException:
if an I/O error occurs.
See Also
java.io.FilterOutputStream#out, java.lang.Float#floatToIntBits(float)
writeIntback to summary
public final void writeInt(int v) throws IOException

Implements java.io.DataOutput.writeInt.

Writes an int to the underlying output stream as four bytes, high byte first. If no exception is thrown, the counter written is incremented by 4.

Parameters
v:int

an int to be written.

Exceptions
IOException:
if an I/O error occurs.
See Also
java.io.FilterOutputStream#out
writeLongback to summary
public final void writeLong(long v) throws IOException

Implements java.io.DataOutput.writeLong.

Writes a long to the underlying output stream as eight bytes, high byte first. In no exception is thrown, the counter written is incremented by 8.

Parameters
v:long

a long to be written.

Exceptions
IOException:
if an I/O error occurs.
See Also
java.io.FilterOutputStream#out
writeShortback to summary
public final void writeShort(int v) throws IOException

Implements java.io.DataOutput.writeShort.

Writes a short to the underlying output stream as two bytes, high byte first. If no exception is thrown, the counter written is incremented by 2.

Parameters
v:int

a short to be written.

Exceptions
IOException:
if an I/O error occurs.
See Also
java.io.FilterOutputStream#out
writeUTFback to summary
public final void writeUTF(String str) throws IOException

Implements java.io.DataOutput.writeUTF.

Writes a string to the underlying output stream using modified UTF-8 encoding in a machine-independent manner.

First, two bytes are written to the output stream as if by the writeShort method giving the number of bytes to follow. This value is the number of bytes actually written out, not the length of the string. Following the length, each character of the string is output, in sequence, using the modified UTF-8 encoding for the character. If no exception is thrown, the counter written is incremented by the total number of bytes written to the output stream. This will be at least two plus the length of str, and at most two plus thrice the length of str.

Parameters
str:String

a string to be written.

Exceptions
IOException:
if some other I/O error occurs.
UTFDataFormatException:
if the modified UTF-8 encoding of str would exceed 65535 bytes in length
See Also
writeChars(String)
writeUTFback to summary
pack-priv static int writeUTF(String str, DataOutput out) throws IOException

Writes a string to the specified DataOutput using modified UTF-8 encoding in a machine-independent manner.

First, two bytes are written to out as if by the writeShort method giving the number of bytes to follow. This value is the number of bytes actually written out, not the length of the string. Following the length, each character of the string is output, in sequence, using the modified UTF-8 encoding for the character. If no exception is thrown, the counter written is incremented by the total number of bytes written to the output stream. This will be at least two plus the length of str, and at most two plus thrice the length of str.

Parameters
str:String

a string to be written.

out:DataOutput

destination to write to

Returns:int

The number of bytes written out.

Annotations
@SuppressWarnings:deprecation
Exceptions
IOException:
if some other I/O error occurs.
UTFDataFormatException:
if the modified UTF-8 encoding of str would exceed 65535 bytes in length