java.io.ByteArrayOutputStream
2016-01-25 本文已影响0人
一氧化二氢有毒
ByteArrayOutputStream
ByteArrayOutputStream是一个字节输出流,其大小会随着写入的字节的多少自动改变。可以通过toByteArray() 和 toString()输出字节串或字符串
Constructor
构造器默认初始化一个大小为32的字节数组buf[] .若参数不为空则初始化一个size大小的数组。
public ByteArrayOutputStream() {
this(32);
}
public ByteArrayOutputStream(int size) {
if (size < 0) {
throw new IllegalArgumentException("Negative initial size: "
+ size);
}
buf = new byte[size];
}
Method
public synchronized void write(int b)
向输出流中写入一个字节,count代表输出流中的字节的大小
public synchronized void write(int b) {
ensureCapacity(count + 1);
buf[count] = (byte) b;
count += 1;
}
public synchronized void write(byte b[], int off, int len)
向输出流中写入字节数组b,off代表开始写入的位置,len代表写入的长度
public synchronized void write(byte b[], int off, int len) {
if ((off < 0) || (off > b.length) || (len < 0) ||
((off + len) - b.length > 0)) {
throw new IndexOutOfBoundsException();
}
ensureCapacity(count + len);
System.arraycopy(b, off, buf, count, len);
count += len;
}
public synchronized void writeTo(OutputStream out)
将当前输出流中的内容写到out流中
public synchronized void writeTo(OutputStream out) throws IOException {
out.write(buf, 0, count);
}
public synchronized void reset()
将输出流中的内容清空
public synchronized void reset() {
count = 0;
}
public synchronized byte toByteArray()[]
将输出流中的内容输出到一个字符数组中
public synchronized byte toByteArray()[] {
return Arrays.copyOf(buf, count);
}
public synchronized String toString()
将输出流中的内容输出到字符串中
public synchronized String toString() {
return new String(buf, 0, count);
}
public synchronized String toString(String charsetName)
将输出流中的内容输出到字符串中,以charsetName格式编码
public synchronized String toString(String charsetName)
throws UnsupportedEncodingException
{
return new String(buf, 0, count, charsetName);
}
public synchronized int size()
public synchronized int size() {
return count;
}