加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java io包 ByteArrayInputStream&ByteArrayOutStream

发布时间:2020-12-15 08:01:42 所属栏目:Java 来源:网络整理
导读:package java.io; ? /** ?* A codeByteArrayInputStream/code contains ?* an internal buffer that contains bytes that ?* may be read from the stream. An internal ?* counter keeps track of the next byte to ?* be supplied by the coderead/code me

package java.io;

?

/**

?* A <code>ByteArrayInputStream</code> contains

?* an internal buffer that contains bytes that

?* may be read from the stream. An internal

?* counter keeps track of the next byte to

?* be supplied by the <code>read</code> method.

?* <p>

?* Closing a <tt>ByteArrayInputStream</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

?* @see???? java.io.StringBufferInputStream

?* @since?? JDK1.0

?*/

public class ByteArrayInputStream extends InputStream {

?

??? /**

???? * An array of bytes that was provided

???? * by the creator of the stream. Elements <code>buf[0]</code>

???? * through <code>buf[count-1]</code> are the

???? * only bytes that can ever be read from the

???? * stream;? element <code>buf[pos]</code> is

???? * the next byte to be read.

???? */

??? //输入流字节数组

protected byte buf[];

?

??? //输入流当前位置

??? protected int pos;

?

??? //输入流标记位置

??? protected int mark = 0;

?

??? //输入流可读取的最大位置加1

??? protected int count;

?

??? //构造函数

??? public ByteArrayInputStream(byte buf[]) {

??????? this.buf = buf;

??????? this.pos = 0;

??????? this.count = buf.length;

??? }

?

??? //构造函数

??? public ByteArrayInputStream(byte buf[],int offset,int length) {

??????? this.buf = buf;

??????? this.pos = offset;

??????? this.count = Math.min(offset + length,buf.length);

??????? this.mark = offset;

??? }

?

??? //从输入流读取下一个字节 返回该字节值(0~255)

??? //读到底了返回-1

??? public synchronized int read() {

??????? return (pos < count) ? (buf[pos++] & 0xff) : -1;

??? }

?

??? //从输入流中读取指定长度和位置的数组

??? public synchronized int read(byte b[],int off,int len) {

??????? if (b == null) {

??????????? throw new NullPointerException();

??????? } else if (off < 0 || len < 0 || len > b.length - off) {

??????????? throw new IndexOutOfBoundsException();

??????? }

?

??????? if (pos >= count) {

??????????? return -1;

??????? }

?

??????? int avail = count - pos;

??????? if (len > avail) {

??????????? len = avail;

??????? }

??????? if (len <= 0) {

??????????? return 0;

??????? }

??????? //native 方法

??????? System.arraycopy(buf,pos,b,off,len);

??????? pos += len;

??????? return len;

??? }

?

??? //跳过指定字节

??? public synchronized long skip(long n) {

??????? long k = count - pos;

??????? if (n < k) {

??????????? k = n < 0 ? 0 : n;

??????? }

?

??????? pos += k;

??????? return k;

??? }

?

??? //返回可读取的最大字节长度

??? public synchronized int available() {

??????? return count - pos;

??? }

?

??? public boolean markSupported() {

??????? return true;

??? }

???

??? public void mark(int readAheadLimit) {

??????? mark = pos;

??? }

??

??? public synchronized void reset() {

??????? pos = mark;

??? }

?

??? public void close() throws IOException {

??? }

?

}

?

?

分析ByteArrayOutputStream

package java.io;

?

import java.util.Arrays;

?

/**

?* 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

?*/

?

public class ByteArrayOutputStream extends OutputStream {

?

//输出流字节数组

??? protected byte buf[];

???

??? //输入流可读取的最大位置加1

??? protected int count;

?

??? //默认构造函数

??? 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 static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;

?

??? //数组扩容

??? private void grow(int minCapacity) {

??????? // overflow-conscious code

??????? int oldCapacity = buf.length;

??????? int newCapacity = oldCapacity << 1;

??????? if (newCapacity - minCapacity < 0)

??????????? newCapacity = minCapacity;

??????? if (newCapacity - MAX_ARRAY_SIZE > 0)

??????????? 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 len) {

??????? if ((off < 0) || (off > b.length) || (len < 0) ||

??????????? ((off + len) - b.length > 0)) {

??????????? throw new IndexOutOfBoundsException();

??????? }

??? ????ensureCapacity(count + len);

??????? System.arraycopy(b,buf,count,len);

??????? count += len;

??? }

?

??? //向其他输出流写入数据

??? public synchronized void writeTo(OutputStream out) throws IOException {

??????? out.write(buf,count);

??? }

?

??? public synchronized void reset() {

??????? count = 0;

??? }

???

??? //将输出流转换为字节数组

??? public synchronized byte toByteArray()[] {

??????? return Arrays.copyOf(buf,count);

??? }

?

??? //输出流大小

??? public synchronized int size() {

??????? return count;

??? }

?

??? //重写toString()

??? public synchronized String toString() {

??????? return new String(buf,count);

??? }

?

??? //根据编码格式将输出流转换为String

??? public synchronized String toString(String charsetName)

??????? throws UnsupportedEncodingException

??? {

?????? ?return new String(buf,charsetName);

??? }

?

??? //废弃

??? @Deprecated

??? public synchronized String toString(int hibyte) {

??????? return new String(buf,hibyte,count);

??? }

?

??? /**

???? * 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>.

???? */

??? public void close() throws IOException {

??? }

?

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读