-
- All Known Subinterfaces:
- ImageOutputStream, ObjectOutput
- All Known Implementing Classes:
- DataOutputStream, FileCacheImageOutputStream, FileImageOutputStream, ImageOutputStreamImpl, MemoryCacheImageOutputStream, ObjectOutputStream, RandomAccessFile
public interface DataOutput
TheDataOutput
interface provides for converting data from any of the Java primitive types to a series of bytes and writing these bytes to a binary stream. There is also a facility for converting aString
into modified UTF-8 format and writing the resulting series of bytes.For all the methods in this interface that write bytes, it is generally true that if a byte cannot be written for any reason, an
IOException
is thrown.- Since:
- JDK1.0
- See Also:
DataInput
,DataOutputStream
-
-
Method Summary
Methods Modifier and Type Method and Description void
write(byte[] b)
Writes to the output stream all the bytes in arrayb
.void
write(byte[] b, int off, int len)
Writeslen
bytes from arrayb
, in order, to the output stream.void
write(int b)
Writes to the output stream the eight low-order bits of the argumentb
.void
writeBoolean(boolean v)
Writes aboolean
value to this output stream.void
writeByte(int v)
Writes to the output stream the eight low- order bits of the argumentv
.void
writeBytes(String s)
Writes a string to the output stream.void
writeChar(int v)
Writes achar
value, which is comprised of two bytes, to the output stream.void
writeChars(String s)
Writes every character in the strings
, to the output stream, in order, two bytes per character.void
writeDouble(double v)
Writes adouble
value, which is comprised of eight bytes, to the output stream.void
writeFloat(float v)
Writes afloat
value, which is comprised of four bytes, to the output stream.void
writeInt(int v)
Writes anint
value, which is comprised of four bytes, to the output stream.void
writeLong(long v)
Writes along
value, which is comprised of eight bytes, to the output stream.void
writeShort(int v)
Writes two bytes to the output stream to represent the value of the argument.void
writeUTF(String s)
Writes two bytes of length information to the output stream, followed by the modified UTF-8 representation of every character in the strings
.
-
-
-
Method Detail
-
write
void write(int b) throws IOException
Writes to the output stream the eight low-order bits of the argumentb
. The 24 high-order bits ofb
are ignored.- Parameters:
b
- the byte to be written.- Throws:
IOException
- if an I/O error occurs.
-
write
void write(byte[] b) throws IOException
Writes to the output stream all the bytes in arrayb
. Ifb
isnull
, aNullPointerException
is thrown. Ifb.length
is zero, then no bytes are written. Otherwise, the byteb[0]
is written first, thenb[1]
, and so on; the last byte written isb[b.length-1]
.- Parameters:
b
- the data.- Throws:
IOException
- if an I/O error occurs.
-
write
void write(byte[] b, int off, int len) throws IOException
Writeslen
bytes from arrayb
, in order, to the output stream. Ifb
isnull
, aNullPointerException
is thrown. Ifoff
is negative, orlen
is negative, oroff+len
is greater than the length of the arrayb
, then anIndexOutOfBoundsException
is thrown. Iflen
is zero, then no bytes are written. Otherwise, the byteb[off]
is written first, thenb[off+1]
, and so on; the last byte written isb[off+len-1]
.- 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.
-
writeBoolean
void writeBoolean(boolean v) throws IOException
Writes aboolean
value to this output stream. If the argumentv
istrue
, the value(byte)1
is written; ifv
isfalse
, the value(byte)0
is written. The byte written by this method may be read by thereadBoolean
method of interfaceDataInput
, which will then return aboolean
equal tov
.- Parameters:
v
- the boolean to be written.- Throws:
IOException
- if an I/O error occurs.
-
writeByte
void writeByte(int v) throws IOException
Writes to the output stream the eight low- order bits of the argumentv
. The 24 high-order bits ofv
are ignored. (This means thatwriteByte
does exactly the same thing aswrite
for an integer argument.) The byte written by this method may be read by thereadByte
method of interfaceDataInput
, which will then return abyte
equal to(byte)v
.- Parameters:
v
- the byte value to be written.- Throws:
IOException
- if an I/O error occurs.
-
writeShort
void writeShort(int v) throws IOException
Writes two bytes to the output stream to represent the value of the argument. The byte values to be written, in the order shown, are:(byte)(0xff & (v >> 8)) (byte)(0xff & v)
The bytes written by this method may be read by the
readShort
method of interfaceDataInput
, which will then return ashort
equal to(short)v
.- Parameters:
v
- theshort
value to be written.- Throws:
IOException
- if an I/O error occurs.
-
writeChar
void writeChar(int v) throws IOException
Writes achar
value, which is comprised of two bytes, to the output stream. The byte values to be written, in the order shown, are:(byte)(0xff & (v >> 8)) (byte)(0xff & v)
The bytes written by this method may be read by the
readChar
method of interfaceDataInput
, which will then return achar
equal to(char)v
.- Parameters:
v
- thechar
value to be written.- Throws:
IOException
- if an I/O error occurs.
-
writeInt
void writeInt(int v) throws IOException
Writes anint
value, which is comprised of four bytes, to the output stream. The byte values to be written, in the order shown, are:(byte)(0xff & (v >> 24)) (byte)(0xff & (v >> 16)) (byte)(0xff & (v >> 8)) (byte)(0xff & v)
The bytes written by this method may be read by the
readInt
method of interfaceDataInput
, which will then return anint
equal tov
.- Parameters:
v
- theint
value to be written.- Throws:
IOException
- if an I/O error occurs.
-
writeLong
void writeLong(long v) throws IOException
Writes along
value, which is comprised of eight bytes, to the output stream. The byte values to be written, in the order shown, are:(byte)(0xff & (v >> 56)) (byte)(0xff & (v >> 48)) (byte)(0xff & (v >> 40)) (byte)(0xff & (v >> 32)) (byte)(0xff & (v >> 24)) (byte)(0xff & (v >> 16)) (byte)(0xff & (v >> 8)) (byte)(0xff & v)
The bytes written by this method may be read by the
readLong
method of interfaceDataInput
, which will then return along
equal tov
.- Parameters:
v
- thelong
value to be written.- Throws:
IOException
- if an I/O error occurs.
-
writeFloat
void writeFloat(float v) throws IOException
Writes afloat
value, which is comprised of four bytes, to the output stream. It does this as if it first converts thisfloat
value to anint
in exactly the manner of theFloat.floatToIntBits
method and then writes theint
value in exactly the manner of thewriteInt
method. The bytes written by this method may be read by thereadFloat
method of interfaceDataInput
, which will then return afloat
equal tov
.- Parameters:
v
- thefloat
value to be written.- Throws:
IOException
- if an I/O error occurs.
-
writeDouble
void writeDouble(double v) throws IOException
Writes adouble
value, which is comprised of eight bytes, to the output stream. It does this as if it first converts thisdouble
value to along
in exactly the manner of theDouble.doubleToLongBits
method and then writes thelong
value in exactly the manner of thewriteLong
method. The bytes written by this method may be read by thereadDouble
method of interfaceDataInput
, which will then return adouble
equal tov
.- Parameters:
v
- thedouble
value to be written.- Throws:
IOException
- if an I/O error occurs.
-
writeBytes
void writeBytes(String s) throws IOException
Writes a string to the output stream. For every character in the strings
, taken in order, one byte is written to the output stream. Ifs
isnull
, aNullPointerException
is thrown.If
s.length
is 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 thewriteByte
method . The high-order eight bits of each character in the string are ignored.- Parameters:
s
- the string of bytes to be written.- Throws:
IOException
- if an I/O error occurs.
-
writeChars
void writeChars(String s) throws IOException
Writes every character in the strings
, to the output stream, in order, two bytes per character. Ifs
isnull
, aNullPointerException
is thrown. Ifs.length
is zero, then no characters 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, two bytes are actually written, high-order byte first, in exactly the manner of thewriteChar
method.- Parameters:
s
- the string value to be written.- Throws:
IOException
- if an I/O error occurs.
-
writeUTF
void writeUTF(String s) throws IOException
Writes two bytes of length information to the output stream, followed by the modified UTF-8 representation of every character in the strings
. Ifs
isnull
, aNullPointerException
is thrown. Each character in the strings
is converted to a group of one, two, or three bytes, depending on the value of the character.If a character
c
is in the range\u0001
through\u007f
, it is represented by one byte:(byte)c
If a character
c
is\u0000
or is in the range\u0080
through\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
c
is in the range\u0800
throughuffff
, 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
s
is calculated. If this number is larger than65535
, then aUTFDataFormatException
is thrown. Otherwise, this length is written to the output stream in exactly the manner of thewriteShort
method; after this, the one-, two-, or three-byte representation of each character in the strings
is written.The bytes written by this method may be read by the
readUTF
method of interfaceDataInput
, which will then return aString
equal tos
.- Parameters:
s
- the string value to be written.- Throws:
IOException
- if an I/O error occurs.
-
-
Document created the 11/06/2005, last modified the 04/03/2020
Source of the printed document:https://www.gaudry.be/en/java-api-rf-java/io/dataoutput.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.