java.io.ByteArrayOutputStream . Note that
this variant does not extend ByteArrayOutputStream, unlike
its sibling ResizableByteArrayOutputStream.
Unlike java., this implementation is backed
by a java. of byte[] buffers instead of one
constantly resizing byte[]. It does not copy buffers when it gets expanded.
The initial buffer is only created when the stream is first written.
There is also no copying of the internal buffers if the stream's content is
extracted via the writeTo(OutputStream) method.
resize, ResizableByteArrayOutputStream
| Modifier and Type | Class and Description |
|---|---|
| private static class | FastByteArrayOutputStream.FastByteArrayInputStream
An implementation of |
| Modifier and Type | Field and Description |
|---|---|
| private int | |
| private final Deque | |
| private boolean | |
| private static final int | |
| private int | |
| private final int | |
| private int |
| Access | Constructor and Description |
|---|---|
| public | FastByteArrayOutputStream()
Create a new |
| public | FastByteArrayOutputStream(int
the initial buffer size in bytes initialBlockSize)Create a new |
| Modifier and Type | Method and Description |
|---|---|
| private void | |
| public void | close()
Overrides java. Implements java. |
| public InputStream | Returns: InputStream of the contents of this FastByteArrayOutputStreamGet an |
| private static int | |
| public void | |
| public void | resize(int
the desired size of the buffer targetCapacity)Resize the internal buffer size to the specified capacity. |
| public int | |
| public byte[] | Returns: the current contents of this stream as a byte arrayCreate a newly allocated byte array. |
| public byte[] | Returns: the current contents of this stream as a byte arrayConvert this stream's contents to a byte array and return the byte array. |
| public String | Returns: a String decoded from this stream's contentsOverrides java. |
| public String | |
| public void | write(int
the datum)byte.Implements abstract java. |
| public void | write(byte[]
the data. data, int the start offset in the data. offset, int the number of bytes to write. length)Overrides java. len bytes from the specified byte array
starting at offset off to this output stream.
|
| public void | writeTo(OutputStream
the OutputStream to write to out)Write the contents of this |
| alreadyBufferedSize | back to summary |
|---|---|
| private int alreadyBufferedSize | |
| buffers | back to summary |
|---|---|
| private final Deque<byte[]> buffers | |
| closed | back to summary |
|---|---|
| private boolean closed | |
| DEFAULT_BLOCK_SIZE | back to summary |
|---|---|
| private static final int DEFAULT_BLOCK_SIZE | |
| index | back to summary |
|---|---|
| private int index | |
| initialBlockSize | back to summary |
|---|---|
| private final int initialBlockSize | |
| nextBlockSize | back to summary |
|---|---|
| private int nextBlockSize | |
| FastByteArrayOutputStream | back to summary |
|---|---|
| public FastByteArrayOutputStream() Create a new | |
| FastByteArrayOutputStream | back to summary |
|---|---|
| public FastByteArrayOutputStream(int initialBlockSize) Create a new
| |
| addBuffer | back to summary |
|---|---|
| private void addBuffer(int minCapacity) Create a new buffer and store it in the ArrayDeque. Adds a new buffer that can store at least | |
| close | back to summary |
|---|---|
| public void close() Overrides java. Implements java. Doc from java. Closes this output stream and releases any system resources
associated with this stream. The general contract of
| |
| getInputStream | back to summary |
|---|---|
| public InputStream getInputStream() Get an Note that if any methods are called on this
| |
| nextPowerOf2 | back to summary |
|---|---|
| private static int nextPowerOf2(int val) Get the next power of 2 of a number (ex, the next power of 2 of 119 is 128). | |
| reset | back to summary |
|---|---|
| public void reset() Reset the contents of this All currently accumulated output in the output stream is discarded. The output stream can be used again. | |
| resize | back to summary |
|---|---|
| public void resize(int targetCapacity) Resize the internal buffer size to the specified capacity.
| |
| size | back to summary |
|---|---|
| public int size() Return the number of bytes stored in this | |
| toByteArray | back to summary |
|---|---|
| public byte[] toByteArray() Create a newly allocated byte array. Its size is the current size of this output stream, and it will contain the valid contents of the internal buffers.
| |
| toByteArrayUnsafe | back to summary |
|---|---|
| public byte[] toByteArrayUnsafe() Convert this stream's contents to a byte array and return the byte array. Also replaces the internal structures with the byte array to conserve memory: if the byte array is being created anyway, we might as well as use it. This approach also means that if this method is called twice without any writes in the interim, the second call is a no-op. This method is "unsafe" as it returns the internal buffer. Callers should not modify the returned buffer.
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Convert this stream's contents to a string by decoding the bytes using the
platform's default character set. The length of the new This method always replaces malformed-input and unmappable-character sequences with the default replacement string for the platform's default character set. The java.nio.charset.CharsetDecoder class should be used when more control over the decoding process is required.
| |
| toString | back to summary |
|---|---|
| public String toString(Charset charset) Convert this stream's contents to a string by decoding the bytes using the
specified
| |
| write | back to summary |
|---|---|
| public void write(int datum) throws IOException Implements abstract java. Doc from java. Writes the specified byte to this output stream. The general
contract for
| |
| write | back to summary |
|---|---|
| public void write(byte[] data, int offset, int length) throws IOException Overrides java. Doc from java. Writes
If
If
| |
| writeTo | back to summary |
|---|---|
| public void writeTo(OutputStream out) throws IOException Write the contents of this
| |
java.io.InputStream that reads from a given
FastByteArrayOutputStream.
| Modifier and Type | Field and Description |
|---|---|
| private final Iterator | |
| private byte[] | |
| private int | |
| private final FastByteArrayOutputStream | |
| private int | |
| private int |
| Access | Constructor and Description |
|---|---|
| public | FastByteArrayInputStream(FastByteArrayOutputStream fastByteArrayOutputStream)
Create a new |
| Modifier and Type | Method and Description |
|---|---|
| public int | available()
Overrides java. |
| public int | read()
Implements abstract java. |
| public int | read(byte[]
the buffer into which the data is read. b)Overrides java. b.
|
| public int | read(byte[]
the buffer into which the data is read. b, int the start offset in array off, int b
at which the data is written.the maximum number of bytes to read. len)Overrides java. len bytes of data from the input stream into
an array of bytes.
|
| public long | skip(long
the number of bytes to be skipped. n)Overrides java. n bytes of data from this input
stream.
|
| private void | |
| public void | updateMessageDigest(MessageDigest
the message digest to update messageDigest)Overrides org. |
| public void | updateMessageDigest(MessageDigest
the message digest to update messageDigest, int how many bytes to read from this stream and use to update the message digest len)Overrides org. |
| buffersIterator | back to summary |
|---|---|
| private final Iterator<byte[]> buffersIterator | |
| currentBuffer | back to summary |
|---|---|
| private byte[] currentBuffer
| |
| currentBufferLength | back to summary |
|---|---|
| private int currentBufferLength | |
| fastByteArrayOutputStream | back to summary |
|---|---|
| private final FastByteArrayOutputStream fastByteArrayOutputStream | |
| nextIndexInCurrentBuffer | back to summary |
|---|---|
| private int nextIndexInCurrentBuffer | |
| totalBytesRead | back to summary |
|---|---|
| private int totalBytesRead | |
| FastByteArrayInputStream | back to summary |
|---|---|
| public FastByteArrayInputStream(FastByteArrayOutputStream fastByteArrayOutputStream) Create a new | |
| available | back to summary |
|---|---|
| public int available() Overrides java. Doc from java. Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking, which may be 0, or 0 when end of stream is detected. The read might be on the same thread or another thread. A single read or skip of this many bytes will not block, but may read or skip fewer bytes. Note that while some implementations of A subclass's implementation of this method may choose to throw an
| |
| read | back to summary |
|---|---|
| public int read() Implements abstract java. Doc from java. Reads the next byte of data from the input stream. The value byte is
returned as an
| |
| read | back to summary |
|---|---|
| public int read(byte[] b) Overrides java. Doc from java. Reads some number of bytes from the input stream and stores them into
the buffer array If the length of The first byte read is stored into element
| |
| read | back to summary |
|---|---|
| public int read(byte[] b, int off, int len) Overrides java. Doc from java. Reads up to This method blocks until input data is available, end of file is detected, or an exception is thrown. If The first byte read is stored into element In every case, elements
| |
| skip | back to summary |
|---|---|
| public long skip(long n) throws IOException Overrides java. Doc from java. Skips over and discards
| |
| updateCurrentBufferLength | back to summary |
|---|---|
| private void updateCurrentBufferLength() | |
| updateMessageDigest | back to summary |
|---|---|
| public void updateMessageDigest(MessageDigest messageDigest) Overrides org. Update the message digest with the remaining bytes in this stream.
| |
| updateMessageDigest | back to summary |
|---|---|
| public void updateMessageDigest(MessageDigest messageDigest, int len) Overrides org. Update the message digest with the next len bytes in this stream. Avoids creating new byte arrays and use internal buffers for performance.
| |