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