View Javadoc

1   // BSD License (http://www.galagosearch.org/license)
2   package org.galagosearch.tupleflow;
3   
4   import java.io.DataInput;
5   import java.io.IOException;
6   
7   /***
8    *
9    * @author trevor
10   */
11  public class VByteInput implements DataInput {
12      DataInput input;
13  
14      public VByteInput(DataInput input) {
15          this.input = input;
16      }
17  
18      public void readFully(byte[] b, int i, int i0) throws IOException {
19          input.readFully(b, i, i0);
20      }
21  
22      public void readFully(byte[] b) throws IOException {
23          input.readFully(b);
24      }
25      
26      public String readString() throws IOException {
27          int length = readInt();
28          byte[] data = new byte[length];
29          input.readFully(data);
30          return Utility.makeString(data);
31      }
32  
33      public int skipBytes(int i) throws IOException {
34          return input.skipBytes(i);
35      }
36  
37      public int readUnsignedShort() throws IOException {
38          int result = readInt();
39          return (int) (result & 0xffff);
40      }
41  
42      public boolean readBoolean() throws IOException {
43          int result = readInt();
44          return result != 0 ? true : false;
45      }
46  
47      public byte readByte() throws IOException {
48          int result = readInt();
49          return (byte) (result & 0xff);
50      }
51  
52      public char readChar() throws IOException {
53          int result = readInt();
54          return (char) (result & 0xffff);
55      }
56  
57      public double readDouble() throws IOException {
58          long result = input.readLong();
59          return Double.longBitsToDouble(result);
60      }
61  
62      public float readFloat() throws IOException {
63          int result = input.readInt();
64          return Float.intBitsToFloat(result);
65      }
66  
67      public int readInt() throws IOException {
68          int result = 0;
69          int b;
70  
71          for (int position = 0; true; position++) {
72              assert position < 6;
73              b = input.readUnsignedByte();
74  
75              if ((b & 0x80) == 0x80) {
76                  result |= ((b & 0x7f) << (7 * position));
77                  break;
78              } else {
79                  result |= (b << (7 * position));
80              }
81          }
82  
83          return result;
84      }
85  
86      public long readLong() throws IOException {
87          long result = 0;
88          long b;
89  
90          for (int position = 0; true; position++) {
91              b = input.readUnsignedByte();
92  
93              if ((b & 0x80) == 0x80) {
94                  result |= ((long) (b & 0x7f) << (7 * position));
95                  break;
96              } else {
97                  result |= ((long) b << (7 * position));
98              }
99          }
100 
101         return result;
102     }
103 
104     public String readLine() throws IOException {
105         throw new IOException("Operation not supported.");
106     }
107 
108     public short readShort() throws IOException {
109         return (short) (readInt() & 0xffff);
110     }
111 
112     public String readUTF() throws IOException {
113         throw new IOException("Operation not supported.");
114     }
115 
116     public int readUnsignedByte() throws IOException {
117         return (readInt() & 0xff);
118     }
119 }