public class ByteArrayOutput extends BufferedOutput
BufferedOutput
using single byte array buffer.
Note, that most of the time, you need to use ChunkedOutput
for better efficiency.buffer, EMPTY_BYTE_ARRAY, limit, position, totalPositionBase
Constructor and Description |
---|
ByteArrayOutput()
Creates a new byte array output without pre-allocated buffer.
|
ByteArrayOutput(byte[] buffer)
Creates a new byte array output with the specified buffer.
|
ByteArrayOutput(int size)
Creates a new byte array output with the specified buffer capacity.
|
Modifier and Type | Method and Description |
---|---|
void |
clear()
Clears this buffer.
|
void |
ensureCapacity(int requiredCapacity)
Ensures that the byte array used for buffering has at least the specified capacity.
|
byte[] |
getBuffer()
Returns byte array used for buffering.
|
int |
getLimit()
Returns limit.
|
int |
getPosition()
Returns position.
|
protected void |
needSpace()
This method is invoked when output methods need more space to write data.
|
void |
setBuffer(byte[] newBuffer)
Sets specified byte array to be used for buffering.
|
void |
setLimit(int newLimit)
Sets limit as specified.
|
void |
setPosition(int newPosition)
Sets position as specified.
|
byte[] |
toByteArray()
Returns a full copy of byte array buffer from zero to the position.
|
String |
toString()
Converts the buffer's content from zero to the position into a string,
translating bytes into characters according to the platform's default character encoding.
|
void |
write(byte[] b,
int off,
int len) |
checkEOB, totalPosition, write, write, writeAllFromChunkList, writeBoolean, writeByte, writeByteArray, writeBytes, writeChar, writeChars, writeCompactInt, writeCompactLong, writeDouble, writeFloat, writeFromByteBuffer, writeFromChunk, writeFromDataInput, writeFromInputStream, writeInt, writeLong, writeMarshalled, writeObject, writeShort, writeUTF, writeUTFChar, writeUTFString
close, flush
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
close, flush
public ByteArrayOutput()
public ByteArrayOutput(int size)
size
- the initial buffer sizeIllegalArgumentException
- if size is negativepublic ByteArrayOutput(byte[] buffer)
buffer
- the buffer.NullPointerException
- if the buffer is null.protected final void needSpace() throws IOException
(position < limit)
is true or throw an exception.
This implementation is final and invokes
.
Classes that extend ensureCapacity
(position
+ 1)ByteArrayOutput
shall override ensureCapacity(int)
to
customize their buffer allocation strategy.
needSpace
in class BufferedOutput
IOException
- if buffer is already too bigpublic byte[] getBuffer()
public void setBuffer(byte[] newBuffer)
public int getPosition()
public void setPosition(int newPosition)
IllegalArgumentException
- if the new position is negative or is larger than the limitpublic int getLimit()
public void setLimit(int newLimit)
IllegalArgumentException
- if the new limit is negative or is larger than the capacitypublic void clear()
totalPosition
is reset to 0.public void write(byte[] b, int off, int len) throws IOException
This implementation also ensures that buffer has an appropriate capacity with at most one reallocation of the underlying array.
This implementation allocates an internal buffer at the size that is equal to the number of
written bytes at the first invocation of this method. This ensures that if all data that is
written into this byte array output is a result of single invocation of this write method, then
the underlying buffer has an appropriate size. This property is used by Marshalled.getBytes()
method
to avoid extra copying of bytes.
write
in interface DataOutput
write
in interface ObjectOutput
write
in class BufferedOutput
IOException
public void ensureCapacity(int requiredCapacity)
public byte[] toByteArray()
Copyright © 2002–2025 Devexperts LLC. All rights reserved.