Java 日看一类(7)之IO包中的ByteArrayOutputStream类
2018-02-16 19:09
357 查看
所引入的包和继承关系:
引入java.util.Arrays文件
继承自OutputStream类
类的作用注释:
/**
* This class implements an output stream in which the data is
* written into a byte array. The buffer automatically grows as data
* is written to it.
* The data can be retrieved using <code>toByteArray()</code> and
* <code>toString()</code>.
* <p>
* Closing a <tt>ByteArrayOutputStream</tt> has no effect. The methods in
* this class can be called after the stream has been closed without
* generating an <tt>IOException</tt>.
*
* @author Arthur van Hoff
* @since JDK1.0
*/大意如下:这个类是一个输出流接口,会把数据写入到byte数组中。
缓冲区会自动增长,添加数据。
数据可以通过“toByteArray”和“toString”方法进行格式转换
关闭ByteArrayOutputStream是无效的。
close方法在这个类中只能在流已经被关闭后调用(不抛出IO异常)
这个类含有三个成员变量:
缓冲区:protected byte buf[];缓冲区有效长度:protected int count;最大数组长度:private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
含有十五个方法:
构造函数(默认大小缓冲区):public ByteArrayOutputStream() {
this(32);
}构造函数(构造特定大小缓冲区):public ByteArrayOutputStream(int size) {
if (size < 0) {
throw new IllegalArgumentException("Negative initial size: "
+ size);
}
buf = new byte[size];
}确认缓冲区容量是否够用:private void ensureCapacity(int minCapacity) {
// overflow-conscious code
if (minCapacity - buf.length > 0)//不够就增加缓冲区大小
grow(minCapacity);
}增大缓冲区容量:private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = buf.length;
int newCapacity = oldCapacity << 1;//新容量为旧容量2倍
if (newCapacity - minCapacity < 0)//新容量小于最小需要容量
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)//新容量超出系统数组所能容纳最大值(Int型)
newCapacity = hugeCapacity(minCapacity);
buf = Arrays.copyOf(buf, newCapacity);//引入包内容
}大容量判定(返回一个合适的容量值):private static int hugeCapacity(int minCapacity) {
if (minCapacity < 0) // overflow
throw new OutOfMemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}写入缓冲区(单个值):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) {
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) throws IOException {
out.write(buf, 0, count);
}重置缓冲区:public synchronized void reset() {
count = 0;
}检查缓冲区内容(以Byte数组形式):public synchronized byte toByteArray()[] {
return Arrays.copyOf(buf, count);
}获得当前缓冲区有效大小:public synchronized int size() {
return count;
}检查缓冲区内容(以字符串形式):public synchronized String toString() {
return new String(buf, 0, count);
}检查缓冲区内容(字符串形式,特定字符集编码):public synchronized String toString(String charsetName)
throws UnsupportedEncodingException
{
return new String(buf, 0, count, charsetName);
}检查缓冲区内容(字符串形式,该String构造函数早已过时,我也不知道这个方法现在有啥用):public synchronized String toString(int hibyte) {
return new String(buf, hibyte, 0, count);
}
(作者目前使用的JDK1.8)
关闭流(没啥用,和ByteArrayInputStream类相同):public void close() throws IOException {
}
ByteArrayOutputStream仍是比ByteArrayInputStream简单,但相对于一般的OutputStream,由于其加入了缓冲区检测机制,所以功能会更为强大,准确率也更高。
引入java.util.Arrays文件
继承自OutputStream类
类的作用注释:
/**
* This class implements an output stream in which the data is
* written into a byte array. The buffer automatically grows as data
* is written to it.
* The data can be retrieved using <code>toByteArray()</code> and
* <code>toString()</code>.
* <p>
* Closing a <tt>ByteArrayOutputStream</tt> has no effect. The methods in
* this class can be called after the stream has been closed without
* generating an <tt>IOException</tt>.
*
* @author Arthur van Hoff
* @since JDK1.0
*/大意如下:这个类是一个输出流接口,会把数据写入到byte数组中。
缓冲区会自动增长,添加数据。
数据可以通过“toByteArray”和“toString”方法进行格式转换
关闭ByteArrayOutputStream是无效的。
close方法在这个类中只能在流已经被关闭后调用(不抛出IO异常)
这个类含有三个成员变量:
缓冲区:protected byte buf[];缓冲区有效长度:protected int count;最大数组长度:private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
含有十五个方法:
构造函数(默认大小缓冲区):public ByteArrayOutputStream() {
this(32);
}构造函数(构造特定大小缓冲区):public ByteArrayOutputStream(int size) {
if (size < 0) {
throw new IllegalArgumentException("Negative initial size: "
+ size);
}
buf = new byte[size];
}确认缓冲区容量是否够用:private void ensureCapacity(int minCapacity) {
// overflow-conscious code
if (minCapacity - buf.length > 0)//不够就增加缓冲区大小
grow(minCapacity);
}增大缓冲区容量:private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = buf.length;
int newCapacity = oldCapacity << 1;//新容量为旧容量2倍
if (newCapacity - minCapacity < 0)//新容量小于最小需要容量
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)//新容量超出系统数组所能容纳最大值(Int型)
newCapacity = hugeCapacity(minCapacity);
buf = Arrays.copyOf(buf, newCapacity);//引入包内容
}大容量判定(返回一个合适的容量值):private static int hugeCapacity(int minCapacity) {
if (minCapacity < 0) // overflow
throw new OutOfMemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}写入缓冲区(单个值):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) {
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) throws IOException {
out.write(buf, 0, count);
}重置缓冲区:public synchronized void reset() {
count = 0;
}检查缓冲区内容(以Byte数组形式):public synchronized byte toByteArray()[] {
return Arrays.copyOf(buf, count);
}获得当前缓冲区有效大小:public synchronized int size() {
return count;
}检查缓冲区内容(以字符串形式):public synchronized String toString() {
return new String(buf, 0, count);
}检查缓冲区内容(字符串形式,特定字符集编码):public synchronized String toString(String charsetName)
throws UnsupportedEncodingException
{
return new String(buf, 0, count, charsetName);
}检查缓冲区内容(字符串形式,该String构造函数早已过时,我也不知道这个方法现在有啥用):public synchronized String toString(int hibyte) {
return new String(buf, hibyte, 0, count);
}
(作者目前使用的JDK1.8)
关闭流(没啥用,和ByteArrayInputStream类相同):public void close() throws IOException {
}
ByteArrayOutputStream仍是比ByteArrayInputStream简单,但相对于一般的OutputStream,由于其加入了缓冲区检测机制,所以功能会更为强大,准确率也更高。
相关文章推荐
- Java 日看一类(21)之IO包中的FileReader类
- Java 日看一类(9)之IO包中的CharConversionException类和Console类
- Java 日看一类(28)之IO包中的Flushable接口和InputStream类
- Java 日看一类(20)之IO包中的FilePermission类与FilePermissionCollection类
- Java 日看一类(22)之IO包中的FileSystem类(抽象类
- Java 日看一类(5)之IO包中的BufferedWriter类
- Java 日看一类(19)之IO包中的FileOutputStream类
- Java 日看一类(16) 之IO包中的FileDescriptor类
- Java 日看一类(3)之IO包中的BufferedOutputStream类
- Java 日看一类(29)之IO包中的InputStreamReader类
- Java 日看一类(34)之IO包中的NotActiveException和NotSerializableException
- Java 日看一类(8)之IO包中的Ch 4000 arArrayReader类
- Java 日看一类(1)之IO包中的Bits
- Java 日看一类(13)之IO包中的DefaultFileSystem类和DeleteOnExitHook类
- Java 日看一类(6)之IO包中的ByteArrayInputStream类
- Java 日看一类(23)之IO包中的FileWriter类
- Java 日看一类(25)之IO包中的FilterOutputStream类
- Java 日看一类(4)之IO包中的BufferedReader类
- Java 日看一类(30)之IO包中的InterruptedIOException、InvalidObjectException异常类
- Java 日看一类(35)之IO包中的ObjectInput接口