View Javadoc

1   // BSD License (http://www.galagosearch.org/license)
2   
3   package org.galagosearch.core.index;
4   
5   import java.io.EOFException;
6   import java.io.IOException;
7   import java.io.InputStream;
8   
9   public class PositionalByteStream extends InputStream {
10      int pos = 0;
11      InputStream input;
12      
13      public PositionalByteStream(InputStream in) {
14          this.input = in;
15          this.pos = 0;
16      }
17      
18      @Override
19      public int read(byte[] buffer) throws IOException {
20          int result = input.read(buffer);
21          if(result > 0)
22              pos += result;
23          return result;
24      }
25  
26      public int read() throws IOException {
27          int result = input.read();
28          if(result >= 0)
29              pos += 1;
30          else
31              throw new EOFException("Read off the end of the stream.");
32          return result;
33      }
34  
35      public int position() {
36          return pos;
37      }
38  }