Interface ByteArrayDataOutput
-
- All Superinterfaces:
DataOutput
@GwtIncompatible public interface ByteArrayDataOutput extends DataOutput
An extension ofDataOutput
for writing to in-memory byte arrays; its methods offer identical functionality but do not throwIOException
.- Since:
- 1.0
- Author:
- Jayaprabhakar Kadarkarai
-
-
Method Summary
All Methods Instance Methods Abstract Methods Deprecated Methods Modifier and Type Method Description byte[]
toByteArray()
Returns the contents that have been written to this instance, as a byte array.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)
Deprecated.This method is dangerous as it discards the high byte of every character.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)
Description copied from interface:java.io.DataOutput
Writes to the output stream the eight low-order bits of the argumentb
. The 24 high-order bits ofb
are ignored.- Specified by:
write
in interfaceDataOutput
- Parameters:
b
- the byte to be written.
-
write
void write(byte[] b)
Description copied from interface:java.io.DataOutput
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]
.- Specified by:
write
in interfaceDataOutput
- Parameters:
b
- the data.
-
write
void write(byte[] b, int off, int len)
Description copied from interface:java.io.DataOutput
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]
.- Specified by:
write
in interfaceDataOutput
- Parameters:
b
- the data.off
- the start offset in the data.len
- the number of bytes to write.
-
writeBoolean
void writeBoolean(boolean v)
Description copied from interface:java.io.DataOutput
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
.- Specified by:
writeBoolean
in interfaceDataOutput
- Parameters:
v
- the boolean to be written.
-
writeByte
void writeByte(int v)
Description copied from interface:java.io.DataOutput
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
.- Specified by:
writeByte
in interfaceDataOutput
- Parameters:
v
- the byte value to be written.
-
writeShort
void writeShort(int v)
Description copied from interface:java.io.DataOutput
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
.- Specified by:
writeShort
in interfaceDataOutput
- Parameters:
v
- theshort
value to be written.
-
writeChar
void writeChar(int v)
Description copied from interface:java.io.DataOutput
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
.- Specified by:
writeChar
in interfaceDataOutput
- Parameters:
v
- thechar
value to be written.
-
writeInt
void writeInt(int v)
Description copied from interface:java.io.DataOutput
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
.- Specified by:
writeInt
in interfaceDataOutput
- Parameters:
v
- theint
value to be written.
-
writeLong
void writeLong(long v)
Description copied from interface:java.io.DataOutput
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
.- Specified by:
writeLong
in interfaceDataOutput
- Parameters:
v
- thelong
value to be written.
-
writeFloat
void writeFloat(float v)
Description copied from interface:java.io.DataOutput
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
.- Specified by:
writeFloat
in interfaceDataOutput
- Parameters:
v
- thefloat
value to be written.
-
writeDouble
void writeDouble(double v)
Description copied from interface:java.io.DataOutput
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
.- Specified by:
writeDouble
in interfaceDataOutput
- Parameters:
v
- thedouble
value to be written.
-
writeChars
void writeChars(String s)
Description copied from interface:java.io.DataOutput
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.- Specified by:
writeChars
in interfaceDataOutput
- Parameters:
s
- the string value to be written.
-
writeUTF
void writeUTF(String s)
Description copied from interface:java.io.DataOutput
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
.- Specified by:
writeUTF
in interfaceDataOutput
- Parameters:
s
- the string value to be written.
-
writeBytes
@Deprecated void writeBytes(String s)
Deprecated.This method is dangerous as it discards the high byte of every character. For UTF-8, usewrite(s.getBytes(StandardCharsets.UTF_8))
.Description copied from interface:java.io.DataOutput
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.- Specified by:
writeBytes
in interfaceDataOutput
- Parameters:
s
- the string of bytes to be written.
-
toByteArray
byte[] toByteArray()
Returns the contents that have been written to this instance, as a byte array.
-
-