2 //#if defined(FOUNDATION10) || defined(J2SE13)
\r
5 *******************************************************************************
\r
6 * Copyright (C) 1996-2009, International Business Machines Corporation and *
\r
7 * others. All Rights Reserved. *
\r
8 *******************************************************************************
\r
10 package com.ibm.icu.dev.test.util;
\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
22 //import com.ibm.icu.impl.Utility;
\r
23 import com.ibm.icu.text.UTF16;
\r
26 * Simple data input compressor. Nothing fancy, but much smaller footprint for
\r
27 * ints and many strings.
\r
29 public final class DataInputCompressor implements ObjectInput {
\r
30 static final boolean SHOW = false;
\r
32 private ObjectInput dataInput;
\r
34 private transient StringBuffer stringBuffer = new StringBuffer();
\r
36 public DataInputCompressor(ObjectInput dataInput) {
\r
37 this.dataInput = dataInput;
\r
40 public DataInput getDataInput() {
\r
44 public void setDataInput(ObjectInput dataInput) {
\r
45 this.dataInput = dataInput;
\r
48 public boolean readBoolean() throws IOException {
\r
49 return dataInput.readBoolean();
\r
52 public byte readByte() throws IOException {
\r
53 return dataInput.readByte();
\r
56 public int readUnsignedByte() throws IOException {
\r
57 return dataInput.readUnsignedByte();
\r
60 public double readDouble() throws IOException {
\r
61 return dataInput.readDouble();
\r
64 public float readFloat() throws IOException {
\r
65 return dataInput.readFloat();
\r
68 public void readFully(byte[] b) throws IOException {
\r
69 dataInput.readFully(b);
\r
72 public void readFully(byte[] b, int off, int len) throws IOException {
\r
73 dataInput.readFully(b, off, len);
\r
76 public int skipBytes(int n) throws IOException {
\r
77 return dataInput.skipBytes(n);
\r
80 public String readLine() throws IOException {
\r
81 return dataInput.readLine();
\r
84 public int available() throws IOException {
\r
85 return dataInput.available();
\r
87 public void close() throws IOException {
\r
90 public int read() throws IOException {
\r
91 return dataInput.read();
\r
93 public int read(byte[] b) throws IOException {
\r
94 return dataInput.read(b);
\r
96 public int read(byte[] b, int off, int len) throws IOException {
\r
97 return dataInput.read(b, off, len);
\r
99 public Object readObject() throws ClassNotFoundException, IOException {
\r
100 return dataInput.readObject();
\r
102 public long skip(long n) throws IOException {
\r
103 return dataInput.skip(n);
\r
105 public String toString() {
\r
106 return dataInput.toString();
\r
108 // ==== New Routines ====
\r
110 public char readChar() throws IOException {
\r
111 return (char) readULong();
\r
114 public short readShort() throws IOException {
\r
115 return (short) readLong();
\r
118 public int readUnsignedShort() throws IOException {
\r
119 return (int) readULong();
\r
122 public int readUShort() throws IOException {
\r
123 return (int) readULong();
\r
126 public int readInt() throws IOException {
\r
127 return (int) readLong();
\r
130 public int readUInt() throws IOException {
\r
131 return (int) readULong();
\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
140 return stringBuffer.toString();
\r
143 public String readUTF() throws IOException {
\r
144 int len = (int) readULong();
\r
145 return readChars(len);
\r
148 public long readLong() throws IOException {
\r
152 long input = readByte();
\r
153 result |= (input & 0x7F) << offset;
\r
154 if ((input & 0x80) == 0)
\r
158 boolean negative = (result & 1) != 0; // get sign bit from the bottom,
\r
166 public long readULong() throws IOException {
\r
169 while (true) { // read sequence of 7 bits, with top bit = 1 for
\r
171 int input = readByte();
\r
172 result |= (input & 0x7F) << offset;
\r
173 if ((input & 0x80) == 0)
\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
194 if (SHOW) System.out.println(common);
\r
197 int poolIndex = readUInt();
\r
198 if (SHOW) System.out.println("\t" + poolIndex);
\r
199 current = (String) trailingPool.get(poolIndex);
\r
201 current = readUTF();
\r
202 trailingPool.add(current);
\r
204 valuesList[i + 1] = lastString = lastString.substring(0, common)
\r
206 if (SHOW) System.out.println("\t\t" + lastString);
\r
207 if (availableValues != null) availableValues.add(current);
\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
217 public void add(Object o) {
\r
218 trailingPool.add(o);
\r
223 * @throws IOException
\r
224 * @throws ClassNotFoundException
\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