Java BufferedReader相关源码实例分析

 更新时间:2020年10月29日 10:59:01   作者:Y_wee  
这篇文章主要介绍了Java BufferedReader相关源码实例分析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

1、案例代码

假设b.txt存储了abcdegfhijk

 public static void main(String[] args) throws IOException {
    //字符缓冲流
    BufferedReader bufferedReader=new BufferedReader(new FileReader
        (new File("H:\\ioText\\b.txt")),8);
    //存储读取的数据
    char[] charsRead=new char[5];
    //读取数据
    bufferedReader.read(charsRead);
    //遍历并输出charsRead
    for (char c:charsRead){
      System.out.println(c);
    }
  }

2、通过源码(部分)分析案例

a、第一次读取

public class BufferedReader extends Reader {
  private Reader in;//字符流
  private char cb[];//缓冲区
  private int nChars, nextChar;//nChars缓冲区可读字符数,nextChar下一个字符位置
  private static final int INVALIDATED = -2;
  private static final int UNMARKED = -1;
  private int markedChar = UNMARKED;
  private int readAheadLimit = 0; 
  private boolean skipLF = false;
  private boolean markedSkipLF = false;
  private static int defaultCharBufferSize = 8192;//缓冲区默认大小
  private static int defaultExpectedLineLength = 80;
  
  //案例调用的构造方法
   public BufferedReader(Reader in, int sz) {
     //调用父类构造
    super(in);
     //判断缓冲区大小是否正常
    if (sz <= 0)
      throw new IllegalArgumentException("Buffer size <= 0");
     //用户传入的字符流
    this.in = in;
    //给缓冲区指定空间大小(案例指定为8)
    cb = new char[sz];
     //缓冲区可读字符数和下一个字符位置初始化为0
    nextChar = nChars = 0;
  }
  
  //读取数据
  public int read(char cbuf[], int off, int len) throws IOException {
    synchronized (lock) {
      ensureOpen();
      if ((off < 0) || (off > cbuf.length) || (len < 0) ||
        ((off + len) > cbuf.length) || ((off + len) < 0)) {
        throw new IndexOutOfBoundsException();
      } else if (len == 0) {
        return 0;
      }
      //调用read1方法进行读取(真正读取数据的方法是read1方法)
      int n = read1(cbuf, off, len);
      if (n <= 0) return n;
      //将之前没处理完的数据复制到自定以数组charsRead再次调用read1方法读取
      while ((n < len) && in.ready()) {
        int n1 = read1(cbuf, off + n, len - n);
        if (n1 <= 0) break;
        n += n1;
      }
      return n;
    }
  }
  
  //cbuf用户自定义数组(charsRead),off=0,len=5
   private int read1(char[] cbuf, int off, int len) throws IOException {
    if (nextChar >= nChars) {//第一次读nextChar、nChars都为0,满足条件
      if (len >= cb.length && markedChar <= UNMARKED && !skipLF) {
        return in.read(cbuf, off, len);
      }
      //刷新缓冲区,先往下找到fill方法源码分析
      fill();
    }
    if (nextChar >= nChars) return -1;
    if (skipLF) {
      skipLF = false;
      if (cb[nextChar] == '\n') {
        nextChar++;
        if (nextChar >= nChars)
          fill();
        if (nextChar >= nChars)
          return -1;
      }
    }
    //执行完fill方法到这里,(len=5,nChars - nextChar=8-0)->n=5
    int n = Math.min(len, nChars - nextChar);
    //将缓冲区cb从nextChar开始复制n=5个字符到自定义数组
    System.arraycopy(cb, nextChar, cbuf, off, n);
    //nextChar=5
    nextChar += n;
    //n=5
    return n;
  }
  
  //刷新缓冲区方法
  private void fill() throws IOException {
    int dst;
    if (markedChar <= UNMARKED) {//markedChar初始值为UNMARKED,满足条件
      /* No mark */
      dst = 0;//初始化dst
    } else {
      /* Marked */
      int delta = nextChar - markedChar;
      if (delta >= readAheadLimit) {
        /* Gone past read-ahead limit: Invalidate mark */
        markedChar = INVALIDATED;
        readAheadLimit = 0;
        dst = 0;
      } else {
        if (readAheadLimit <= cb.length) {
          /* Shuffle in the current buffer */
          System.arraycopy(cb, markedChar, cb, 0, delta);
          markedChar = 0;
          dst = delta;
        } else {
          /* Reallocate buffer to accommodate read-ahead limit */
          char ncb[] = new char[readAheadLimit];
          System.arraycopy(cb, markedChar, ncb, 0, delta);
          cb = ncb;
          markedChar = 0;
          dst = delta;
        }
        nextChar = nChars = delta;
      }
    }
​
    int n;
    do {
      //dst=0,cb.length - dst=8-0->n=8
      n = in.read(cb, dst, cb.length - dst);
    } while (n == 0);
    if (n > 0) {//满足条件
      //nChars=8
      nChars = dst + n;
      //nextChar=0
      nextChar = dst;
    }
  }
  
}

第一次读取后charsRead存储了五个字符:abcde

b、第二次读取

//cbuf用户自定义数组(charsRead),off=0,len=5
   private int read1(char[] cbuf, int off, int len) throws IOException {
    if (nextChar >= nChars) {//第二次读nextChar=5、nChars=8,不满足条件
      if (len >= cb.length && markedChar <= UNMARKED && !skipLF) {
        return in.read(cbuf, off, len);
      }
      fill();
    }
    if (nextChar >= nChars) return -1;
    if (skipLF) {
      skipLF = false;
      if (cb[nextChar] == '\n') {
        nextChar++;
        if (nextChar >= nChars)
          fill();
        if (nextChar >= nChars)
          return -1;
      }
    }
    //跳过if直接到这里,len=5,nChars - nextChar=8-5=3->n=3
    int n = Math.min(len, nChars - nextChar);
    //将缓冲区cb从nextChar=5开始复制n=3个字符到自定义数组
    System.arraycopy(cb, nextChar, cbuf, off, n);
    //nextChar=5+3=8
    nextChar += n;
    //n=8
    return n;
  }

第二次读取只读了三个字符把charsRead五个字符的前三个覆盖:fghde

c、第三次读取

//cbuf用户自定义数组(charsRead),off=0,len=5
   private int read1(char[] cbuf, int off, int len) throws IOException {
    if (nextChar >= nChars) {//第三次读nextChar=8、nChars=8,满足条件
      if (len >= cb.length && markedChar <= UNMARKED && !skipLF) {
        return in.read(cbuf, off, len);
      }
      //刷新缓冲区,先往下找到fill方法源码分析
      fill();
    }
    if (nextChar >= nChars) return -1;
    if (skipLF) {
      skipLF = false;
      if (cb[nextChar] == '\n') {
        nextChar++;
        if (nextChar >= nChars)
          fill();
        if (nextChar >= nChars)
          return -1;
      }
    }
     //执行完fill方法到这里,(len=2,nChars - nextChar=8-0)->n=2
    int n = Math.min(len, nChars - nextChar);
    //将缓冲区cb从nextChar=0开始复制n=2个字符到自定义数组
    System.arraycopy(cb, nextChar, cbuf, off, n);
    //nextChar=5+3=8
    nextChar += n;
    //n=8
    return n;
  }
  
  //刷新缓冲区方法
  private void fill() throws IOException {
    int dst;
    if (markedChar <= UNMARKED) {//markedChar初始值为UNMARKED,满足条件
      /* No mark */
      dst = 0;//初始化dst
    } else {
      /* Marked */
      int delta = nextChar - markedChar;
      if (delta >= readAheadLimit) {
        /* Gone past read-ahead limit: Invalidate mark */
        markedChar = INVALIDATED;
        readAheadLimit = 0;
        dst = 0;
      } else {
        if (readAheadLimit <= cb.length) {
          /* Shuffle in the current buffer */
          System.arraycopy(cb, markedChar, cb, 0, delta);
          markedChar = 0;
          dst = delta;
        } else {
          /* Reallocate buffer to accommodate read-ahead limit */
          char ncb[] = new char[readAheadLimit];
          System.arraycopy(cb, markedChar, ncb, 0, delta);
          cb = ncb;
          markedChar = 0;
          dst = delta;
        }
        nextChar = nChars = delta;
      }
    }
​
    int n;
    do {
      //dst=0,cb.length - dst=8-0->n=8
      n = in.read(cb, dst, cb.length - dst);
    } while (n == 0);
    if (n > 0) {//满足条件
      //nChars=8
      nChars = dst + n;
      //nextChar=0
      nextChar = dst;
    }
  }
  
}

第三次读取了两个字符到charsRead,把最后两个字符覆盖:fghijk

3、源码执行过程图解

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • java的GUI实现简单切换界面

    java的GUI实现简单切换界面

    这篇文章主要为大家详细介绍了java的GUI实现简单切换界面,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-04-04
  • Spring Data JPA 实体类中常用注解说明

    Spring Data JPA 实体类中常用注解说明

    这篇文章主要介绍了Spring Data JPA 实体类中常用注解说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-11-11
  • 二叉排序树的实现与基本操作

    二叉排序树的实现与基本操作

    二叉排序树又称二叉查找树。本文主要对二叉排序树的实现与基本操作进行详细介绍,以下代码实现了:1、二叉树的构建;2、二叉树的中、前、后、层序遍历;3、二叉树中结点的最大距离。下面就跟着小编一起来看下吧
    2016-12-12
  • 详解SpringBoot简化配置分析总结

    详解SpringBoot简化配置分析总结

    这篇文章主要介绍了详解SpringBoot简化配置分析总结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-10-10
  • Springboot几种任务的整合方法

    Springboot几种任务的整合方法

    这篇文章主要介绍了Springboot几种任务的整合方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-10-10
  • Java正则验证电话,手机,邮箱,日期,金额的方法示例

    Java正则验证电话,手机,邮箱,日期,金额的方法示例

    这篇文章主要介绍了Java正则验证电话,手机,邮箱,日期,金额的方法,结合具体实例形式分析了Java针对电话,手机,邮箱,日期,金额的正则判定操作技巧,需要的朋友可以参考下
    2017-03-03
  • Java中关于char类型变量能够输出中文的问题

    Java中关于char类型变量能够输出中文的问题

    这篇文章主要介绍了Java中关于char类型变量能够输出中文的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-12-12
  • SpringBoot中使用拦截器拦截跳转登录的两种实现方法

    SpringBoot中使用拦截器拦截跳转登录的两种实现方法

    拦截器(Interceptor)是Spring框架提供的一种机制,用于在请求的生命周期中插入自定义逻辑,如身份验证、日志记录等,本文将详细介绍两种在SpringBoot中使用拦截器来控制用户登录并跳转到指定页面的方法,需要的朋友可以参考下
    2024-11-11
  • mybatis的动态SQL以及连接池详解

    mybatis的动态SQL以及连接池详解

    这篇文章主要介绍了mybatis的动态SQL以及连接池详解,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-02-02
  • 详解Java解析XML的四种方法

    详解Java解析XML的四种方法

    本篇文章主要介绍了java解析XML的几种方式,XML现在已经成为一种通用的数据交换格式,给数据集成与交互提供了方便,有需要的可以了解一下。
    2016-11-11

最新评论