-----------------------------------------------------------------------------------

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

相关文章: