public class ByteArrayHolder extends OutputStream
| Modifier and Type | Field and Description |
|---|---|
protected byte[] |
buf
The buffer where data is stored.
|
protected int |
count
The number of valid bytes in the buffer.
|
| Constructor and Description |
|---|
ByteArrayHolder()
Creates a new byte array output stream.
|
ByteArrayHolder(byte[] underlyingBuf) |
ByteArrayHolder(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 |
clear() |
void |
close()
Closing a ByteArrayOutputStream has no effect.
|
void |
ensureCapacity(int minCapacity)
Increases the capacity if necessary to ensure that it can hold at least the number of elements
specified by the minimum capacity argument.
|
void |
ensureCapacity(int minCapacity,
boolean keepData) |
void |
ensureHasSpace(int numBytesToAdd)
Checks if there is enough space in the buffer to write N additional bytes.
|
int |
getCapacity() |
int |
getCapacityLeft() |
int |
getPosition()
This is the position of the next write to the underlying buffer.
|
byte[] |
getUnderlyingBytes() |
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 newPos)
This is a dangerous method, be sure you know what you're doing.
|
int |
size()
Returns the current size of the buffer.
|
byte[] |
toByteArray()
Creates a newly allocated byte array.
|
ByteBuffer |
toByteBuffer() |
String |
toString()
Converts the buffer's contents into a string decoding bytes using the platform's default
character set.
|
String |
toString(int hibyte)
Deprecated.
This method does not properly convert bytes into characters. As of JDK 1.1,
the preferred way to do this is via the
toString(String enc) method, which takes an encoding-name
argument, or the toString() method, which uses the platform's default character
encoding. |
String |
toString(String charsetName)
Converts the buffer's contents into a string by decoding the bytes using the specified
charsetName. |
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 |
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, writeprotected byte[] buf
protected int count
public ByteArrayHolder(int size)
size - the initial size.IllegalArgumentException - if size is negative.public ByteArrayHolder()
public ByteArrayHolder(byte[] underlyingBuf)
public void write(int b)
write in class OutputStreamb - 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 OutputStreamb - the data.off - the start offset in the data.len - the number of bytes to write.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.ByteArrayInputStream.countpublic byte[] toByteArray()
ByteArrayOutputStream.size()public int size()
count field, which is the number of valid bytes in this
output stream.ByteArrayOutputStream.countpublic 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
charsetName. 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
charsetUnsupportedEncodingException - If the named charset is not supported@Deprecated public String toString(int hibyte)
toString(String enc) method, which takes an encoding-name
argument, or the toString() method, which uses the platform's default character
encoding.
c == (char)(((hibyte & 0xff) << 8) | (b & 0xff))
hibyte - the high byte of each resulting Unicode character.ByteArrayOutputStream.size(),
ByteArrayOutputStream.toString(String),
ByteArrayOutputStream.toString()public void close()
throws IOException
close in interface Closeableclose in interface AutoCloseableclose in class OutputStreamIOExceptionpublic ByteBuffer toByteBuffer()
public byte[] getUnderlyingBytes()
public final int getCapacity()
public final int getCapacityLeft()
public void ensureCapacity(int minCapacity)
minCapacity - the desired minimum capacityOutOfMemoryError - if minCapacity < 0. This is interpreted as a request for the
unsatisfiably large capacity (long) Integer.MAX_VALUE + (minCapacity -
Integer.MAX_VALUE).public void ensureCapacity(int minCapacity,
boolean keepData)
minCapacity - the min capacity to grow the underlying buffer to. The actual increased
capacity might be more than that.keepData - if true, will copy the previous contents of the buffer to the new one. If you
are just reusing an old buffer for new data, you might want to set this to false, then the
buffer will be reset.public void ensureHasSpace(int numBytesToAdd)
numBytesToAdd - the number of bytes you want to add to the bufferpublic final int getPosition()
public final void setPosition(int newPos)
public final void clear()
Copyright © 2017–2018. All rights reserved.