您的位置:首页 > 移动开发 > Android开发

Android API讲解之:ByteArrayOutputStream

2016-04-04 21:30 681 查看
ByteArrayOutputStream继承自OutputStream,它的变量及方法包括:

ByteArrayOutputStream() 长度为32个字节
public ByteArrayOutputStream(int size)可自定义长度size,用于字节超过32个字节时。
public void close() throws IOException关闭输出流,可释放系统资源
public synchronized void reset() 重置输出流,使内部字节为0
public 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为写入数据的长度。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: