|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectde.undercouch.bson4jackson.io.DynamicOutputBuffer
public class DynamicOutputBuffer
A random-access buffer that resizes itself. This buffer differentiates
from ByteArrayOutputStream in the following points:
The buffer has an initial size. This is also the size of each internal buffer, so if a new buffer has to be allocated it will take exactly that many bytes.
By calling flushTo(OutputStream) or flushTo(WritableByteChannel)
some of this buffer's internal buffers are flushed and then deallocated. The
buffer maintains an internal counter for all flushed buffers. This allows the
writeTo(OutputStream) and writeTo(WritableByteChannel)
methods to only write non-flushed buffers. So, this class can be used for
streaming by flushing internal buffers from time to time and at the end
writing the rest:
... buf.flushTo(out); ... buf.flushTo(out); ... buf.flushTo(out); ... buf.writeTo(out);
If flushing is never used a single call to one of the writeTo
methods is enough to write the whole buffer.
Once the buffer has been written to an output stream or channel, putting
elements into it is not possible anymore and will lead to an
IndexOutOfBoundsException.
| Field Summary | |
|---|---|
static int |
DEFAULT_BUFFER_SIZE
The default initial buffer size if nothing is specified |
static java.nio.ByteOrder |
DEFAULT_BYTE_ORDER
The default byte order if nothing is specified |
| Constructor Summary | |
|---|---|
DynamicOutputBuffer()
Creates a dynamic buffer with BIG_ENDIAN byte order and a default initial buffer size of DEFAULT_BUFFER_SIZE bytes. |
|
DynamicOutputBuffer(java.nio.ByteOrder order)
Creates a dynamic buffer with the given byte order and a default initial buffer size of DEFAULT_BUFFER_SIZE bytes. |
|
DynamicOutputBuffer(java.nio.ByteOrder order,
int initialSize)
Creates a dynamic buffer with the given byte order and the given initial buffer size. |
|
DynamicOutputBuffer(int initialSize)
Creates a dynamic buffer with BIG_ENDIAN byte order and the given initial buffer size. |
|
| Method Summary | |
|---|---|
void |
clear()
Clear the buffer and reset size and write position |
void |
flushTo(java.io.OutputStream out)
Tries to copy as much bytes as possible from this buffer to the given channel. |
void |
flushTo(java.nio.channels.WritableByteChannel out)
Tries to copy as much bytes as possible from this buffer to the given channel. |
void |
putByte(byte b)
Puts a byte into the buffer at the current write position and increases the write position accordingly. |
void |
putByte(int pos,
byte b)
Puts a byte into the buffer at the given position. |
void |
putBytes(byte... bs)
Puts several bytes into the buffer at the given position and increases the write position accordingly. |
void |
putBytes(int pos,
byte... bs)
Puts several bytes into the buffer at the given position. |
void |
putDouble(double d)
Puts a 64-bit floating point number into the buffer at the current write position and increases the write position accordingly. |
void |
putDouble(int pos,
double d)
Puts a 64-bit floating point number into the buffer at the given position. |
void |
putFloat(float f)
Puts a 32-bit floating point number into the buffer at the current write position and increases the write position accordingly. |
void |
putFloat(int pos,
float f)
Puts a 32-bit floating point number into the buffer at the given position. |
void |
putInt(int i)
Puts a 32-bit integer into the buffer at the current write position and increases write position accordingly. |
void |
putInt(int pos,
int i)
Puts a 32-bit integer into the buffer at the given position. |
void |
putLong(int pos,
long l)
Puts a 64-bit integer into the buffer at the given position. |
void |
putLong(long l)
Puts a 64-bit integer into the buffer at the current write position and increases the write position accordingly. |
void |
putString(java.lang.CharSequence s)
Puts a character sequence into the buffer at the current write position and increases the write position accordingly. |
void |
putString(int pos,
java.lang.CharSequence s)
Puts a character sequence into the buffer at the given position. |
int |
putUTF8(int pos,
java.lang.String s)
Puts the given string as UTF-8 into the buffer at the given position. |
int |
putUTF8(java.lang.String s)
Encodes the given string as UTF-8, puts it into the buffer and increases the write position accordingly. |
void |
setReuseBuffersCount(int count)
Sets the number of buffers to save for reuse after they have been invalidated by flushTo(OutputStream) or flushTo(WritableByteChannel). |
int |
size()
|
void |
writeTo(java.io.OutputStream out)
Writes all non-flushed internal buffers to the given output stream. |
void |
writeTo(java.nio.channels.WritableByteChannel out)
Writes all non-flushed internal buffers to the given channel. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final java.nio.ByteOrder DEFAULT_BYTE_ORDER
public static final int DEFAULT_BUFFER_SIZE
| Constructor Detail |
|---|
public DynamicOutputBuffer()
DEFAULT_BUFFER_SIZE bytes.
public DynamicOutputBuffer(int initialSize)
initialSize - the initial buffer sizepublic DynamicOutputBuffer(java.nio.ByteOrder order)
DEFAULT_BUFFER_SIZE bytes.
order - the byte order
public DynamicOutputBuffer(java.nio.ByteOrder order,
int initialSize)
order - the byte orderinitialSize - the initial buffer size| Method Detail |
|---|
public void setReuseBuffersCount(int count)
flushTo(OutputStream) or flushTo(WritableByteChannel).
Invalidated buffers will be saved in an internal queue. When the buffer
needs a new internal buffer, it first attempts to reuse an existing one
before allocating a new one.
count - the number of buffers to save for reusepublic int size()
public void clear()
public void putByte(byte b)
b - the byte to putpublic void putBytes(byte... bs)
bs - an array of bytes to put
public void putByte(int pos,
byte b)
pos - the position where to put the byteb - the byte to put
public void putBytes(int pos,
byte... bs)
pos - the position where to put the bytesbs - an array of bytes to putpublic void putInt(int i)
i - the integer to put
public void putInt(int pos,
int i)
pos - the position where to put the integeri - the integer to putpublic void putLong(long l)
l - the 64-bit integer to put
public void putLong(int pos,
long l)
pos - the position where to put the integerl - the 64-bit integer to putpublic void putFloat(float f)
f - the float to put
public void putFloat(int pos,
float f)
pos - the position where to put the floatf - the float to putpublic void putDouble(double d)
d - the double to put
public void putDouble(int pos,
double d)
pos - the position where to put the doubled - the double to putpublic void putString(java.lang.CharSequence s)
s - the character sequence to put
public void putString(int pos,
java.lang.CharSequence s)
pos - the position where to put the character sequences - the character sequence to putpublic int putUTF8(java.lang.String s)
s - the string to put
public int putUTF8(int pos,
java.lang.String s)
pos - the position where to put the strings - the string to put
public void flushTo(java.io.OutputStream out)
throws java.io.IOException
flushTo(WritableByteChannel)
for further information.
out - the output stream to write to
java.io.IOException - if the buffer could not be flushed
public void flushTo(java.nio.channels.WritableByteChannel out)
throws java.io.IOException
out - the channel to write to
java.io.IOException - if the buffer could not be flushed
public void writeTo(java.io.OutputStream out)
throws java.io.IOException
flushTo(OutputStream) has not been called
before, this method writes the whole buffer to the output stream.
out - the output stream to write to
java.io.IOException - if the buffer could not be written
public void writeTo(java.nio.channels.WritableByteChannel out)
throws java.io.IOException
flushTo(WritableByteChannel) has not been called
before, this method writes the whole buffer to the channel.
out - the channel to write to
java.io.IOException - if the buffer could not be written
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||