public class BsonDataOutput extends Object implements DataOutput
DataOutput
with additional methods needed for writing BSON formatted content. Specifically, this
class reads little-endian byte order (purposefully in violation of the DataInput interface specification) and provides a way to
read C-style strings (where the length is not known up front but instead contain all characters until the zero-byte
terminator), which are commonly used within the BSON specification.Constructor and Description |
---|
BsonDataOutput() |
Modifier and Type | Method and Description |
---|---|
protected ByteBuffer |
getBufferFor(int position) |
protected ByteBuffer |
newByteBuffer(int bufferSize) |
int |
size() |
protected void |
updateSize(int newSize) |
void |
write(byte[] b) |
void |
write(byte[] b,
int off,
int len) |
void |
write(int b) |
void |
write(int position,
byte[] value,
int offset,
int length) |
void |
writeBoolean(boolean value) |
void |
writeBoolean(int position,
boolean value) |
void |
writeByte(int value) |
void |
writeByte(int position,
int value) |
void |
writeBytes(String str)
Deprecated.
The semantics of
writeBytes(String s) are considered dangerous. Please use writeUTF(String s) ,
writeChars(String s) or another write method instead. |
void |
writeChar(int value) |
void |
writeChar(int position,
int value) |
void |
writeChars(String str)
Writes every character in the string
s , to the output stream, in order, two bytes per character. |
void |
writeDouble(double value) |
void |
writeDouble(int position,
double value) |
void |
writeFloat(float value) |
void |
writeFloat(int position,
float value) |
void |
writeInt(int value) |
void |
writeInt(int position,
int value) |
void |
writeLong(int position,
long value) |
void |
writeLong(long value) |
void |
writeShort(int value) |
void |
writeShort(int position,
int value) |
void |
writeTo(OutputStream stream)
Write all content to the supplied stream.
|
void |
writeTo(WritableByteChannel channel)
Write all content to the supplied channel.
|
int |
writeUTF(int position,
String str) |
void |
writeUTF(String str) |
int |
writeUTFString(String str)
Writes the modified UTF-8 representation of every character in the string
s . |
protected ByteBuffer getBufferFor(int position)
protected ByteBuffer newByteBuffer(int bufferSize)
protected void updateSize(int newSize)
public int size()
public void writeByte(int value)
writeByte
in interface DataOutput
public void writeByte(int position, int value)
public void write(byte[] b)
write
in interface DataOutput
public void write(byte[] b, int off, int len)
write
in interface DataOutput
public void write(int position, byte[] value, int offset, int length)
public void write(int b)
write
in interface DataOutput
public void writeBoolean(boolean value)
writeBoolean
in interface DataOutput
public void writeBoolean(int position, boolean value)
public void writeChar(int value)
writeChar
in interface DataOutput
public void writeChar(int position, int value)
public void writeInt(int value)
writeInt
in interface DataOutput
public void writeInt(int position, int value)
public void writeShort(int value)
writeShort
in interface DataOutput
public void writeShort(int position, int value)
public void writeLong(long value)
writeLong
in interface DataOutput
public void writeLong(int position, long value)
public void writeFloat(float value)
writeFloat
in interface DataOutput
public void writeFloat(int position, float value)
public void writeDouble(double value)
writeDouble
in interface DataOutput
public void writeDouble(int position, double value)
@Deprecated public void writeBytes(String str)
writeBytes(String s)
are considered dangerous. Please use writeUTF(String s)
,
writeChars(String s)
or another write method instead.s
, taken in order, one byte is written
to the output stream. If s
is null
, a NullPointerException
is thrown.
If s.length
is zero, then no bytes are written. Otherwise, the character s[0]
is written first,
then s[1]
, and so on; the last character written is s[s.length-1]
. For each character, one byte
is written, the low-order byte, in exactly the manner of the writeByte
method . The high-order eight bits of
each character in the string are ignored.
writeBytes
in interface DataOutput
str
- the string value to be written.public void writeChars(String str)
s
, to the output stream, in order, two bytes per character. If
s
is null
, a NullPointerException
is thrown. If s.length
is zero, then
no characters are written. Otherwise, the character s[0]
is written first, then s[1]
, and so on;
the last character written is s[s.length-1]
. For each character, two bytes are actually written, low-order
byte first, in exactly the manner of the writeChar
method.writeChars
in interface DataOutput
str
- the string value to be written.public void writeUTF(String str)
writeUTF
in interface DataOutput
public int writeUTFString(String str)
s
. This is similar to the standard writeUTF(String)
but without the leading two byte length.
If s
is null
, a NullPointerException
is thrown. Each character in the string
s
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
through uffff
, 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 than 65535
, then a UTFDataFormatException
is thrown. Otherwise, this length is written to
the output stream in exactly the manner of the writeShort
method; after this, the one-, two-, or three-byte
representation of each character in the string s
is written.
The bytes written by this method may be read by the readUTF
method of interface DataInput
, which
will then return a String
equal to s
.
str
- the string value to be written.public int writeUTF(int position, String str)
public void writeTo(OutputStream stream) throws IOException
stream
- the stream to which the content is to be written.IOException
- if there is a problem writing to the supplied streampublic void writeTo(WritableByteChannel channel) throws IOException
channel
- the channel to which the content is to be written.IOException
- if there is a problem writing to the supplied streamCopyright © 2008–2016 JBoss, a division of Red Hat. All rights reserved.