返回 登录
0

Java字节流:ByteArrayInputStream ByteArrayOutputStream

ByteArrayInputStream

类声明: public class ByteArrayInputStream extends InputStream

位于java.io包下

官方对其说明:

A ByteArrayInputStream 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 read method.

(简单翻译:ByteArrayInputStream包含一个内部缓冲区,该缓冲区包含从流中读取的字节数据。内部计数器跟踪read方法要提供的下一个字节)

Closing a ByteArrayInputStream has no effect. The methods in this class can be called after the stream has been closed without generating an IOException.

(简单翻译:关闭ByteArrayInputStream无效,此类中的方法在关闭此流后仍可被调用,而不会产生IOException)

主要字段 :

protected byte[] buf: 存储输入流中的字节数组

protected int count: 输入流中字节的个数

protected int mark: 流中当前的标记位置

protected int pos: 要从输入流缓冲区中读取的下一个字节的索引

构造方法 :

ByteArrayInputStream(byte[] buf);

创建一个ByteArrayInputStream实例,使用字节数组buf作为其缓冲区数组。

ByteArrayInputStream(byte[] buf,int offset,int length);

创建一个ByteArrayInputStream实例,使用字节数组buf从offset开始的len个字节作为其缓冲区数组。

主要方法:

  • int available(): 输入流中可读取的字节个数

  • void close(): 关闭此输入流并释放与该流有关的系统资源.

  • void mark(int readlimit): 在此输入流中标记当前的位置.

  • boolean markSupported(): 检测此输入流是否支持mark和reset.

  • int read(): 从输入流中读取下一个字节数据 .

  • int read(byte[] b,int off,int len): 从输入流中读取len个字节,并将其存储在字节数组b中off位置开始的地方

  • void reset(): 将此流重新定位到最后一次对此输入流调用mark方法时的位置.

  • long skip(long n): 跳过和丢弃此输入流中n个字节的数据.

通过实例演示查看源代码:

通过构造方法ByteArrayInputStream(byte[] buf)来创建一个ByteArrayInputStream类的实例

byte[] bytes = new byte[]{‘a’,’b’,’c’,’d’,’e’,’f’,’g’,’h’,’j’,’k’};

ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);

查看ByteArrayInputStream(byte[] buf)构造方法的源代码:


1 public ByteArrayInputStream(byte buf[]) {
2         this.buf = buf;
3         this.pos = 0;
4         this.count = buf.length;
5     }

View Code

此时ByteArrayInputStream实例在内存中的情况如下图,可以看出构造函数的参数 byte buf[]就是输入流中的数据。

实例byteArrayInputStream中的属性 count = 10、mark = 0、pos = 0;
图片描述

下面来看看具体的方法

(1)int available()方法

功能: 返回输入流中还能够被读取的字节个数

源代码如下:

 1     /**
 2      * Returns the number of remaining bytes that can be read (or skipped over)
 3      * from this input stream.
 4      * <p>
 5      * The value returned is <code>count - pos</code>,
 6      * which is the number of bytes remaining to be read from the input buffer.
 7      *
 8      * @return  the number of remaining bytes that can be read (or skipped
 9      *          over) from this input stream without blocking.
10      */
11     public synchronized int available() {
12         return count - pos;
13     }

View Code

执行:byteArrayInputStream.available()方法 返回值是10。(available()方法的返回值会随着pos的增大而变小)

(2)void close()方法

功能: 关闭输入流(但ByteArrayInputStream此方法无效)

源代码如下:

1     /**
2      * Closing a <tt>ByteArrayInputStream</tt> has no effect. The methods in
3      * this class can be called after the stream has been closed without
4      * generating an <tt>IOException</tt>.
5      * <p>
6      */
7     public void close() throws IOException {
8     }

View Code

执行:byteArrayInputStream.close()方法 没有任何的效果,因为源代码中方法体没有任何的代码.

(3)int read()方法

功能: 从输入流中读取下一个字节数据

源代码如下:

 1     /**
 2      * Reads the next byte of data from this input stream. The value
 3      * byte is returned as an <code>int</code> in the range
 4      * <code>0</code> to <code>255</code>. If no byte is available
 5      * because the end of the stream has been reached, the value
 6      * <code>-1</code> is returned.
 7      * <p>
 8      * This <code>read</code> method
 9      * cannot block.
10      *
11      * @return  the next byte of data, or <code>-1</code> if the end of the
12      *          stream has been reached.
13      */
14     public synchronized int read() {
15         return (pos < count) ? (buf[pos++] & 0xff) : -1;
16     }

View Code
评论