2 *******************************************************************************
\r
3 * Copyright (C) 1996-2009, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.dev.test.util;
\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
17 import com.ibm.icu.text.UTF16;
\r
20 * Simple data input compressor. Nothing fancy, but much smaller footprint for
\r
21 * ints and many strings.
\r
23 public final class DataInputCompressor implements ObjectInput {
\r
24 static final boolean SHOW = false;
\r
26 private ObjectInput dataInput;
\r
28 private transient StringBuffer stringBuffer = new StringBuffer();
\r
30 public DataInputCompressor(ObjectInput dataInput) {
\r
31 this.dataInput = dataInput;
\r
34 public DataInput getDataInput() {
\r
38 public void setDataInput(ObjectInput dataInput) {
\r
39 this.dataInput = dataInput;
\r
42 public boolean readBoolean() throws IOException {
\r
43 return dataInput.readBoolean();
\r
46 public byte readByte() throws IOException {
\r
47 return dataInput.readByte();
\r
50 public int readUnsignedByte() throws IOException {
\r
51 return dataInput.readUnsignedByte();
\r
54 public double readDouble() throws IOException {
\r
55 return dataInput.readDouble();
\r
58 public float readFloat() throws IOException {
\r
59 return dataInput.readFloat();
\r
62 public void readFully(byte[] b) throws IOException {
\r
63 dataInput.readFully(b);
\r
66 public void readFully(byte[] b, int off, int len) throws IOException {
\r
67 dataInput.readFully(b, off, len);
\r
70 public int skipBytes(int n) throws IOException {
\r
71 return dataInput.skipBytes(n);
\r
74 public String readLine() throws IOException {
\r
75 return dataInput.readLine();
\r
78 public int available() throws IOException {
\r
79 return dataInput.available();
\r
81 public void close() throws IOException {
\r
84 public int read() throws IOException {
\r
85 return dataInput.read();
\r
87 public int read(byte[] b) throws IOException {
\r
88 return dataInput.read(b);
\r
90 public int read(byte[] b, int off, int len) throws IOException {
\r
91 return dataInput.read(b, off, len);
\r
93 public Object readObject() throws ClassNotFoundException, IOException {
\r
94 return dataInput.readObject();
\r
96 public long skip(long n) throws IOException {
\r
97 return dataInput.skip(n);
\r
99 public String toString() {
\r
100 return dataInput.toString();
\r
102 // ==== New Routines ====
\r
104 public char readChar() throws IOException {
\r
105 return (char) readULong();
\r
108 public short readShort() throws IOException {
\r
109 return (short) readLong();
\r
112 public int readUnsignedShort() throws IOException {
\r
113 return (int) readULong();
\r
116 public int readUShort() throws IOException {
\r
117 return (int) readULong();
\r
120 public int readInt() throws IOException {
\r
121 return (int) readLong();
\r
124 public int readUInt() throws IOException {
\r
125 return (int) readULong();
\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
134 return stringBuffer.toString();
\r
137 public String readUTF() throws IOException {
\r
138 int len = (int) readULong();
\r
139 return readChars(len);
\r
142 public long readLong() throws IOException {
\r
146 long input = readByte();
\r
147 result |= (input & 0x7F) << offset;
\r
148 if ((input & 0x80) == 0)
\r
152 boolean negative = (result & 1) != 0; // get sign bit from the bottom,
\r
160 public long readULong() throws IOException {
\r
163 while (true) { // read sequence of 7 bits, with top bit = 1 for
\r
165 int input = readByte();
\r
166 result |= (input & 0x7F) << offset;
\r
167 if ((input & 0x80) == 0)
\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
188 if (SHOW) System.out.println(common);
\r
191 int poolIndex = readUInt();
\r
192 if (SHOW) System.out.println("\t" + poolIndex);
\r
193 current = (String) trailingPool.get(poolIndex);
\r
195 current = readUTF();
\r
196 trailingPool.add(current);
\r
198 valuesList[i + 1] = lastString = lastString.substring(0, common)
\r
200 if (SHOW) System.out.println("\t\t" + lastString);
\r
201 if (availableValues != null) availableValues.add(current);
\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
211 public void add(Object o) {
\r
212 trailingPool.add(o);
\r
217 * @throws IOException
\r
218 * @throws ClassNotFoundException
\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