模块  java.base
软件包  java.io

Class ByteArrayOutputStream

  • 实现的所有接口
    CloseableFlushableAutoCloseable

    public class ByteArrayOutputStream
    extends OutputStream
    此类实现一个输出流,其中数据被写入字节数组。 缓冲区会在数据写入时自动增长。 可以使用toByteArray()toString()检索数据。

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

    从以下版本开始:
    1.0
    • 字段汇总

      字段  
      变量和类型 字段 描述
      protected byte[] buf
      存储数据的缓冲区。
      protected int count
      缓冲区中的有效字节数。
    • 构造方法摘要

      构造方法  
      构造器 描述
      ByteArrayOutputStream()
      创建一个新的 ByteArrayOutputStream
      ByteArrayOutputStream​(int size)
      创建一个新的 ByteArrayOutputStream ,具有指定大小的缓冲区容量(以字节为单位)。
    • 方法摘要

      所有方法  实例方法 具体的方法  弃用的方法 
      变量和类型 方法 描述
      void close()
      关闭 ByteArrayOutputStream无效。
      void reset()
      将此 ByteArrayOutputStreamcount字段重置为零,以便丢弃输出流中当前累积的所有输出。
      int size()
      返回缓冲区的当前大小。
      byte[] toByteArray()
      创建一个新分配的字节数组。
      String toString()
      使用平台的默认字符集将缓冲区的内容转换为字符串解码字节。
      String toString​(int hibyte)
      已过时。
      此方法无法将字节正确转换为字符。
      String toString​(String charsetName)
      通过使用名为charset的字节解码将缓冲区的内容转换为字符串。
      String toString​(Charset charset)
      通过使用指定的charset解码字节,将缓冲区的内容转换为字符串。
      void write​(byte[] b, int off, int len)
      从偏移量为 off的指定字节数组写入 len字节到此 ByteArrayOutputStream
      void write​(int b)
      将指定的字节写入此 ByteArrayOutputStream
      void writeBytes​(byte[] b)
      将指定字节数组的完整内容写入此 ByteArrayOutputStream
      void writeTo​(OutputStream out)
      将此 ByteArrayOutputStream的完整内容写入指定的输出流参数,就像通过使用 out.write(buf, 0, count)调用输出流的write方法 out.write(buf, 0, count)
    • 字段详细信息

      • buf

        protected byte[] buf
        存储数据的缓冲区。
      • count

        protected int count
        缓冲区中的有效字节数。
    • 构造方法详细信息

      • ByteArrayOutputStream

        public ByteArrayOutputStream()
        创建一个新的ByteArrayOutputStream 缓冲区容量最初为32字节,但必要时其大小会增加。
      • ByteArrayOutputStream

        public ByteArrayOutputStream​(int size)
        创建一个新的 ByteArrayOutputStream ,其缓冲区容量为指定大小(以字节为单位)。
        参数
        size - 初始大小。
        异常
        IllegalArgumentException - 如果大小为负数。
    • 方法详细信息

      • write

        public void write​(int b)
        将指定的字节写入此 ByteArrayOutputStream
        Specified by:
        writeOutputStream
        参数
        b - 要写入的字节。
      • write

        public void write​(byte[] b,
                          int off,
                          int len)
        len字节从指定的字节数组(从偏移量 off开始)写入此 ByteArrayOutputStream
        重写:
        write在类 OutputStream
        参数
        b - 数据。
        off - 数据中的起始偏移量。
        len - 要写入的字节数。
        异常
        NullPointerException - 如果 bnull
        IndexOutOfBoundsException - 如果 off为负数, len为负数,或者 len为大于 b.length - off
      • writeBytes

        public void writeBytes​(byte[] b)
        将指定字节数组的完整内容写入此 ByteArrayOutputStream
        API Note:
        此方法相当于 write(b, 0, b.length)
        参数
        b - 数据。
        异常
        NullPointerException - 如果 bnull
        从以下版本开始:
        11
      • writeTo

        public void writeTo​(OutputStream out)
                     throws IOException
        将此 ByteArrayOutputStream的完整内容写入指定的输出流参数,就像通过使用 out.write(buf, 0, count)调用输出流的write方法 out.write(buf, 0, count)
        参数
        out - 要写入数据的输出流。
        异常
        NullPointerException - 如果 outnull
        IOException - 如果发生I / O错误。
      • reset

        public void reset()
        将此ByteArrayOutputStreamcount字段重置为零,以便丢弃输出流中当前累积的所有输出。 可以再次使用输出流,重用已分配的缓冲区空间。
        另请参见:
        ByteArrayInputStream.count
      • toByteArray

        public byte[] toByteArray()
        创建一个新分配的字节数组。 它的大小是此输出流的当前大小,缓冲区的有效内容已复制到其中。
        结果
        此输出流的当前内容,作为字节数组。
        另请参见:
        size()
      • size

        public int size()
        返回缓冲区的当前大小。
        结果
        count字段的值,它是此输出流中的有效字节数。
        另请参见:
        count
      • toString

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

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

        重写:
        toString在类 Object
        结果
        从缓冲区内容解码的字符串。
        从以下版本开始:
        1.1
      • toString

        public String toString​(String charsetName)
                        throws UnsupportedEncodingException
        通过使用名为charset的字节解码将缓冲区的内容转换为字符串。

        此方法相当于#toString(charset) ,需要charset

        调用此方法的形式

           ByteArrayOutputStream b = ... b.toString("UTF-8")  
        行为与表达式完全相同
           ByteArrayOutputStream b = ... b.toString(StandardCharsets.UTF_8)  
        参数
        charsetName - 支持的charset的名称
        结果
        从缓冲区内容解码的字符串。
        异常
        UnsupportedEncodingException - 如果不支持指定的字符集
        从以下版本开始:
        1.1
      • toString

        public String toString​(Charset charset)
        通过使用指定的charset解码字节,将缓冲区的内容转换为字符串。 String的长度是字符集的函数,因此可能不等于字节数组的长度。

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

        参数
        charset -该charset被用来解码bytes
        结果
        从缓冲区内容解码的字符串。
        从以下版本开始:
        10
      • toString

        @Deprecated
        public String toString​(int hibyte)
        Deprecated.
        This method does not properly convert bytes into characters. As of JDK 1.1, the preferred way to do this is via the toString(String charsetName) or toString(Charset charset) method, which takes an encoding-name or charset argument, or the toString() method, which uses the platform's default character encoding.
        创建一个新分配的字符串。 它的大小是输出流的当前大小,缓冲区的有效内容已复制到其中。 结果字符串中的每个字符c都是从字节数组中的相应元素b构造的,这样:
        
             c == (char)(((hibyte & 0xff) << 8) | (b & 0xff))
         
        参数
        hibyte - 每个生成的Unicode字符的高字节。
        结果
        输出流的当前内容,作为字符串。
        另请参见:
        size()toString(String)toString()