封装了FSInputStream


 package org.apache.hadoop.fs;

 import java.io.BufferedInputStream;
import java.io.IOException; /**
* A class optimizes reading from FSInputStream by bufferring
*/
//通过缓存优化FSInputStream读取 public class BufferedFSInputStream extends BufferedInputStream
implements Seekable, PositionedReadable {
//两个接口在前面刚看过了,功能为...
/**
* Creates a <code>BufferedFSInputStream</code>
* with the specified buffer size,
* and saves its argument, the input stream
* <code>in</code>, for later use. An internal
* buffer array of length <code>size</code>
* is created and stored in <code>buf</code>.
*
* @param in the underlying input stream.
* @param size the buffer size.
* @exception IllegalArgumentException if size <= 0.
*/
public BufferedFSInputStream(FSInputStream in, int size) {
super(in, size);
}
//通过跟踪父类代码知道对接了输入流“管道”,初始化了一个大小为size的buffer
public long getPos() throws IOException {
return ((FSInputStream)in).getPos()-(count-pos);
}
//返回现在的偏移量
public long skip(long n) throws IOException {
if (n <= 0) {
return 0;
} seek(getPos()+n);
return n;
}
//跳过n长度后得到现偏移量
public void seek(long pos) throws IOException {
if( pos<0 ) {
return;
}
// optimize: check if the pos is in the buffer
long end = ((FSInputStream)in).getPos();
long start = end - count;
if( pos>=start && pos<end) {
this.pos = (int)(pos-start);
return;
} // invalidate buffer
this.pos = 0;
this.count = 0; ((FSInputStream)in).seek(pos);
}
//实现了Seekable的seek方法
public boolean seekToNewSource(long targetPos) throws IOException {
pos = 0;
count = 0;
return ((FSInputStream)in).seekToNewSource(targetPos);
}
//.....
public int read(long position, byte[] buffer, int offset, int length) throws IOException {
return ((FSInputStream)in).read(position, buffer, offset, length) ;
} public void readFully(long position, byte[] buffer, int offset, int length) throws IOException {
((FSInputStream)in).readFully(position, buffer, offset, length);
} public void readFully(long position, byte[] buffer) throws IOException {
((FSInputStream)in).readFully(position, buffer);
}
}

最新文章

  1. Linux - Screen
  2. jQ1.5中的事件系统(低版本的事件系统)
  3. 2.python基础深入(元组、字符串、列表、字典)
  4. codeforces 486C. Palindrome Transformation 解题报告
  5. 字符编码笔记:ASCII,Unicode和UTF-8【转载】
  6. uedit富文本编辑器
  7. CSS 概览(CSS2.1)更新时间2014-0406
  8. myeclipse 8.5打开文件Could not open the editor: Invalid thread access 异常
  9. C#委托的异步调用【转】
  10. aix Mysql安装 Oracle官方教程
  11. poj 1837 Balance 动态规划 (经典好题,很锻炼思维)
  12. win7下sublime text3 安装Emmet的pyv8
  13. mysql 用户管理 权限控制
  14. YIi2 Pjax简单使用
  15. MinerUtil.java 爬虫工具类
  16. Spring Cloud实战的代码和视频位置
  17. luoguo 1306 斐波那契公约数
  18. abap 增强查找小程序
  19. s21day06 python笔记
  20. 洛谷P5206 数树

热门文章

  1. asp.net(class0625)
  2. 百家搜索:在网站中添加Google、百度等搜索引擎
  3. JSON解析关联类型发生死循环 There is a cycle in the hierarchy!
  4. UVALive 7325 Book Borders (模拟)
  5. MANACHER---求最长回文串
  6. Spring入门(10)-Spring JDBC
  7. MongoDB的安装配置
  8. 在数据库各种状态下查询DBID的五大类十种方法汇总
  9. Castle ActiveRecord学习实践
  10. 解决安装SQL Server2008失败的问题