001: //##header
002: /*
003: *******************************************************************************
004: * Copyright (C) 1996-2006, International Business Machines Corporation and *
005: * others. All Rights Reserved. *
006: *******************************************************************************
007: */
008: //#ifndef FOUNDATION
009: package com.ibm.icu.dev.test.util;
010:
011: import java.io.DataInput;
012: import java.io.IOException;
013: import java.io.ObjectInput;
014: import java.util.ArrayList;
015: import java.util.Collection; //import java.util.HashMap;
016: import java.util.LinkedHashSet;
017: import java.util.List; //import java.util.Map;
018:
019: //import com.ibm.icu.impl.Utility;
020: import com.ibm.icu.text.UTF16;
021:
022: /**
023: * Simple data input compressor. Nothing fancy, but much smaller footprint for
024: * ints and many strings.
025: */
026: public final class DataInputCompressor implements ObjectInput {
027: static final boolean SHOW = false;
028:
029: private ObjectInput dataInput;
030:
031: private transient StringBuffer stringBuffer = new StringBuffer();
032:
033: public DataInputCompressor(ObjectInput dataInput) {
034: this .dataInput = dataInput;
035: }
036:
037: public DataInput getDataInput() {
038: return dataInput;
039: }
040:
041: public void setDataInput(ObjectInput dataInput) {
042: this .dataInput = dataInput;
043: }
044:
045: public boolean readBoolean() throws IOException {
046: return dataInput.readBoolean();
047: }
048:
049: public byte readByte() throws IOException {
050: return dataInput.readByte();
051: }
052:
053: public int readUnsignedByte() throws IOException {
054: return dataInput.readUnsignedByte();
055: }
056:
057: public double readDouble() throws IOException {
058: return dataInput.readDouble();
059: }
060:
061: public float readFloat() throws IOException {
062: return dataInput.readFloat();
063: }
064:
065: public void readFully(byte[] b) throws IOException {
066: dataInput.readFully(b);
067: }
068:
069: public void readFully(byte[] b, int off, int len)
070: throws IOException {
071: dataInput.readFully(b, off, len);
072: }
073:
074: public int skipBytes(int n) throws IOException {
075: return dataInput.skipBytes(n);
076: }
077:
078: public String readLine() throws IOException {
079: return dataInput.readLine();
080: }
081:
082: public int available() throws IOException {
083: return dataInput.available();
084: }
085:
086: public void close() throws IOException {
087: dataInput.close();
088: }
089:
090: public int read() throws IOException {
091: return dataInput.read();
092: }
093:
094: public int read(byte[] b) throws IOException {
095: return dataInput.read(b);
096: }
097:
098: public int read(byte[] b, int off, int len) throws IOException {
099: return dataInput.read(b, off, len);
100: }
101:
102: public Object readObject() throws ClassNotFoundException,
103: IOException {
104: return dataInput.readObject();
105: }
106:
107: public long skip(long n) throws IOException {
108: return dataInput.skip(n);
109: }
110:
111: public String toString() {
112: return dataInput.toString();
113: }
114:
115: // ==== New Routines ====
116:
117: public char readChar() throws IOException {
118: return (char) readULong();
119: }
120:
121: public short readShort() throws IOException {
122: return (short) readLong();
123: }
124:
125: public int readUnsignedShort() throws IOException {
126: return (int) readULong();
127: }
128:
129: public int readUShort() throws IOException {
130: return (int) readULong();
131: }
132:
133: public int readInt() throws IOException {
134: return (int) readLong();
135: }
136:
137: public int readUInt() throws IOException {
138: return (int) readULong();
139: }
140:
141: public String readChars(int len) throws IOException {
142: stringBuffer.setLength(0);
143: for (int i = 0; i < len; ++i) {
144: int cp = (int) readULong();
145: UTF16.append(stringBuffer, cp);
146: }
147: return stringBuffer.toString();
148: }
149:
150: public String readUTF() throws IOException {
151: int len = (int) readULong();
152: return readChars(len);
153: }
154:
155: public long readLong() throws IOException {
156: long result = 0;
157: int offset = 0;
158: while (true) {
159: long input = readByte();
160: result |= (input & 0x7F) << offset;
161: if ((input & 0x80) == 0)
162: break;
163: offset += 7;
164: }
165: boolean negative = (result & 1) != 0; // get sign bit from the bottom,
166: // and invert
167: result >>>= 1;
168: if (negative)
169: result = ~result;
170: return result;
171: }
172:
173: public long readULong() throws IOException {
174: long result = 0;
175: int offset = 0;
176: while (true) { // read sequence of 7 bits, with top bit = 1 for
177: // continuation
178: int input = readByte();
179: result |= (input & 0x7F) << offset;
180: if ((input & 0x80) == 0)
181: return result;
182: offset += 7;
183: }
184: }
185:
186: /**
187: *
188: */
189: public Object[] readStringSet(Collection availableValues)
190: throws IOException {
191: int size = readUInt();
192: if (SHOW)
193: System.out.println("readStringSet");
194: Object[] valuesList = new Object[size + 1];
195: // first item is null
196: String lastString = "";
197: ReadPool trailingPool = new ReadPool();
198: for (int i = 0; i < size; ++i) {
199: int common = readUInt();
200: boolean inPool = (common & 1) != 0;
201: common >>>= 1;
202: if (SHOW)
203: System.out.println(common);
204: String current;
205: if (inPool) {
206: int poolIndex = readUInt();
207: if (SHOW)
208: System.out.println("\t" + poolIndex);
209: current = (String) trailingPool.get(poolIndex);
210: } else {
211: current = readUTF();
212: trailingPool.add(current);
213: }
214: valuesList[i + 1] = lastString = lastString.substring(0,
215: common)
216: + current;
217: if (SHOW)
218: System.out.println("\t\t" + lastString);
219: if (availableValues != null)
220: availableValues.add(current);
221: }
222: return valuesList;
223: }
224:
225: public static class ReadPool {
226: private List trailingPool = new ArrayList();
227:
228: public Object get(int index) {
229: return trailingPool.get(index);
230: }
231:
232: public void add(Object o) {
233: trailingPool.add(o);
234: }
235: }
236:
237: /**
238: * @throws IOException
239: * @throws ClassNotFoundException
240: *
241: */
242: public Object[] readCollection(LinkedHashSet availableValues)
243: throws ClassNotFoundException, IOException {
244: int size = readUInt();
245: Object[] valuesList = new Object[size + 1];
246: for (int i = 0; i < size; ++i) {
247: valuesList[i + 1] = readObject();
248: }
249: return valuesList;
250: }
251: }
252: //#endif
|