一、简介

  BufferedInputStream会缓存一部分数据(默认8K),这个函数的作用就是读取更多的数据到缓存,必要的时候会扩大缓存的内容。

在该类中有几个重要的标志位:markpos,pos,count

  【markpos的作用,marklength区域内的数据表示需要保留的数据,也就是在重置(reset 方法)buffer的时候,这部分数据是不会被删除的,强制要求保留。】  

  pos:

    current position in the buffer, this is the index of the next character to be read from the buffer.

    表示下一个即将读入的字符。

  markpos:

    The value of the pos field at the time the last mark method was called.

    markpos 的数值等于上一次调用mark方法时候的pos位置。

     public synchronized void mark(int readlimit) {
marklimit = readlimit;
markpos = pos;
}

   count:

      可以使用的buffer

二、fill()函数

  当pos>count的时候,调用该方法来扩容。它总会将预留空间的位置挪动到buffer的前端。

 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 { /* grow buffer */
int nsz = pos * 2;
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;
}

    byte[] buffer = getBufIfOpen(); 获取当前buffer的引用

     if (markpos < 0)
              pos = 0;            /* no mark: throw away the buffer */ 整个buffer没有被标记过,也就是没有预留空间。

    else if (pos >= buffer.length)  /* no room left in buffer */   (markpos >0 或=0 )有预留空间,而且没有剩余空间可用。

      if (markpos > 0) {  /* can throw away early part of the buffer */  预留空间的起始位置不在buffer的开始位置
                  int sz = pos - markpos;    挪动预留空间
                  System.arraycopy(buffer, markpos, buffer, 0, sz);
                  pos = sz;
                  markpos = 0;

      } else if (buffer.length >= marklimit) { 如果marklimit的值小于缓存的长度,说明buffer很大,从内存使用的角度考虑,此时不宜再增大缓存的容量,

                         在这种情形下直接丢弃buf中的已有内容;
                  markpos = -1;   /* buffer got too big, invalidate mark */
                  pos = 0;        /* drop buffer contents */

     } else {            /* grow buffer */ 2倍扩展buffer
                  int nsz = pos * 2;
                  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;
            }

BufferedInputStream 源码分析的更多相关文章

  1. java.io.BufferedInputStream 源码分析

    BufferedInputStream是一个带缓冲区的输入流,在读取字节数据时可以从底层流中一次性读取多个字节到缓冲区,而不必每次读取操作都调用底层流,从而提高系统性能. 先介绍几个关键属性 //默认 ...

  2. 【Zookeeper】源码分析之持久化--FileSnap

    一.前言 前篇博文已经分析了FileTxnLog的源码,现在接着分析持久化中的FileSnap,其主要提供了快照相应的接口. 二.SnapShot源码分析 SnapShot是FileTxnLog的父类 ...

  3. 【Zookeeper】源码分析之持久化(二)之FileSnap

    一.前言 前篇博文已经分析了FileTxnLog的源码,现在接着分析持久化中的FileSnap,其主要提供了快照相应的接口. 二.SnapShot源码分析 SnapShot是FileTxnLog的父类 ...

  4. hadoop的RPC机制 -源码分析

    这些天一直奔波于长沙和武汉之间,忙着腾讯的笔试.面试,以至于对hadoop RPC(Remote Procedure Call Protocol ,远程过程调用协议,它是一种通过网络从远程计算机程序上 ...

  5. Spark大师之路:广播变量(Broadcast)源码分析

    概述 最近工作上忙死了……广播变量这一块其实早就看过了,一直没有贴出来. 本文基于Spark 1.0源码分析,主要探讨广播变量的初始化.创建.读取以及清除. 类关系 BroadcastManager类 ...

  6. TOMCAT8源码分析——SESSION管理分析(上)

    前言 对于广大java开发者而已,对于J2EE规范中的Session应该并不陌生,我们可以使用Session管理用户的会话信息,最常见的就是拿Session用来存放用户登录.身份.权限及状态等信息.对 ...

  7. Tomcat源码分析——Session管理分析(上)

    前言 对于广大java开发者而已,对于J2EE规范中的Session应该并不陌生,我们可以使用Session管理用户的会话信息,最常见的就是拿Session用来存放用户登录.身份.权限及状态等信息.对 ...

  8. Hadoop的RPC机制源码分析

    分析对象: hadoop版本:hadoop 0.20.203.0 必备技术点: 1. 动态代理(参考 :http://www.cnblogs.com/sh425/p/6893662.html )2. ...

  9. HDFS源码分析DataXceiver之整体流程

    在<HDFS源码分析之DataXceiverServer>一文中,我们了解到在DataNode中,有一个后台工作的线程DataXceiverServer.它被用于接收来自客户端或其他数据节 ...

随机推荐

  1. 制作一个vagrant的win7 box

    准备: 1.win7镜像文件 2.vagrant安装文件 3.virtual box安装文件 步骤: 1.先在本机上安装virtualbox和vagrant,本机为win7,安装虚机也为win7 2. ...

  2. poj 2417

    Accepted 8508K 391MS C++ 2004B 相比下边,,优化太多太多了... /** baby-step-giant-step 因为数据量太大,,自己写hash **/ #inclu ...

  3. C单链表实现

    /* * LinkNode.c * * Created on: Jan 14, 2014 * Author: root */ #include <stdlib.h> #include &l ...

  4. CodeIgniter结合Bootstrap

    CodeIgniter-Bootstrap结合了 cI和bootstrap的长处,一个专注于服务器端,一个专注于ui,这个把2个结合起来了.框架地址: http://www.andyhawthorne ...

  5. HDU 1544 Palindromes(回文子串)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1544 问题分析: 问题要求求出字符串的连续子串中的回文子串个数.首先,需要区分连续子串与子序列的区别. ...

  6. 非常可乐(bfs)

    非常可乐 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  7. Foundation Sorting: Shellsort

    /* Shell Sorting. * Implemention history:. * 2013-09-15, Mars Fu, first version. */ /* [Shell Sortin ...

  8. 编写可维护的JavaScript—语句和表达式&变量、函数和运算符

    语句和表达式 所有的块语句都应当使用花括号.包括: if for while do…while… try…catch…finally //不好的写法 if (condition) doSomethin ...

  9. ubuntu14.04LTS ruby on rails 开发环境

    小弟初学 Ruby,也没用过Linux. 在网上搜了好多关于开发环境的配置的文章,但总是和实际有点出入,找了N遍文章后,终于找到最简环境安装配置方法,分享下 推荐用 Ubuntu,感觉对于习惯用Win ...

  10. 关于RadUpload上传问题总结

    最近在开发上传控件,使用RadUpload上传大附件 发现了几个小问题,总结后分享给大家: 1.IE6浏览器下文件的路径显示的是物理路径,需要进行转换 2.IIS7.0 配置时要选择经典模式 3.we ...