• 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 }

    欲为大树,何与草争;心若不动,风又奈何。
  • 相关阅读:
    VS.NET 2010 新功能 [整理]
    Sculpture 基于MDD的集成开发平台
    更换VS.NET 2010的皮肤 [Visual Studio Blog]
    SQL Server BI Step by Step SSIS 7 (End) 事务,错误输出,事件处理,日志记录
    和我一起学CSLA.NET设计模型及数据访问
    Visual Studio DSL 入门 2
    SQL Server BI Step by Step SSIS 5 通过Email发送查询结果
    SQL Server BI Step by Step SSRS 1Reporting Service 2008的新特性
    SQL Server BI Step by Step SSRS 2SQL Server 2008 Reporting Services实现匿名访问报表
    和我一起学CSLA.NET先忽悠下
  • 原文地址:https://www.cnblogs.com/admln/p/BufferedFSInputStream.html
Copyright © 2020-2023  润新知