类 FastByteArrayOutputStream
- 所有已实现的接口:
Closeable,Flushable,AutoCloseable
ByteArrayOutputStream. Note that
this variant does not extend ByteArrayOutputStream, unlike
its sibling ResizableByteArrayOutputStream.
Unlike ByteArrayOutputStream, this implementation is backed
by an ArrayDeque of byte[] instead of 1 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 buffer if its contents is extracted
with the writeTo(OutputStream) method.
- 从以下版本开始:
- 4.0 2021/8/21 01:18
- 作者:
- Craig Andrews, Juergen Hoeller, Harry Yang
- 另请参阅:
-
嵌套类概要
嵌套类修饰符和类型类说明private static final classAn implementation ofInputStreamthat reads from a givenFastByteArrayOutputStream.(专用程序包) static class -
字段概要
字段修饰符和类型字段说明private intprivate final ArrayDeque<byte[]>private booleanprivate static final intprivate intprivate final intprivate int -
构造器概要
构造器构造器说明Create a newFastByteArrayOutputStreamwith the default initial capacity of 256 bytes.FastByteArrayOutputStream(int initialBlockSize) Create a newFastByteArrayOutputStreamwith the specified initial capacity. -
方法概要
修饰符和类型方法说明private voidaddBuffer(int minCapacity) Create a new buffer and store it in the ArrayDeque.voidclose()Get anInputStreamto retrieve the data in this OutputStream.private static intnextPowerOf2(int val) Get the next power of 2 of a number (ex, the next power of 2 of 119 is 128).voidreset()Reset the contents of thisFastByteArrayOutputStream.voidresize(int targetCapacity) Resize the internal buffer size to a specified capacity.intsize()Return the number of bytes stored in thisFastByteArrayOutputStream.byte[]Creates a newly allocated byte array.byte[]Convert the stream's data to a byte array and return the byte array.toString()Convert the buffer's contents into a string decoding bytes using the platform's default character set.Convert this stream's contents to a string by decoding the bytes using the specifiedCharset.voidwrite(byte[] data, int offset, int length) voidwrite(int datum) voidwriteTo(OutputStream out) Write the buffers content to the given OutputStream.从类继承的方法 java.io.OutputStream
flush, nullOutputStream, write
-
字段详细资料
-
DEFAULT_BLOCK_SIZE
private static final int DEFAULT_BLOCK_SIZE- 另请参阅:
-
buffers
-
initialBlockSize
private final int initialBlockSize -
nextBlockSize
private int nextBlockSize -
alreadyBufferedSize
private int alreadyBufferedSize -
index
private int index -
closed
private boolean closed
-
-
构造器详细资料
-
FastByteArrayOutputStream
public FastByteArrayOutputStream()Create a newFastByteArrayOutputStreamwith the default initial capacity of 256 bytes. -
FastByteArrayOutputStream
public FastByteArrayOutputStream(int initialBlockSize) Create a newFastByteArrayOutputStreamwith the specified initial capacity.- 参数:
initialBlockSize- the initial buffer size in bytes
-
-
方法详细资料
-
write
- 指定者:
write在类中OutputStream- 抛出:
IOException
-
write
- 覆盖:
write在类中OutputStream- 抛出:
IOException
-
close
public void close()- 指定者:
close在接口中AutoCloseable- 指定者:
close在接口中Closeable- 覆盖:
close在类中OutputStream
-
toString
Convert the buffer's contents into a string decoding 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 buffer.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.
-
toString
Convert this stream's contents to a string by decoding the bytes using the specifiedCharset.- 参数:
charset- theCharsetto use to decode the bytes- 返回:
- a String decoded from this stream's contents
- 另请参阅:
-
size
public int size()Return the number of bytes stored in thisFastByteArrayOutputStream. -
toByteArrayUnsafe
public byte[] toByteArrayUnsafe()Convert the stream's data 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 made anyways, mind as well as use it. This approach also means that if this method is called twice without any writes in between, the second call is a no-op.
This method is "unsafe" as it returns the internal buffer. Callers should not modify the returned buffer.
- 返回:
- the current contents of this output stream, as a byte array.
- 另请参阅:
-
toByteArray
public byte[] toByteArray()Creates a newly allocated byte array.Its size is the current size of this output stream and the valid contents of the buffer have been copied into it.
- 返回:
- the current contents of this output stream, as a byte array.
- 另请参阅:
-
reset
public void reset()Reset the contents of thisFastByteArrayOutputStream.All currently accumulated output in the output stream is discarded. The output stream can be used again.
-
getInputStream
Get anInputStreamto retrieve the data in this OutputStream.Note that if any methods are called on the OutputStream (including, but not limited to, any of the write methods,
reset(),toByteArray(), andtoByteArrayUnsafe()) then theInputStream's behavior is undefined.- 返回:
InputStreamof the contents of this OutputStream
-
writeTo
Write the buffers content to the given OutputStream.- 参数:
out- the OutputStream to write to- 抛出:
IOException
-
resize
public void resize(int targetCapacity) Resize the internal buffer size to a specified capacity.- 参数:
targetCapacity- the desired size of the buffer- 抛出:
IllegalArgumentException- if the given capacity is smaller than the actual size of the content stored in the buffer already- 另请参阅:
-
addBuffer
private void addBuffer(int minCapacity) Create a new buffer and store it in the ArrayDeque.Adds a new buffer that can store at least
minCapacitybytes. -
nextPowerOf2
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).
-