public class MessageBufferOutput extends OutputStream
This is literally a copy of the ByteArrayOutputStream
from the runtime library except it's been modified in the following ways:
getBuffer()
.grow()
method to externally increase the internal buffer size.replace(byte[])
method that will allow replacing the underlying buffer with a different one.setPosition(int)
method that will set the current output position.getPosition()
for clarity.DataOutputStream
write methods have been added.Remember, with great power comes great responsibility
Modifier and Type | Field and Description |
---|---|
protected byte[] |
buf
The buffer where data is stored.
|
protected int |
length
This is a cache for the length of the underlying byte[] to avoid constant dereferencing.
|
protected int |
position
The number of valid bytes in the buffer.
|
Constructor and Description |
---|
MessageBufferOutput()
Creates a new byte array output stream.
|
MessageBufferOutput(int size)
Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Closing a ByteArrayOutputStream has no effect.
|
byte[] |
getBuffer()
Returns the underlying byte[]
|
int |
getPosition()
Returns the current position in the buffer where the next byte will be written to.
|
void |
grow()
Doubles the internal buffer size while preserving the data
|
void |
grow(int newcap)
Doubles the internal buffer size, or sets to the capacity provided if that's higher, while preserving the data
|
void |
replace(byte[] buffer)
Completely replace the underlying buffer.
|
void |
reset()
Resets the
count field of this byte array output stream to zero, so that all currently accumulated output in the output stream is discarded. |
void |
setPosition(int newPosition)
Explicitly set the current position in the buffer.
|
byte[] |
toByteArray()
Creates a newly allocated byte array.
|
String |
toString()
Converts the buffer's contents into a string decoding bytes using the platform's default character set.
|
String |
toString(String charsetName)
Converts the buffer's contents into a string by decoding the bytes using the specified
Charset name. |
void |
write(byte[] b,
int off,
int len)
Writes
len bytes from the specified byte array starting at offset off to this byte array output stream. |
void |
write(int b)
Writes the specified byte to this byte array output stream.
|
void |
writeInt(int v)
Writes an
int to the underlying buffer as four
bytes, high byte first. |
void |
writeShort(short x) |
void |
writeTo(OutputStream out)
Writes the complete contents of this byte array output stream to the specified output stream argument, as if by calling the output stream's write method using
out.write(buf, 0, count) . |
flush, write
protected byte[] buf
protected int position
protected int length
public MessageBufferOutput()
public MessageBufferOutput(int size)
size
- the initial size.IllegalArgumentException
- if size is negative.public void grow()
public void grow(int newcap)
public void replace(byte[] buffer)
public void setPosition(int newPosition)
public byte[] toByteArray()
ByteArrayOutputStream.size()
public void write(int b)
write
in class OutputStream
b
- the byte to be written.public void write(byte[] b, int off, int len)
len
bytes from the specified byte array starting at offset off
to this byte array output stream.write
in class OutputStream
b
- the data.off
- the start offset in the data.len
- the number of bytes to write.public void writeShort(short x)
public void writeInt(int v) throws IOException
int
to the underlying buffer as four
bytes, high byte first.IOException
public void writeTo(OutputStream out) throws IOException
out.write(buf, 0, count)
.out
- the output stream to which to write the data.IOException
- if an I/O error occurs.public void reset()
count
field of this byte array output stream to zero, so that all currently accumulated output in the output stream is discarded. The output stream can be used again, reusing the
already allocated buffer space.public byte[] getBuffer()
public int getPosition()
count
field, which is the number of valid bytes in this output stream.public String toString()
This method always replaces malformed-input and unmappable-character sequences with the default replacement string for the platform's default character set. The CharsetDecoder class should be used when more control over the decoding process is required.
public String toString(String charsetName) throws UnsupportedEncodingException
Charset
name. The length of the new String is a function of the charset, and hence
may not be equal to the length of the byte array.
This method always replaces malformed-input and unmappable-character sequences with this charset's default replacement string. The CharsetDecoder
class should be used when more
control over the decoding process is required.
charsetName
- the name of a supported Charset charset
UnsupportedEncodingException
- If the named charset is not supportedpublic void close()
close
in interface Closeable
close
in interface AutoCloseable
close
in class OutputStream
Copyright © 2018. All rights reserved.