]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/main/tests/framework/src/com/ibm/icu/dev/util/DataInputCompressor.java
Clean up imports.
[Dictionary.git] / jars / icu4j-52_1 / main / tests / framework / src / com / ibm / icu / dev / util / DataInputCompressor.java
1 /*
2  *******************************************************************************
3  * Copyright (C) 1996-2012, International Business Machines Corporation and    *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  */
7 package com.ibm.icu.dev.util;
8
9 import java.io.DataInput;
10 import java.io.IOException;
11 import java.io.ObjectInput;
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.LinkedHashSet;
15 import java.util.List;
16
17 import com.ibm.icu.text.UTF16;
18
19 /**
20  * Simple data input compressor. Nothing fancy, but much smaller footprint for
21  * ints and many strings.
22  */
23 public final class DataInputCompressor implements ObjectInput {
24     static final boolean SHOW = false;
25
26     private ObjectInput dataInput;
27
28     private transient StringBuffer stringBuffer = new StringBuffer();
29
30     public DataInputCompressor(ObjectInput dataInput) {
31         this.dataInput = dataInput;
32     }
33
34     public DataInput getDataInput() {
35         return dataInput;
36     }
37
38     public void setDataInput(ObjectInput dataInput) {
39         this.dataInput = dataInput;
40     }
41
42     public boolean readBoolean() throws IOException {
43         return dataInput.readBoolean();
44     }
45
46     public byte readByte() throws IOException {
47         return dataInput.readByte();
48     }
49
50     public int readUnsignedByte() throws IOException {
51         return dataInput.readUnsignedByte();
52     }
53
54     public double readDouble() throws IOException {
55         return dataInput.readDouble();
56     }
57
58     public float readFloat() throws IOException {
59         return dataInput.readFloat();
60     }
61
62     public void readFully(byte[] b) throws IOException {
63         dataInput.readFully(b);
64     }
65
66     public void readFully(byte[] b, int off, int len) throws IOException {
67         dataInput.readFully(b, off, len);
68     }
69
70     public int skipBytes(int n) throws IOException {
71         return dataInput.skipBytes(n);
72     }
73
74     public String readLine() throws IOException {
75         return dataInput.readLine();
76     }
77
78     public int available() throws IOException {
79         return dataInput.available();
80     }
81     public void close() throws IOException {
82         dataInput.close();
83     }
84     public int read() throws IOException {
85         return dataInput.read();
86     }
87     public int read(byte[] b) throws IOException {
88         return dataInput.read(b);
89     }
90     public int read(byte[] b, int off, int len) throws IOException {
91         return dataInput.read(b, off, len);
92     }
93     public Object readObject() throws ClassNotFoundException, IOException {
94         return dataInput.readObject();
95     }
96     public long skip(long n) throws IOException {
97         return dataInput.skip(n);
98     }
99     public String toString() {
100         return dataInput.toString();
101     }
102     // ==== New Routines ====
103
104     public char readChar() throws IOException {
105         return (char) readULong();
106     }
107
108     public short readShort() throws IOException {
109         return (short) readLong();
110     }
111
112     public int readUnsignedShort() throws IOException {
113         return (int) readULong();
114     }
115
116     public int readUShort() throws IOException {
117         return (int) readULong();
118     }
119
120     public int readInt() throws IOException {
121         return (int) readLong();
122     }
123
124     public int readUInt() throws IOException {
125         return (int) readULong();
126     }
127
128     public String readChars(int len) throws IOException {
129         stringBuffer.setLength(0);
130         for (int i = 0; i < len; ++i) {
131             int cp = (int) readULong();
132             UTF16.append(stringBuffer, cp);
133         }
134         return stringBuffer.toString();
135     }
136
137     public String readUTF() throws IOException {
138         int len = (int) readULong();
139         return readChars(len);
140     }
141
142     public long readLong() throws IOException {
143         long result = 0;
144         int offset = 0;
145         while (true) {
146             long input = readByte();
147             result |= (input & 0x7F) << offset;
148             if ((input & 0x80) == 0)
149                 break;
150             offset += 7;
151         }
152         boolean negative = (result & 1) != 0; // get sign bit from the bottom,
153                                               // and invert
154         result >>>= 1;
155         if (negative)
156             result = ~result;
157         return result;
158     }
159
160     public long readULong() throws IOException {
161         long result = 0;
162         int offset = 0;
163         while (true) { // read sequence of 7 bits, with top bit = 1 for
164                        // continuation
165             int input = readByte();
166             result |= (input & 0x7F) << offset;
167             if ((input & 0x80) == 0)
168                 return result;
169             offset += 7;
170         }
171     }
172
173     /**
174      *  
175      */
176     public Object[] readStringSet(Collection availableValues)
177             throws IOException {
178         int size = readUInt();
179         if (SHOW) System.out.println("readStringSet");
180         Object[] valuesList = new Object[size + 1];
181         // first item is null
182         String lastString = "";
183         ReadPool trailingPool = new ReadPool();
184         for (int i = 0; i < size; ++i) {
185             int common = readUInt();
186             boolean inPool = (common & 1) != 0;
187             common >>>= 1;
188             if (SHOW) System.out.println(common);
189             String current;
190             if (inPool) {
191                 int poolIndex = readUInt();
192                 if (SHOW) System.out.println("\t" + poolIndex);
193                 current = (String) trailingPool.get(poolIndex);
194             } else {
195                 current = readUTF();
196                 trailingPool.add(current);
197             }
198             valuesList[i + 1] = lastString = lastString.substring(0, common)
199                     + current;
200             if (SHOW) System.out.println("\t\t" + lastString);
201             if (availableValues != null) availableValues.add(current);
202         }
203         return valuesList;
204     }
205     
206     public static class ReadPool {
207         private List trailingPool = new ArrayList();
208         public Object get(int index) {
209             return trailingPool.get(index);
210         }
211         public void add(Object o) {
212             trailingPool.add(o);
213         }
214     }
215
216     /**
217      * @throws IOException
218      * @throws ClassNotFoundException
219      * 
220      */
221     public Object[] readCollection(LinkedHashSet availableValues) throws ClassNotFoundException, IOException {
222         int size = readUInt();
223         Object[] valuesList = new Object[size + 1];
224         for (int i = 0; i < size; ++i) {
225             valuesList[i + 1] = readObject();
226         }
227        return valuesList;
228     }
229 }