Coverage Report - org.galagosearch.tupleflow.ArrayInput
 
Classes in this File Line Coverage Branch Coverage Complexity
ArrayInput
0%
0/58
0%
0/20
1.526
 
 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  0
     public ArrayInput(DataInput i) {
 15  0
         input = i;
 16  0
     }
 17  
 
 18  
     public int readInt() throws IOException {
 19  0
         return input.readInt();
 20  
     }
 21  
 
 22  
     public int[] readInts() throws IOException {
 23  0
         int count = readInt();
 24  0
         int[] result = new int[count];
 25  
 
 26  0
         for (int i = 0; i < count; i++) {
 27  0
             result[i] = readInt();
 28  
         }
 29  
 
 30  0
         return result;
 31  
     }
 32  
 
 33  
     public long readLong() throws IOException {
 34  0
         return input.readLong();
 35  
     }
 36  
 
 37  
     public long[] readLongs() throws IOException {
 38  0
         int count = readInt();
 39  0
         long[] result = new long[count];
 40  
 
 41  0
         for (int i = 0; i < count; i++) {
 42  0
             result[i] = readLong();
 43  
         }
 44  
 
 45  0
         return result;
 46  
     }
 47  
 
 48  
     public char readChar() throws IOException {
 49  0
         return input.readChar();
 50  
     }
 51  
 
 52  
     public char[] readChars() throws IOException {
 53  0
         int count = readInt();
 54  0
         char[] result = new char[count];
 55  
 
 56  0
         for (int i = 0; i < count; i++) {
 57  0
             result[i] = readChar();
 58  
         }
 59  
 
 60  0
         return result;
 61  
     }
 62  
 
 63  
     public boolean readBoolean() throws IOException {
 64  0
         return input.readByte() != 0 ? true : false;
 65  
     }
 66  
 
 67  
     public byte readByte() throws IOException {
 68  0
         return input.readByte();
 69  
     }
 70  
 
 71  
     public byte[] readBytes() throws IOException {
 72  0
         int count = readInt();
 73  0
         byte[] result = new byte[count];
 74  0
         input.readFully(result);
 75  0
         return result;
 76  
     }
 77  
 
 78  
     public short readShort() throws IOException {
 79  0
         return input.readShort();
 80  
     }
 81  
 
 82  
     public short[] readShorts() throws IOException {
 83  0
         int count = readInt();
 84  0
         short[] result = new short[count];
 85  
 
 86  0
         for (int i = 0; i < count; i++) {
 87  0
             result[i] = readShort();
 88  
         }
 89  
 
 90  0
         return result;
 91  
     }
 92  
 
 93  
     public double readDouble() throws IOException {
 94  0
         return input.readDouble();
 95  
     }
 96  
 
 97  
     public double[] readDoubles() throws IOException {
 98  0
         int count = readInt();
 99  0
         double[] result = new double[count];
 100  
 
 101  0
         for (int i = 0; i < count; i++) {
 102  0
             result[i] = readDouble();
 103  
         }
 104  
 
 105  0
         return result;
 106  
     }
 107  
 
 108  
     public float readFloat() throws IOException {
 109  0
         return input.readFloat();
 110  
     }
 111  
 
 112  
     public float[] readFloats() throws IOException {
 113  0
         int count = readInt();
 114  0
         float[] result = new float[count];
 115  
 
 116  0
         for (int i = 0; i < count; i++) {
 117  0
             result[i] = readFloat();
 118  
         }
 119  
 
 120  0
         return result;
 121  
     }
 122  
 
 123  
     public String readString() throws IOException {
 124  0
         byte[] bytes = readBytes();
 125  0
         char[] chars = new char[bytes.length];
 126  
 
 127  0
         for (int i = 0; i < bytes.length; i++) {
 128  0
             if (bytes[i] < 0) {
 129  0
                 return new String(bytes, "UTF-8");
 130  
             }
 131  0
             chars[i] = (char) bytes[i];
 132  
         }
 133  
 
 134  0
         return new String(chars);
 135  
     }
 136  
 
 137  
     public String[] readStrings() throws IOException {
 138  0
         int count = readInt();
 139  0
         String[] result = new String[count];
 140  0
         for (int i = 0; i < count; i++) {
 141  0
             result[i] = readString();
 142  
         }
 143  0
         return result;
 144  
     }
 145  
 
 146  
     public DataInput getDataInput() {
 147  0
         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  
  */