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