KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > nutch > fs > NFSDataInputStream


1 /* Copyright (c) 2004 The Nutch Organization. All rights reserved. */
2 /* Use subject to the conditions in http://www.nutch.org/LICENSE.txt. */
3 package net.nutch.fs;
4
5 import java.io.*;
6 import net.nutch.util.NutchConf;
7
8 /** Utility that wraps a {@link NFSInputStream} in a {@link DataInputStream}
9  * and buffers input through a {@link BufferedInputStream}. */

10 public class NFSDataInputStream extends DataInputStream {
11   
12   /** Cache the file position. This improves performance significantly.*/
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     // This is the only read() method called by BufferedInputStream, so we trap
22
// calls to it in order to cache the position.
23
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); // seek underlying stream
31
position = desired; // update position
32
}
33       
34     public long getPos() throws IOException {
35       return position; // return cached position
36
}
37     
38   }
39
40   /** Buffer input. This improves performance significantly.*/
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); // can position within buffer
51
} else {
52         this.count = 0; // invalidate buffer
53
this.pos = 0;
54
55         ((PositionCache)in).seek(desired); // seek underlying stream
56
}
57     }
58       
59     public long getPos() throws IOException { // adjust for buffer
60
return ((PositionCache)in).getPos() - (this.count - this.pos);
61     }
62
63     // optimized version of read()
64
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