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.DataOutput;
\r
10 import java.io.IOException;
\r
11 import java.io.ObjectOutput;
\r
12 import java.util.Collection;
\r
13 import java.util.HashMap;
\r
14 import java.util.Iterator;
\r
15 import java.util.Map;
\r
16 import java.util.SortedSet;
\r
18 import com.ibm.icu.text.UTF16;
\r
21 * Simple data output compressor. Nothing fancy, but much smaller footprint for ints and many strings.
\r
23 public final class DataOutputCompressor implements ObjectOutput {
\r
24 static final boolean SHOW = false;
\r
26 private ObjectOutput dataOutput;
\r
28 public DataOutputCompressor(ObjectOutput dataOutput) {
\r
29 this.dataOutput = dataOutput;
\r
32 public DataOutput getDataOutput() {
\r
36 public void setDataOutput(ObjectOutput dataOutput) {
\r
37 this.dataOutput = dataOutput;
\r
40 public void write(byte[] b) throws IOException {
\r
41 dataOutput.write(b);
\r
44 public void write(byte[] b, int off, int len) throws IOException {
\r
45 dataOutput.write(b, off, len);
\r
48 public void write(int b) throws IOException {
\r
49 dataOutput.write(b);
\r
52 public void writeBoolean(boolean v) throws IOException {
\r
53 dataOutput.writeBoolean(v);
\r
56 public void writeByte(int v) throws IOException {
\r
57 dataOutput.writeByte(v);
\r
60 public void writeBytes(String s) throws IOException {
\r
61 dataOutput.writeBytes(s);
\r
64 public void writeDouble(double v) throws IOException {
\r
65 dataOutput.writeDouble(v);
\r
68 public void writeFloat(float v) throws IOException {
\r
69 dataOutput.writeFloat(v);
\r
72 public void close() throws IOException {
\r
75 public void flush() throws IOException {
\r
78 public String toString() {
\r
79 return dataOutput.toString();
\r
81 public void writeObject(Object obj) throws IOException {
\r
82 dataOutput.writeObject(obj);
\r
84 // ==== New Routines ====
\r
86 public void writeChar(int v) throws IOException {
\r
90 public void writeShort(int v) throws IOException {
\r
94 public void writeUShort(int v) throws IOException {
\r
98 public void writeInt(int v) throws IOException {
\r
102 public void writeUInt(int v) throws IOException {
\r
106 public void writeUTF(String str) throws IOException {
\r
107 writeULong(UTF16.countCodePoint(str));
\r
111 public void writeChars(String s) throws IOException {
\r
113 for (int i = 0; i < s.length(); i += UTF16.getCharCount(cp)) {
\r
114 cp = UTF16.charAt(s, i);
\r
119 public void writeLong(long v) throws IOException {
\r
120 long flag = 0; // put sign bit at the bottom, and invert
\r
128 if ((v & ~0x7FL) == 0) {
\r
129 dataOutput.writeByte((byte) v);
\r
132 dataOutput.writeByte((byte) (0x80L | v));
\r
137 public void writeULong(long v) throws IOException {
\r
138 while (true) { // write sequence of 7 bits, with top bit = 1 for continuation
\r
139 if ((v & ~0x7FL) == 0) {
\r
140 dataOutput.writeByte((byte) v);
\r
143 dataOutput.writeByte((byte) (0x80L | v));
\r
151 public void writeStringSet(SortedSet c, Map object_index) throws IOException {
\r
152 if (SHOW) System.out.println("writeStringSet");
\r
153 writeUInt(c.size());
\r
155 object_index.put(null, new Integer(i++));
\r
156 WritePool trailingPool = new WritePool();
\r
157 String lastString = "";
\r
158 for (Iterator it = c.iterator(); it.hasNext();) {
\r
159 String s = (String) it.next();
\r
160 object_index.put(s, new Integer(i++));
\r
161 int common = UnicodeMap.findCommonPrefix(lastString, s); // runlength encode
\r
163 String piece = s.substring(common);
\r
164 if (SHOW) System.out.println(common);
\r
166 int inPool = trailingPool.getIndex(piece);
\r
170 trailingPool.put(piece);
\r
172 writeUInt(common | 1);
\r
174 if (SHOW) System.out.println("\t" + inPool);
\r
176 if (SHOW) System.out.println("\t\t" + lastString);
\r
180 public static class WritePool {
\r
181 private Map trailingPool = new HashMap();
\r
182 private int poolCount = 0;
\r
183 public int getIndex(Object o) {
\r
184 Integer inPool = (Integer) trailingPool.get(o);
\r
185 if (inPool == null) return -1;
\r
186 return inPool.intValue();
\r
188 public void put(Object o) {
\r
189 trailingPool.put(o, new Integer(poolCount++));
\r
194 * @throws IOException
\r
197 public void writeCollection(Collection c, Map object_index) throws IOException {
\r
198 writeUInt(c.size());
\r
200 object_index.put(null, new Integer(i++));
\r
201 for (Iterator it = c.iterator(); it.hasNext();) {
\r
202 Object s = it.next();
\r
203 dataOutput.writeObject(s);
\r
204 if (object_index != null) object_index.put(s, new Integer(i++));
\r