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 }

    欲为大树,何与草争;心若不动,风又奈何。
  • 相关阅读:
    设计模式中的多态——策略模式详解
    Spring IOC容器启动流程源码解析(一)——容器概念详解及源码初探
    并发包下常见的同步工具类详解(CountDownLatch,CyclicBarrier,Semaphore)
    HNOI2020游记
    旧年之末,新年伊始
    退役V次后做题记录
    PKUWC2020 游记
    CSP2019退役记
    CTS/APIO2019游记
    HNOI2019游记
  • 原文地址:https://www.cnblogs.com/admln/p/BufferedFSInputStream.html
Copyright © 2011-2022 走看看