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