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