zoukankan      html  css  js  c++  java
  • org.apache.hadoop.fs-BufferedFSInputStream


    封装了FSInputStream


     1 package org.apache.hadoop.fs;
     2 
     3 import java.io.BufferedInputStream;
     4 import java.io.IOException;
     5 
     6 
     7 /**
     8  * A class optimizes reading from FSInputStream by bufferring
     9  */
    10 //通过缓存优化FSInputStream读取
    11 
    12 public class BufferedFSInputStream extends BufferedInputStream
    13 implements Seekable, PositionedReadable {
    14 //两个接口在前面刚看过了,功能为...
    15   /**
    16    * Creates a <code>BufferedFSInputStream</code>
    17    * with the specified buffer size,
    18    * and saves its  argument, the input stream
    19    * <code>in</code>, for later use.  An internal
    20    * buffer array of length  <code>size</code>
    21    * is created and stored in <code>buf</code>.
    22    *
    23    * @param   in     the underlying input stream.
    24    * @param   size   the buffer size.
    25    * @exception IllegalArgumentException if size <= 0.
    26    */
    27   public BufferedFSInputStream(FSInputStream in, int size) {
    28     super(in, size);
    29   }
    30 //通过跟踪父类代码知道对接了输入流“管道”,初始化了一个大小为size的buffer
    31   public long getPos() throws IOException {
    32     return ((FSInputStream)in).getPos()-(count-pos);
    33   }
    34 //返回现在的偏移量
    35   public long skip(long n) throws IOException {
    36     if (n <= 0) {
    37       return 0;
    38     }
    39 
    40     seek(getPos()+n);
    41     return n;
    42   }
    43 //跳过n长度后得到现偏移量
    44   public void seek(long pos) throws IOException {
    45     if( pos<0 ) {
    46       return;
    47     }
    48     // optimize: check if the pos is in the buffer
    49     long end = ((FSInputStream)in).getPos();
    50     long start = end - count;
    51     if( pos>=start && pos<end) {
    52       this.pos = (int)(pos-start);
    53       return;
    54     }
    55 
    56     // invalidate buffer
    57     this.pos = 0;
    58     this.count = 0;
    59 
    60     ((FSInputStream)in).seek(pos);
    61   }
    62 //实现了Seekable的seek方法
    63   public boolean seekToNewSource(long targetPos) throws IOException {
    64     pos = 0;
    65     count = 0;
    66     return ((FSInputStream)in).seekToNewSource(targetPos);
    67   }
    68 //.....
    69   public int read(long position, byte[] buffer, int offset, int length) throws IOException {
    70     return ((FSInputStream)in).read(position, buffer, offset, length) ;
    71   }
    72 
    73   public void readFully(long position, byte[] buffer, int offset, int length) throws IOException {
    74     ((FSInputStream)in).readFully(position, buffer, offset, length);
    75   }
    76 
    77   public void readFully(long position, byte[] buffer) throws IOException {
    78     ((FSInputStream)in).readFully(position, buffer);
    79   }
    80 }

    欲为大树,何与草争;心若不动,风又奈何。
  • 相关阅读:
    极简Docker和Kubernetes发展史
    什么是健身
    《高效休息法》IT从业者如何高效休息
    《我们赖以生存的隐喻》文学中的面向对象
    sequelize时间自动格式化
    什么是消息队列
    node.js中this指向失效解决
    node.js的async和await
    node.js箭头函数使用
    node.js如何批量赋值
  • 原文地址:https://www.cnblogs.com/admln/p/BufferedFSInputStream.html
Copyright © 2011-2022 走看看