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