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