1 2 3 package net.nutch.fs; 4 5 import java.io.*; 6 import net.nutch.util.NutchConf; 7 8 10 public class NFSDataInputStream extends DataInputStream { 11 12 13 private static class PositionCache extends FilterInputStream { 14 long position; 15 16 public PositionCache(NFSInputStream in) throws IOException { 17 super(in); 18 this.position = in.getPos(); 19 } 20 21 public int read(byte b[], int off, int len) throws IOException { 24 int result = in.read(b, off, len); 25 position += result; 26 return result; 27 } 28 29 public void seek(long desired) throws IOException { 30 ((NFSInputStream)in).seek(desired); position = desired; } 33 34 public long getPos() throws IOException { 35 return position; } 37 38 } 39 40 41 private static class Buffer extends BufferedInputStream { 42 public Buffer(PositionCache in, int bufferSize) throws IOException { 43 super(in, bufferSize); 44 } 45 46 public void seek(long desired) throws IOException { 47 long current = getPos(); 48 long start = (current - this.pos); 49 if (desired >= start && desired < start + this.count) { 50 this.pos += (desired - current); } else { 52 this.count = 0; this.pos = 0; 54 55 ((PositionCache)in).seek(desired); } 57 } 58 59 public long getPos() throws IOException { return ((PositionCache)in).getPos() - (this.count - this.pos); 61 } 62 63 public int read() throws IOException { 65 if (pos >= count) 66 return super.read(); 67 return buf[pos++] & 0xff; 68 } 69 70 } 71 72 public NFSDataInputStream(NFSInputStream in) throws IOException { 73 this(in, NutchConf.getInt("io.file.buffer.size", 4096)); 74 } 75 76 public NFSDataInputStream(NFSInputStream in, int bufferSize) 77 throws IOException { 78 super(new Buffer(new PositionCache(in), bufferSize)); 79 } 80 81 public void seek(long desired) throws IOException { 82 ((Buffer)in).seek(desired); 83 } 84 85 public long getPos() throws IOException { 86 return ((Buffer)in).getPos(); 87 } 88 89 } 90 | Popular Tags |