ByteArrayInputStream类

一、说明

哈哈,这是学习Java之路的第一篇博文。虽然说接触学习Java有一段时间了,但是对流的概念一直并不是很清楚。也看了很多资料,但是感觉还是非常的抽象很难去理解。但是流又是Java中很重要的一部分,所以就决定花点时间好好弄一下。这篇文章也并非全部是我的原创文章,我浏览了写的比较好的博客,将其修改添加,加上一些自己的看法,就当成自己学习的笔记。仅供学习参考。参考原文链接

 

二、ByteArrayInputStream类

流的概念真的是挺令人困惑的。有一本书中说:流代表任何有能力产出数据的数据流对象,或者有能力接收数据的接收端对象开始也是不理解的,后来结合流类的jdk源码,测试实例仔细想一下,好像是这么回事。这个流的定义好在把抽象的流定义在了对象上。
再来说一下ByteArrayInputStream类。
ByteArrayInputStream 是字节数组输入流。它继承于InputStream。
它包含一个内部缓冲区,该缓冲区包含从流中读取的字节;通俗点说,它的内部缓冲区就是一个字节数组,而ByteArrayInputStream本质就是通过字节数组来实现的。
我们都知道,InputStream通过read()向外提供接口,供它们来读取字节数据;而ByteArrayInputStream 的内部额外的定义了一个计数器,它被用来跟踪 read() 方法要读取的下一个字节。
 

InputStream类

InputStream类函数列表

函数 返回值 功能
public int read()  throws IOEXception

返回下一个数据字节

(返回 0255 范围内的 int 字节值)

从输入流中读取数据的下一个字节
public int read(byte[] b)  throws IOEXception

 以整数形式返回实际读取的字节数。

如果因为已经到达流末尾而不再有数据可用,

则返回 -1

从输入流中读取一定数量的字节,

并将其存储在缓冲区数组 b

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

         throws IOEXception

 读入缓冲区的总字节数;

如果因为已到达流末尾而不再有数据可用,

则返回 -1

 将输入流中最多 len 个数据字节读入 byte 数
public long skip(long n)  throws IOEXception 跳过的实际字节数 跳过和丢弃此输入流中数据的 n 个字
public int available()  throws IOEXception 
             

可以不受阻塞地从此输入流读取

(或跳过)的估计字节数

返回此输入流下一个方法调用

可以不受阻塞地从此输入流读取

(或跳过)的估计字节数

public boolean markSupported()

如果此输入流实例支持 mark 和 reset 方法,

则返回 true;否则返回 false

测试此输入流是否支持 markreset 方法
public void mark(int readlimit) 在此输入流中标记当前的位置
public void reset() throws IOEXception

将此流重新定位到最后一次对此输入流

调用 mark 方法时的位置

public void close() throws IOEXception 关闭此输入流并释放与该流关联的所有系统资源

关于上表的解释:

1.可以这样理解read()函数里的“读取下一个字节”,就是光标在前

2.mark()方法的参数在实现中没有什么实际的意义。

3.InputStreamclose 方法不执行任何操作。

4.由于InputStream类是所以输入流的父类,方法的实现也就没有那么具体,它相当于给出了一个方法的规范,它的所以子类会按照这个规范去实现方法。也就是说,其子类方法的实现尽管不同,提供的功能却是相同的。

5.想了解更加详细的信息可以去查看jdk文档,是学习java很重要的工具。jdk文档

 

InputStream类源码分析

由于上述给出了此类函数比较详细的介绍,所以这里不作关于源码太多的说明。

 1 package java.io;
 2 
 3 public abstract class InputStream implements Closeable {
 4 
 5     // 能skip的大小
 6     private static final int MAX_SKIP_BUFFER_SIZE = 2048;
 7 
 8     // 从输入流中读取数据的下一个字节。
 9     public abstract int read() throws IOException;
10 
11     // 将数据从输入流读入 byte 数组。
12     public int read(byte b[]) throws IOException {
13         return read(b, 0, b.length);
14     }
15 
16     // 将最多 len 个数据字节从此输入流读入 byte 数组。
17     public int read(byte b[], int off, int len) throws IOException {
18         if (b == null) {
19             throw new NullPointerException();
20         } else if (off < 0 || len < 0 || len > b.length - off) {
21             throw new IndexOutOfBoundsException();
22         } else if (len == 0) {
23             return 0;
24         }
25 
26         int c = read();
27         if (c == -1) {
28             return -1;
29         }
30         b[off] = (byte)c;
31 
32         int i = 1;
33         try {
34             for (; i < len ; i++) {
35                 c = read();
36                 if (c == -1) {
37                     break;
38                 }
39                 b[off + i] = (byte)c;
40             }
41         } catch (IOException ee) {
42         }
43         return i;
44     }
45 
46     // 跳过输入流中的n个字节
47     public long skip(long n) throws IOException {
48 
49         long remaining = n;
50         int nr;
51 
52         if (n <= 0) {
53             return 0;
54         }
55 
56         int size = (int)Math.min(MAX_SKIP_BUFFER_SIZE, remaining);
57         byte[] skipBuffer = new byte[size];
58         while (remaining > 0) {
59             nr = read(skipBuffer, 0, (int)Math.min(size, remaining));
60             if (nr < 0) {
61                 break;
62             }
63             remaining -= nr;
64         }
65 
66         return n - remaining;
67     }
68 
69     public int available() throws IOException {
70         return 0;
71     }
72 
73     public void close() throws IOException {}
74 
75     public synchronized void mark(int readlimit) {}
76 
77     public synchronized void reset() throws IOException {
78         throw new IOException("mark/reset not supported");
79     }
80 
81     public boolean markSupported() {
82         return false;
83     }
84 }
View Code

说明:

1.Closeable 接口表示可以关闭的数据源或目标。这个接口只有一个方法close()。

ByteArrayInputStream类

如果还是觉得上面的内容理解起来还是很抽象,很难懂。没有关系,结合这个具体的InputStream类具体的子类ByteArrayInputStream类来看,会帮助你解开一部分困惑。计算机的学习就是这个样子,后面的知识需要前面的知识作为铺垫,前面的知识需要后面的知识才可以更深的理解。所以要前前后后的看一下,才可以真正的理解。

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

关闭 ByteArrayInputStream 无效。此类中的方法在关闭此流后仍可被调用,而不会产生任何 IOException

这是jdk文档中关于此类的描述。说明:

1.内部缓冲区指的是buf数组

2.内部计数器就是指字段pos,可以将其看成是光标

3.关闭 ByteArrayInputStream 无效指的是,关闭流的close()方法,在此类中时空方法

这里就不给出ByteArrayInputStream类的方法列表里,因为大体上和InputStream类方法列表的描述是一样的,只是方法的实现是不同的而已。

ByteArrayInputStream类源码分析

源码还是很有必要看一下的可以加强对此类的理解

  1 public class ByteArrayInputStream extends InputStream {
  2 
  3     // 保存字节输入流数据的字节数组
  4     protected byte buf[];
  5 
  6     // 下一个会被读取的字节的索引
  7     protected int pos;
  8 
  9     // 用于标记输入流当前位置,就是标记pos
 10     protected int mark = 0;
 11 
 12     // 字节流的长度
 13     protected int count;
 14 
 15     // 构造函数:创建一个内容为buf的字节流
 16     public ByteArrayInputStream(byte buf[]) {
 17         // 初始化“字节流对应的字节数组为buf”
 18         this.buf = buf;
 19         // 初始化“下一个要被读取的字节索引号为0”
 20         this.pos = 0;
 21         // 初始化“字节流的长度为buf的长度”
 22         this.count = buf.length;
 23     }
 24 
 25     // 构造函数:创建一个内容为buf的字节流,并且是从offset开始读取数据,读取的长度最多为length
 26     public ByteArrayInputStream(byte buf[], int offset, int length) {
 27         // 初始化“字节流对应的字节数组为buf”
 28         this.buf = buf;
 29         // 初始化“下一个要被读取的字节索引号为offset”
 30         this.pos = offset;
 31         // 初始化“字节流的长度”
 32         this.count = Math.min(offset + length, buf.length);
 33         // 初始化“标记的字节流读取位置”
 34         this.mark = offset;
 35     }
 36 
 37     // 读取下一个字节
 38     public synchronized int read() {
 39         return (pos < count) ? (buf[pos++] & 0xff) : -1;
 40     }
 41 
 42     // 将“字节流的数据写入到字节数组b中”
 43     // off是“字节数组b的偏移地址”,表示从数组b的off开始写入数据
 44     // len是“读到b数组中的字节最大长度”
 45     public synchronized int read(byte b[], int off, int len) {
 46         if (b == null) {
 47             throw new NullPointerException();
 48         } else if (off < 0 || len < 0 || len > b.length - off) {
 49             throw new IndexOutOfBoundsException();
 50         }
 51 
 52         if (pos >= count) {
 53             return -1;
 54         }
 55 
 56         int avail = count - pos;
 57         if (len > avail) {
 58             len = avail;
 59         }
 60         if (len <= 0) {
 61             return 0;
 62         }
 63         System.arraycopy(buf, pos, b, off, len);
 64         pos += len;
 65         return len;//从字节流中成功读出的字节数
 66     }
 67 
 68     // 跳过“字节流”中的n个字节。
 69     public synchronized long skip(long n) {
 70         long k = count - pos;
 71         if (n < k) {
 72             k = n < 0 ? 0 : n;
 73         }
 74 
 75         pos += k;
 76         return k;//成功跳过的字节数
 77     }
 78 
 79     // “能否读取字节流的下一个字节”
 80     public synchronized int available() {
 81         return count - pos;
 82     }
 83 
 84     // 是否支持“标签”
 85     public boolean markSupported() {
 86         return true;
 87     }
 88 
 89     // 保存当前位置。readAheadLimit在此处没有任何实际意义
 90     public void mark(int readAheadLimit) {
 91         mark = pos;
 92     }
 93 
 94     // 重置“字节流的读取索引”为“mark所标记的位置”
 95     public synchronized void reset() {
 96         pos = mark;
 97     }
 98 
 99     public void close() throws IOException {
100     }
101 }
View Code

 

真的是排版能力有限了。此博文仅作学习之用,欢迎指正。

 

posted @ 2019-06-03 15:07  卑微芒果  Views(6562)  Comments(0Edit  收藏  举报