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