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 }

    欲为大树,何与草争;心若不动,风又奈何。
  • 相关阅读:
    [Jweb] JSP-编程 06, 内置对象
    [Jweb] Tomcat 解决编码, 乱码问题
    [Jweb] JSP-编程 05 JSP 使用 javabean
    [Jweb] JSP-编程 04 转向 jsp:forward 与 sendRedirect
    [Jweb] JSP-编程 03 静态, 动态包含
    [Jweb] JSP-编程 02 (Directive-include)
    [Jweb] JSP-编程 01 (Directive-page)
    [Jweb] JSP 编程 00 -Declaration- Scriptlet-表达式-Directive (推出原因 : Servlet写标签非常麻烦!)
    [Jweb] 数据库处理以及在 Servlet 中使用 Bean
    [Jweb] Application / TestServletContext.java
  • 原文地址:https://www.cnblogs.com/admln/p/BufferedFSInputStream.html
Copyright © 2011-2022 走看看