Most visited

Recently visited

Added in API level 1

ByteArrayOutputStream

public class ByteArrayOutputStream
extends OutputStream

java.lang.Object
    java.io.OutputStream
      java.io.ByteArrayOutputStream


该类实现了将数据写入字节数组的输出流。 数据写入时缓冲区会自动增长。 数据可以使用toByteArray()和toString()进行检索。

关闭ByteArrayOutputStream不起作用。 在关闭流之后可以调用此类中的方法,而不生成IOException 。

摘要(Summary)

Fields

protected byte[] buf

数据存储的缓冲区。

protected int count

缓冲区中的有效字节数。

Public constructors

ByteArrayOutputStream()

创建一个新的字节数组输出流。

ByteArrayOutputStream(int size)

创建一个新的字节数组输出流,具有指定大小的缓冲区容量(以字节为单位)。

公共方法(Public methods)

void close()

关闭 ByteArrayOutputStream不起作用。

void reset()

将该字节数组输出流的 count字段重置为零,以便丢弃输出流中当前累积的所有输出。

int size()

返回缓冲区的当前大小。

byte[] toByteArray()

创建一个新分配的字节数组。

String toString(String charsetName)

通过使用指定的 charsetName解码字节,将缓冲区的内容转换为字符串。

String toString()

使用平台的默认字符集将缓冲区的内容转换为字符串解码字节。

String toString(int hibyte)

此方法在API级别1中已弃用。此方法未将字节正确转换为字符。 从JDK 1.1开始,执行此操作的首选方法是通过toString(String enc)方法(采用编码名称参数)或toString()方法(该方法使用平台的默认字符编码)。

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) 。

继承方法(Inherited methods)

From class java.io.OutputStream
From class java.lang.Object
From interface java.io.Closeable
From interface java.io.Flushable
From interface java.lang.AutoCloseable

Fields

buf

Added in API level 1
byte[] buf

数据存储的缓冲区。

count

Added in API level 1
int count

缓冲区中的有效字节数。

Public constructors

ByteArrayOutputStream

Added in API level 1
ByteArrayOutputStream ()

创建一个新的字节数组输出流。 缓冲区容量最初为32个字节,尽管其大小在必要时会增加。

ByteArrayOutputStream

Added in API level 1
ByteArrayOutputStream (int size)

创建一个新的字节数组输出流,具有指定大小的缓冲区容量(以字节为单位)。

参数(Parameters)
size int: the initial size.
抛出异常(Throws)
IllegalArgumentException if size is negative.

公共方法(Public methods)

close

Added in API level 1
void close ()

关闭ByteArrayOutputStream不起作用。 在关闭流之后可以调用此类中的方法,而不生成IOException 。

抛出异常(Throws)
IOException

reset

Added in API level 1
void reset ()

将该字节数组输出流的count字段重置为零,以便丢弃输出流中当前累积的所有输出。 输出流可以再次使用,重用已分配的缓冲区空间。

也可以看看:

size

Added in API level 1
int size ()

返回缓冲区的当前大小。

返回(Returns)
int the value of the count field, which is the number of valid bytes in this output stream.

也可以看看:

toByteArray

Added in API level 1
byte[] toByteArray ()

创建一个新分配的字节数组。 其大小是此输出流的当前大小,并且缓冲区的有效内容已被复制到其中。

返回(Returns)
byte[] the current contents of this output stream, as a byte array.

也可以看看:

toString

Added in API level 1
String toString (String charsetName)

使用指定的charsetName解码字节,将缓冲区的内容转换为字符串。 新的String的长度是字符集的函数,因此可能不等于字节数组的长度。

此方法始终用此字符集的默认替换字符串替换格式错误的输入和不可映射字符序列。 当需要对解码过程进行更多控制时,应该使用CharsetDecoder类。

参数(Parameters)
charsetName String: the name of a supported charset
返回(Returns)
String String decoded from the buffer's contents.
抛出异常(Throws)
UnsupportedEncodingException If the named charset is not supported

toString

Added in API level 1
String toString ()

使用平台的默认字符集将缓冲区的内容转换为字符串解码字节。 新的String的长度是字符集的函数,因此可能不等于缓冲区的大小。

此方法始终使用平台默认字符集的默认替换字符串替换格式错误的输入和不可映射字符序列。 当需要对解码过程进行更多的控制时,应该使用CharsetDecoder类。

返回(Returns)
String String decoded from the buffer's contents.

toString

Added in API level 1
String toString (int hibyte)

此方法在API级别1中已弃用。
此方法不能正确地将字节转换为字符。 从JDK 1.1开始,执行此操作的首选方法是通过toString(String enc)方法(采用编码名称参数)或toString()方法(该方法使用平台的默认字符编码)。

创建一个新分配的字符串。 其大小是输出流的当前大小,并且缓冲区的有效内容已被复制到其中。 结果字符串中的每个字符c都是从字节数组中的对应元素b构造而成的:

     c == (char)(((hibyte & 0xff) << 8) | (b & 0xff))
 

参数(Parameters)
hibyte int: the high byte of each resulting Unicode character.
返回(Returns)
String the current contents of the output stream, as a string.

也可以看看:

write

Added in API level 1
void write (byte[] b, 
                int off, 
                int len)

将 len字节从指定字节数组开始偏移 off写入此字节数组输出流。

参数(Parameters)
b byte: the data.
off int: the start offset in the data.
len int: the number of bytes to write.

write

Added in API level 1
void write (int b)

将指定的字节写入此字节数组输出流。

参数(Parameters)
b int: the byte to be written.

writeTo

Added in API level 1
void writeTo (OutputStream out)

将此字节数组输出流的完整内容写入指定的输出流参数,就像使用 out.write(buf, 0, count)调用输出流的写入方法 out.write(buf, 0, count) 。

参数(Parameters)
out OutputStream: the output stream to which to write the data.
抛出异常(Throws)
IOException if an I/O error occurs.

Hooray!