Package org.springframework.util

# Class FastByteArrayOutputStream

java.lang.Object
java.io.OutputStream
org.springframework.util.FastByteArrayOutputStream

All Implemented Interfaces:
  `Closeable`, `Flushable`, `AutoCloseable`

public class FastByteArrayOutputStream extends OutputStream
A speedy alternative to `ByteArrayOutputStream`. Note that this variant does not extend `ByteArrayOutputStream`, unlike its sibling `ResizableByteArrayOutputStream`.

Unlike `ByteArrayOutputStream`, this implementation is backed by a `ArrayDeque` 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.

Since:
  4.2
Author:
  Craig Andrews, Juergen Hoeller
See Also:
  - `resize(int)`
  - `ResizableByteArrayOutputStream`

## Constructor Summary

Constructors
Constructor
Description
`FastByteArrayOutputStream()`
Create a new `FastByteArrayOutputStream` with the default initial capacity of 256 bytes.
`FastByteArrayOutputStream(int initialBlockSize)`
Create a new `FastByteArrayOutputStream` with the specified initial capacity.

## Method Summary

Modifier and Type
Method
Description
`void`
`close()`
`InputStream`
`getInputStream()`
Get an `InputStream` to retrieve the contents of this `FastByteArrayOutputStream`.
`void`
`reset()`
Reset the contents of this `FastByteArrayOutputStream`.
`void`
`resize(int targetCapacity)`
Resize the internal buffer size to the specified capacity.
`int`
`size()`
Return the number of bytes stored in this `FastByteArrayOutputStream`.
`byte[]`
`toByteArray()`
Create a newly allocated byte array.
`byte[]`
`toByteArrayUnsafe()`
Convert this stream's contents to a byte array and return the byte array.
`String`
`toString()`
Convert this stream's contents to a string by decoding the bytes using the platform's default character set.
`String`
`toString(Charset charset)`
Convert this stream's contents to a string by decoding the bytes using the specified `Charset`.
`void`
`write(byte[] data, int offset, int length)`
`void`
`write(int datum)`
`void`
`writeTo(OutputStream out)`
Write the contents of this `FastByteArrayOutputStream` to the given `OutputStream`.

### Methods inherited from class java.io.OutputStream

`flush, nullOutputStream, write`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

## Constructor Details

### FastByteArrayOutputStream

public FastByteArrayOutputStream()
Create a new `FastByteArrayOutputStream` with the default initial capacity of 256 bytes.

### FastByteArrayOutputStream

public FastByteArrayOutputStream(int initialBlockSize)
Create a new `FastByteArrayOutputStream` with the specified initial capacity.

Parameters:
  `initialBlockSize` - the initial buffer size in bytes

## Method Details

### write

public void write(int datum) throws IOException

Specified by:
  `write` in class `OutputStream`
Throws:
  `IOException`

### write

public void write(byte[] data, int offset, int length) throws IOException

Overrides:
  `write` in class `OutputStream`
Throws:
  `IOException`

### close

public void close()

Specified by:
  `close` in interface `AutoCloseable`
Specified by:
  `close` in interface `Closeable`
Overrides:
  `close` in class `OutputStream`

### toString

public String toString()
Convert this stream's contents to a string by decoding the bytes using the platform's default character set. The length of the new `String` is a function of the character set, and hence may not be equal to the size of the buffers.

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.

Overrides:
  `toString` in class `Object`
Returns:
  a String decoded from this stream's contents
See Also:
  - `toString(Charset)`

### toString

public String toString(Charset charset)
Convert this stream's contents to a string by decoding the bytes using the specified `Charset`.

Parameters:
  `charset` - the `Charset` to use to decode the bytes
Returns:
  a String decoded from this stream's contents
Since:
  6.1.2
See Also:
  - `toString()`

### size

public int size()
Return the number of bytes stored in this `FastByteArrayOutputStream`.

### toByteArrayUnsafe

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.

Returns:
  the current contents of this stream as a byte array
See Also:
  - `size()`
  - `toByteArray()`

### toByteArray

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.

Returns:
  the current contents of this stream as a byte array
See Also:
  - `size()`
  - `toByteArrayUnsafe()`

### reset

public void reset()
Reset the contents of this `FastByteArrayOutputStream`.

All currently accumulated output in the output stream is discarded. The output stream can be used again.

### getInputStream

public InputStream getInputStream()
Get an `InputStream` to retrieve the contents of this `FastByteArrayOutputStream`.

Note that if any methods are called on this `FastByteArrayOutputStream` (including, but not limited to, any of the write methods, `reset()`, `toByteArray()`, and `toByteArrayUnsafe()`) then the `InputStream`'s behavior is undefined.

Returns:
  `InputStream` of the contents of this `FastByteArrayOutputStream`

### writeTo

public void writeTo(OutputStream out) throws IOException
Write the contents of this `FastByteArrayOutputStream` to the given `OutputStream`.

Parameters:
  `out` - the OutputStream to write to
Throws:
  `IOException`

### resize

public void resize(int targetCapacity)
Resize the internal buffer size to the specified capacity.

Parameters:
  `targetCapacity` - the desired size of the buffer
Throws:
  `IllegalArgumentException` - if the given capacity is smaller than the actual size of the content stored in the buffer already
See Also:
  - `size()`