public class ByteArrayOutputStream extends OutputStream
toByteArray()和toString() 。
关闭ByteArrayOutputStream没有任何效果。 该流中的方法可以在流关闭后调用,而不生成IOException 。
| Modifier and Type | Field and Description |
|---|---|
protected byte[] |
buf
存储数据的缓冲区。
|
protected int |
count
缓冲区中有效字节的数量。
|
| Constructor and Description |
|---|
ByteArrayOutputStream()
创建一个新的字节数组输出流。
|
ByteArrayOutputStream(int size)
创建一个新的字节数组输出流,具有指定大小的缓冲区容量(以字节为单位)。
|
| Modifier and Type | Method and Description |
|---|---|
void |
close()
关闭
ByteArrayOutputStream没有任何效果。
|
void |
reset()
将此字节数组输出流的
count字段重置为零,以便丢弃输出流中当前累积的所有输出。
|
int |
size()
返回缓冲区的当前大小。
|
byte[] |
toByteArray()
创建一个新分配的字节数组。
|
String |
toString()
使用平台的默认字符集将缓冲区内容转换为字符串解码字节。
|
String |
toString(int hibyte)
已弃用
此方法无法将字节正确转换为字符。
从JDK 1.1的,要做到这一点的优选方法是通过
toString(String enc)方法,它需要一个编码名称参数,或toString()方法,它使用平台的默认字符编码。
|
String |
toString(String charsetName)
通过使用命名的 charset解码字节,将缓冲区的内容转换为字符串。
|
void |
write(byte[] b, int off, int len)
从指定的字节数组写入
len字节,从偏移量为
off开始,输出到这个字节数组输出流。
|
void |
write(int b)
将指定的字节写入此字节数组输出流。
|
void |
writeTo(OutputStream out)
将此字节数组输出流的完整内容写入指定的输出流参数,就像使用
out.write(buf, 0, count)调用输出流的写入方法
out.write(buf, 0, count) 。
|
flush, writepublic ByteArrayOutputStream()
public ByteArrayOutputStream(int size)
size - 初始尺寸。
IllegalArgumentException - 如果大小为负数。
public void write(int b)
write在
OutputStream
b - 要写入的字节。
public void write(byte[] b,
int off,
int len)
len从指定的字节数组以偏移字节
off该字节数组输出流。
write在类别
OutputStream
b - 数据。
off - 数据中的起始偏移量。
len - 要写入的字节数。
public void writeTo(OutputStream out) throws IOException
out.write(buf, 0, count)调用输出流的写入方法
out.write(buf, 0, count) 。
out - 要写入数据的输出流。
IOException - 如果发生I / O错误。
public void reset()
count字段重置为零,以便丢弃输出流中当前累积的所有输出。
可以再次使用输出流,重用已经分配的缓冲区空间。
public byte[] toByteArray()
size()
public int size()
count字段的值,这是此输出流中有效字节的数量。
count
public String toString()
该方法总是使用默认替换字符串替换格式错误的输入和不可映射的字符序列,用于平台的默认字符集。 当需要更多的解码过程控制时,应使用CharsetDecoder类。
public String toString(String charsetName) throws UnsupportedEncodingException
charset解码字节将缓冲区的内容转换为字符串。
新的String的长度是字符集的函数,因此可能不等于字节数组的长度。
此方法总是用此字符集的默认替换字符串替换格式错误的输入和不可映射字符序列。 当需要更多的解码过程控制时,应使用CharsetDecoder类。
charsetName - 支持的名称charset
UnsupportedEncodingException - 如果不支持命名的字符集
@Deprecated public String toString(int hibyte)
toString(String enc)方法,它需要一个编码名称参数,或toString()方法,它使用平台的默认字符编码。
c == (char)(((hibyte & 0xff) << 8) | (b & 0xff))
hibyte - 每个生成的Unicode字符的高字节。
size() ,
toString(String) ,
toString()
public void close()
throws IOException
close在界面
Closeable
close在界面
AutoCloseable
close在
OutputStream
IOException - 如果发生I / O错误。
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.