public class FastByteArrayOutputStream extends OutputStream
可以通过toByteArray()
和 toString()
来获取数据
close()
方法无任何效果,当流被关闭后不会抛出IOException
这种设计避免重新分配内存块而是分配新增的缓冲区,缓冲区不会被GC,数据也不会被拷贝到其他缓冲区。
Constructor and Description |
---|
FastByteArrayOutputStream()
构造
|
FastByteArrayOutputStream(int size)
构造
|
Modifier and Type | Method and Description |
---|---|
void |
close()
此方法无任何效果,当流被关闭后不会抛出IOException
|
byte |
get(int index)
获取指定位置的字节
|
static FastByteArrayOutputStream |
of(InputStream in,
int limit)
根据输入流的总长度创建一个
FastByteArrayOutputStream 对象如果输入流的长度不确定,且 |
void |
reset()
复位
|
int |
size()
长度
|
byte[] |
toByteArray()
转为Byte数组
|
byte[] |
toByteArray(int start,
int len)
转为Byte数组
|
byte[] |
toByteArrayZeroCopyIfPossible()
转为Byte数组,如果缓冲区中的数据长度固定,则直接返回原始数组
注意此方法共享数组,不能修改数组内容! |
String |
toString() |
String |
toString(Charset charset)
转为字符串
|
void |
write(byte[] b,
int off,
int len) |
void |
write(int b) |
void |
writeTo(OutputStream out)
写出
|
flush, write
public FastByteArrayOutputStream()
public FastByteArrayOutputStream(int size)
size
- 预估大小public static FastByteArrayOutputStream of(InputStream in, int limit)
FastByteArrayOutputStream
对象in
- 输入流limit
- 限制大小FastByteArrayOutputStream
public void write(byte[] b, int off, int len)
write
in class OutputStream
public void write(int b)
write
in class OutputStream
public int size()
public void close()
close
in interface Closeable
close
in interface AutoCloseable
close
in class OutputStream
public void reset()
public void writeTo(OutputStream out) throws IORuntimeException
out
- 输出流IORuntimeException
- IO异常public byte[] toByteArray()
public byte[] toByteArray(int start, int len)
start
- 起始位置(包含)len
- 长度public byte[] toByteArrayZeroCopyIfPossible()
public String toString(Charset charset)
charset
- 编码,null表示默认编码public byte get(int index)
index
- 位置Copyright © 2025. All rights reserved.