View Javadoc

1   // BSD License (http://www.galagosearch.org/license)
2   package org.galagosearch.tupleflow;
3   
4   import java.io.DataOutput;
5   import java.io.IOException;
6   
7   /***
8    *
9    * @author trevor
10   */
11  public class ArrayOutput {
12      DataOutput output;
13  
14      public ArrayOutput(DataOutput o) {
15          output = o;
16      }
17  
18      public DataOutput getDataOutput() {
19          return output;
20      }
21  
22      public void writeInt(int out) throws IOException {
23          output.writeInt(out);
24      }
25  
26      public void writeInts(int[] out) throws IOException {
27          output.writeInt(out.length);
28          for (int i = 0; i < out.length; i++) {
29              output.writeInt(out[i]);
30          }
31      }
32  
33      public void writeLong(long out) throws IOException {
34          output.writeLong(out);
35      }
36  
37      public void writeLongs(long[] out) throws IOException {
38          output.writeInt(out.length);
39          for (int i = 0; i < out.length; i++) {
40              output.writeLong(out[i]);
41          }
42      }
43  
44      public void writeChar(char out) throws IOException {
45          output.writeChar(out);
46      }
47  
48      public void writeChars(char[] out) throws IOException {
49          output.writeInt(out.length);
50          for (int i = 0; i < out.length; i++) {
51              output.writeChar(out[i]);
52          }
53      }
54  
55      public void writeBoolean(boolean out) throws IOException {
56          byte b = out ? (byte) 1 : (byte) 0;
57          output.writeByte(b);
58      }
59  
60      public void writeByte(byte out) throws IOException {
61          output.writeByte(out);
62      }
63  
64      public void writeBytes(byte[] out) throws IOException {
65          output.writeInt(out.length);
66          output.write(out);
67      }
68  
69      public void writeShort(short out) throws IOException {
70          output.writeShort(out);
71      }
72  
73      public void writeShorts(short[] out) throws IOException {
74          output.writeInt(out.length);
75          for (int i = 0; i < out.length; i++) {
76              output.writeShort(out[i]);
77          }
78      }
79  
80      public void writeDouble(double out) throws IOException {
81          output.writeDouble(out);
82      }
83  
84      public void writeDoubles(double[] out) throws IOException {
85          output.writeInt(out.length);
86          for (int i = 0; i < out.length; i++) {
87              output.writeDouble(out[i]);
88          }
89      }
90  
91      public void writeFloat(float out) throws IOException {
92          output.writeFloat(out);
93      }
94  
95      public void writeFloats(float[] out) throws IOException {
96          output.writeInt(out.length);
97          for (int i = 0; i < out.length; i++) {
98              output.writeFloat(out[i]);
99          }
100     }
101 
102     public void writeString(String out) throws IOException {
103         // check to see if there are any UTF-8 chars in here
104         byte[] bytes = new byte[out.length()];
105 
106         for (int i = 0; i < out.length(); i++) {
107             char c = out.charAt(i);
108             if (c >= 128) {
109                 bytes = out.getBytes("UTF-8");
110                 break;
111             } else {
112                 bytes[i] = (byte) c;
113             }
114         }
115 
116         writeBytes(bytes);
117     }
118 
119     public void writeStrings(String[] out) throws IOException {
120         output.writeInt(out.length);
121         for (int i = 0; i < out.length; i++) {
122             writeString(out[i]);
123         }
124     }
125 }