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 ArrayInput {
12      private DataInput input;
13  
14      public ArrayInput(DataInput i) {
15          input = i;
16      }
17  
18      public int readInt() throws IOException {
19          return input.readInt();
20      }
21  
22      public int[] readInts() throws IOException {
23          int count = readInt();
24          int[] result = new int[count];
25  
26          for (int i = 0; i < count; i++) {
27              result[i] = readInt();
28          }
29  
30          return result;
31      }
32  
33      public long readLong() throws IOException {
34          return input.readLong();
35      }
36  
37      public long[] readLongs() throws IOException {
38          int count = readInt();
39          long[] result = new long[count];
40  
41          for (int i = 0; i < count; i++) {
42              result[i] = readLong();
43          }
44  
45          return result;
46      }
47  
48      public char readChar() throws IOException {
49          return input.readChar();
50      }
51  
52      public char[] readChars() throws IOException {
53          int count = readInt();
54          char[] result = new char[count];
55  
56          for (int i = 0; i < count; i++) {
57              result[i] = readChar();
58          }
59  
60          return result;
61      }
62  
63      public boolean readBoolean() throws IOException {
64          return input.readByte() != 0 ? true : false;
65      }
66  
67      public byte readByte() throws IOException {
68          return input.readByte();
69      }
70  
71      public byte[] readBytes() throws IOException {
72          int count = readInt();
73          byte[] result = new byte[count];
74          input.readFully(result);
75          return result;
76      }
77  
78      public short readShort() throws IOException {
79          return input.readShort();
80      }
81  
82      public short[] readShorts() throws IOException {
83          int count = readInt();
84          short[] result = new short[count];
85  
86          for (int i = 0; i < count; i++) {
87              result[i] = readShort();
88          }
89  
90          return result;
91      }
92  
93      public double readDouble() throws IOException {
94          return input.readDouble();
95      }
96  
97      public double[] readDoubles() throws IOException {
98          int count = readInt();
99          double[] result = new double[count];
100 
101         for (int i = 0; i < count; i++) {
102             result[i] = readDouble();
103         }
104 
105         return result;
106     }
107 
108     public float readFloat() throws IOException {
109         return input.readFloat();
110     }
111 
112     public float[] readFloats() throws IOException {
113         int count = readInt();
114         float[] result = new float[count];
115 
116         for (int i = 0; i < count; i++) {
117             result[i] = readFloat();
118         }
119 
120         return result;
121     }
122 
123     public String readString() throws IOException {
124         byte[] bytes = readBytes();
125         char[] chars = new char[bytes.length];
126 
127         for (int i = 0; i < bytes.length; i++) {
128             if (bytes[i] < 0) {
129                 return new String(bytes, "UTF-8");
130             }
131             chars[i] = (char) bytes[i];
132         }
133 
134         return new String(chars);
135     }
136 
137     public String[] readStrings() throws IOException {
138         int count = readInt();
139         String[] result = new String[count];
140         for (int i = 0; i < count; i++) {
141             result[i] = readString();
142         }
143         return result;
144     }
145 
146     public DataInput getDataInput() {
147         return input;
148     }
149 }
150 
151 /*
152 
153 #
154 # The following Python code can autogenerate these Java methods and
155 # those in ArrayOutput.
156 #
157 
158 arrayMethod = """
159 public %s[] read%s() throws IOException {
160 int count = readInt();
161 %s[] result = new %s[count];
162 
163 for(int i=0; i<count; i++) {
164 result[i] = read%s();
165 }                        
166 
167 return result;
168 }
169 """
170 
171 basicMethod = """
172 public %s read%s() throws IOException {
173 return input.read%s();
174 }                         
175 """
176 stringMethod = """
177 public String readString() throws IOException {
178 char[] chars = readChars();
179 return new String(chars);
180 }
181 """             
182 
183 arrayWriter = """
184 public void write%s(%s[] out) throws IOException {
185 output.writeInt(out.length);
186 for(int i=0; i<out.length; i++) {
187 output.write%s(out[i]);
188 }
189 }"""                                              
190 
191 basicWriter = """
192 public void write%s(%s out) throws IOException {
193 output.write%s(out);
194 }
195 """       
196 
197 stringWriter = """
198 public void writeString(String out) throws IOException {
199 output.writeInt(out.length());
200 output.writeChars(out);
201 }
202 """
203 
204 basicTypes = [ 'int', 'long', 'char', 'byte', 'short', 'double', 'float' ]
205 
206 def caps(s):
207 return s[0].upper() + s[1:]
208 
209 def plural(s):
210 return s + 's'
211 
212 for type in basicTypes:
213 print basicMethod % (type, caps(type), caps(type))
214 print arrayMethod % (type, plural(caps(type)), type, type, caps(type))    
215 print stringMethod                                                             
216 
217 print "// writers ----"
218 
219 for type in basicTypes:
220 print basicWriter % (caps(type), type, caps(type))
221 print arrayWriter % (plural(caps(type)), type, caps(type))        
222 print stringWriter
223 
224  */