Android API讲解之:ByteArrayOutputStream

来源:互联网 发布:便利店文化知乎 编辑:程序博客网 时间:2024/04/30 14:33

ByteArrayOutputStream继承自OutputStream,它的变量及方法包括:

ByteArrayOutputStream() 长度为32个字节public ByteArrayOutputStream(int size)可自定义长度size,用于字节超过32个字节时。public void close() throws IOException关闭输出流,可释放系统资源public synchronized void reset() 重置输出流,使内部字节为0public int size() 该输出流中的字节数public synchronized byte[] toByteArray() 将该输出流中内容转换为字节数组public String toString()转换为字符串public String toString(String charsetName) throws UnsupportedEncodingException转换为编码格式为charsetName的字符串public synchronized void write(byte[] buffer, int offset, int len)将buffer写入该输出流public synchronized void write(int oneByte)写入1个字节的数据public synchronized void writeTo(OutputStream out) throws IOException将该输出流写入out中上面所有的方法都很清晰明了,这里就仅说明下:public synchronized void write(byte[] buffer, int offset, int len),其详细代码为:

    @Override    public synchronized void write(byte[] buffer, int offset, int len) {        Arrays.checkOffsetAndCount(buffer.length, offset, len);        if (len == 0) {            return;        }        expand(len);        System.arraycopy(buffer, offset, buf, this.count, len);        this.count += len;    }
其中buffer为写入该OutputStream流的数据源;offset为buffer中数据拷贝的索引值,即从第几个数据开始复制;this.count为该OutputStream的长度,即数据添加的起始编号,len为写入数据的长度。




0 0
原创粉丝点击