BufferedInputStream:

public synchronized int read() throws IOException

int res=bis.read();

System.out.println((char)res)

  调用一次,取一次值,并游标向前走一位;返回值为unicode

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

 byte[] buff=new byte[LEN];

 int res=bis.read(buff,0,LEN);

 System.out.println(new String(buff,0,res));

    调用一次 取LEN个值,并游标向前走LEN位,数据保存在字节数组中,返回值是数据个数

public synchronized int available() throws IOException

  返回目标文件大小, 底层实现​

 public synchronized void mark(int readlimit)
public synchronized void reset() throws IOException

  mark用来标记断点(下标)并传入一个readlimit (缓冲流最大限制)

reset 将返回到断点重新读取数据

根据源码解读:


  public synchronized void mark(int readlimit) {
marklimit = readlimit;
markpos = pos;
} public synchronized void reset() throws IOException {
getBufIfOpen(); // Cause exception if closed
if (markpos < 0)
throw new IOException("Resetting to invalid mark");
pos = markpos;
}

  

 private void fill() throws IOException {
byte[] buffer = getBufIfOpen();
if (markpos < 0)
pos = 0; /* no mark: throw away the buffer */
else if (pos >= buffer.length) /* no room left in buffer */
if (markpos > 0) { /* can throw away early part of the buffer */
int sz = pos - markpos;
System.arraycopy(buffer, markpos, buffer, 0, sz);
pos = sz;
markpos = 0;
} else if (buffer.length >= marklimit) {
markpos = -1; /* buffer got too big, invalidate mark */
pos = 0; /* drop buffer contents */
} else if (buffer.length >= MAX_BUFFER_SIZE) {
throw new OutOfMemoryError("Required array size too large");
} else { /* grow buffer */
int nsz = (pos <= MAX_BUFFER_SIZE - pos) ?
pos * 2 : MAX_BUFFER_SIZE;
if (nsz > marklimit)
nsz = marklimit;
byte nbuf[] = new byte[nsz];
System.arraycopy(buffer, 0, nbuf, 0, pos);
if (!bufUpdater.compareAndSet(this, buffer, nbuf)) {
// Can't replace buf if there was an async close.
// Note: This would need to be changed if fill()
// is ever made accessible to multiple threads.
// But for now, the only way CAS can fail is via close.
// assert buf == null;
throw new IOException("Stream closed");
}
buffer = nbuf;
}
count = pos;
int n = getInIfOpen().read(buffer, pos, buffer.length - pos);
if (n > 0)
count = n + pos;
}

  

1、调用mark 并传入int类型参数赋值给marklimit,同时将pos赋值给markpos;

2、在读取数据过程中,在没调用reset前,pos会持续自增;

3、 在缓冲区中数据读取完后,会再次调用fill方法,使缓冲区扩大一倍,然后pos继续自增

接下来有三种运行路线

1、缓冲区大小超过marklimit时,调用reset时,会抛异常,因为markpos=-1;

2、缓冲区大小超过最大缓冲区大小,直接抛异常

3、缓冲区大小没有超过marklimit,调用reset,缓冲区大小回到初始值大小,将markpos复制给pos,返回到标记点,重新读取数据;

还有查看源码发现,如果缓冲区大小没有超过marklimit,,那么这个标记一直存在,知道缓冲close;没有找到去掉标记的方法;

BufferedOutputStream:

 public synchronized void write(int b) throws IOException 

 public synchronized void write(byte b[], int off, int len) throws IOException  

 public synchronized void flush() throws IOException

 private void flushBuffer() throws IOException

  

最新文章

  1. Ubuntu下配置apache开启https
  2. SAP SMARTFORM 记录实际打印次数
  3. Android开发笔记
  4. atitit.信息安全的控制总结o7
  5. 简述Mesos API–files
  6. linux系统识别和挂载文件系统
  7. NPOI--操作Excel之利器(二)
  8. AndroidHttp通信 HTTP Client与HttpURLConnection的区别
  9. zepto的touch.js左右滑动存在一些问题,用百度的touch.js代替
  10. 第一个python爬虫程序
  11. Java Web入门学习(四)Eclipse与Maven、Tomcat整合配置
  12. 安卓巴士android源码、博文精选1
  13. javascript数组去重的3种方法
  14. [Swift]LeetCode328. 奇偶链表 | Odd Even Linked List
  15. Elasticsearch安装部署教程
  16. LOJ#6285. 数列分块入门 9
  17. 更改linux终端中用户名颜色
  18. jQuery .on() and .off() 命名空间
  19. cvc-complex-type.2.4.a: Invalid content was found starting with element &#39;async-supported&#39;. One of &#39;{&quot;http://java.sun.com/xml/ns/javaee&quot;:init-param}&#39; is expected.
  20. android中RecyclerView控件的列表项横向排列

热门文章

  1. redis介绍及在购物车项目中的应用,用户认证
  2. [Oracle]分区索引
  3. 冒泡排序_C语言_数组
  4. 【前行&amp;赛时总结】◇第2站&amp;赛时&#183;8◇ Atcoder ABC-109
  5. Linux入门-第五周
  6. 老刘的Linux小课堂开课了
  7. react脚手架搭建1
  8. 前后端不分离部署教程(基于Vue,Nginx)
  9. 日志管理——rsyslog
  10. python函数的返回值