2 //#if defined(FOUNDATION10) || defined(J2SE13)
\r
5 *******************************************************************************
\r
6 * Copyright (C) 1996-2009, International Business Machines Corporation and *
\r
7 * others. All Rights Reserved. *
\r
8 *******************************************************************************
\r
10 package com.ibm.icu.dev.test.util;
\r
12 import java.io.DataOutput;
\r
13 import java.io.IOException;
\r
14 import java.io.ObjectOutput;
\r
15 import java.util.Collection;
\r
16 import java.util.HashMap;
\r
17 import java.util.Iterator;
\r
18 //import java.util.LinkedHashMap;
\r
19 import java.util.Map;
\r
20 import java.util.SortedSet;
\r
21 //import java.util.TreeSet;
\r
23 //import com.ibm.icu.impl.Utility;
\r
24 import com.ibm.icu.text.UTF16;
\r
27 * Simple data output compressor. Nothing fancy, but much smaller footprint for ints and many strings.
\r
29 public final class DataOutputCompressor implements ObjectOutput {
\r
30 static final boolean SHOW = false;
\r
32 private ObjectOutput dataOutput;
\r
34 public DataOutputCompressor(ObjectOutput dataOutput) {
\r
35 this.dataOutput = dataOutput;
\r
38 public DataOutput getDataOutput() {
\r
42 public void setDataOutput(ObjectOutput dataOutput) {
\r
43 this.dataOutput = dataOutput;
\r
46 public void write(byte[] b) throws IOException {
\r
47 dataOutput.write(b);
\r
50 public void write(byte[] b, int off, int len) throws IOException {
\r
51 dataOutput.write(b, off, len);
\r
54 public void write(int b) throws IOException {
\r
55 dataOutput.write(b);
\r
58 public void writeBoolean(boolean v) throws IOException {
\r
59 dataOutput.writeBoolean(v);
\r
62 public void writeByte(int v) throws IOException {
\r
63 dataOutput.writeByte(v);
\r
66 public void writeBytes(String s) throws IOException {
\r
67 dataOutput.writeBytes(s);
\r
70 public void writeDouble(double v) throws IOException {
\r
71 dataOutput.writeDouble(v);
\r
74 public void writeFloat(float v) throws IOException {
\r
75 dataOutput.writeFloat(v);
\r
78 public void close() throws IOException {
\r
81 public void flush() throws IOException {
\r
84 public String toString() {
\r
85 return dataOutput.toString();
\r
87 public void writeObject(Object obj) throws IOException {
\r
88 dataOutput.writeObject(obj);
\r
90 // ==== New Routines ====
\r
92 public void writeChar(int v) throws IOException {
\r
96 public void writeShort(int v) throws IOException {
\r
100 public void writeUShort(int v) throws IOException {
\r
104 public void writeInt(int v) throws IOException {
\r
108 public void writeUInt(int v) throws IOException {
\r
112 public void writeUTF(String str) throws IOException {
\r
113 writeULong(UTF16.countCodePoint(str));
\r
117 public void writeChars(String s) throws IOException {
\r
119 for (int i = 0; i < s.length(); i += UTF16.getCharCount(cp)) {
\r
120 cp = UTF16.charAt(s, i);
\r
125 public void writeLong(long v) throws IOException {
\r
126 long flag = 0; // put sign bit at the bottom, and invert
\r
134 if ((v & ~0x7FL) == 0) {
\r
135 dataOutput.writeByte((byte) v);
\r
138 dataOutput.writeByte((byte) (0x80L | v));
\r
143 public void writeULong(long v) throws IOException {
\r
144 while (true) { // write sequence of 7 bits, with top bit = 1 for continuation
\r
145 if ((v & ~0x7FL) == 0) {
\r
146 dataOutput.writeByte((byte) v);
\r
149 dataOutput.writeByte((byte) (0x80L | v));
\r
157 public void writeStringSet(SortedSet c, Map object_index) throws IOException {
\r
158 if (SHOW) System.out.println("writeStringSet");
\r
159 writeUInt(c.size());
\r
161 object_index.put(null, new Integer(i++));
\r
162 WritePool trailingPool = new WritePool();
\r
163 String lastString = "";
\r
164 for (Iterator it = c.iterator(); it.hasNext();) {
\r
165 String s = (String) it.next();
\r
166 object_index.put(s, new Integer(i++));
\r
167 int common = UnicodeMap.findCommon(lastString, s); // runlength encode
\r
169 String piece = s.substring(common);
\r
170 if (SHOW) System.out.println(common);
\r
172 int inPool = trailingPool.getIndex(piece);
\r
176 trailingPool.put(piece);
\r
178 writeUInt(common | 1);
\r
180 if (SHOW) System.out.println("\t" + inPool);
\r
182 if (SHOW) System.out.println("\t\t" + lastString);
\r
186 public static class WritePool {
\r
187 private Map trailingPool = new HashMap();
\r
188 private int poolCount = 0;
\r
189 public int getIndex(Object o) {
\r
190 Integer inPool = (Integer) trailingPool.get(o);
\r
191 if (inPool == null) return -1;
\r
192 return inPool.intValue();
\r
194 public void put(Object o) {
\r
195 trailingPool.put(o, new Integer(poolCount++));
\r
200 * @throws IOException
\r
203 public void writeCollection(Collection c, Map object_index) throws IOException {
\r
204 writeUInt(c.size());
\r
206 object_index.put(null, new Integer(i++));
\r
207 for (Iterator it = c.iterator(); it.hasNext();) {
\r
208 Object s = it.next();
\r
209 dataOutput.writeObject(s);
\r
210 if (object_index != null) object_index.put(s, new Integer(i++));
\r