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