]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/icu/dev/test/util/DataOutputCompressor.java
go
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / dev / test / util / DataOutputCompressor.java
1 //##header J2SE15
2 //#if defined(FOUNDATION10) || defined(J2SE13)
3 //#else
4 /*
5  *******************************************************************************
6  * Copyright (C) 1996-2009, International Business Machines Corporation and    *
7  * others. All Rights Reserved.                                                *
8  *******************************************************************************
9  */
10 package com.ibm.icu.dev.test.util;
11
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;
19 import java.util.Map;
20 import java.util.SortedSet;
21 //import java.util.TreeSet;
22
23 //import com.ibm.icu.impl.Utility;
24 import com.ibm.icu.text.UTF16;
25
26 /**
27  * Simple data output compressor. Nothing fancy, but much smaller footprint for ints and many strings.
28  */
29 public final class DataOutputCompressor implements ObjectOutput {
30     static final boolean SHOW = false;
31
32     private ObjectOutput dataOutput;
33
34     public DataOutputCompressor(ObjectOutput dataOutput) {
35         this.dataOutput = dataOutput;
36     }
37
38     public DataOutput getDataOutput() {
39         return dataOutput;
40     }
41
42     public void setDataOutput(ObjectOutput dataOutput) {
43         this.dataOutput = dataOutput;
44     }
45
46     public void write(byte[] b) throws IOException {
47         dataOutput.write(b);
48     }
49
50     public void write(byte[] b, int off, int len) throws IOException {
51         dataOutput.write(b, off, len);
52     }
53
54     public void write(int b) throws IOException {
55         dataOutput.write(b);
56     }
57
58     public void writeBoolean(boolean v) throws IOException {
59         dataOutput.writeBoolean(v);
60     }
61
62     public void writeByte(int v) throws IOException {
63         dataOutput.writeByte(v);
64     }
65
66     public void writeBytes(String s) throws IOException {
67         dataOutput.writeBytes(s);
68     }
69
70     public void writeDouble(double v) throws IOException {
71         dataOutput.writeDouble(v);
72     }
73
74     public void writeFloat(float v) throws IOException {
75         dataOutput.writeFloat(v);
76     }
77
78     public void close() throws IOException {
79         dataOutput.close();
80     }
81     public void flush() throws IOException {
82         dataOutput.flush();
83     }
84     public String toString() {
85         return dataOutput.toString();
86     }
87     public void writeObject(Object obj) throws IOException {
88         dataOutput.writeObject(obj);
89     }
90     // ==== New Routines ====
91
92     public void writeChar(int v) throws IOException {
93         writeULong(v);
94     }
95
96     public void writeShort(int v) throws IOException {
97         writeLong(v);
98     }
99
100     public void writeUShort(int v) throws IOException {
101         writeULong(v);
102     }
103
104     public void writeInt(int v) throws IOException {
105         writeLong(v);
106     }
107
108     public void writeUInt(int v) throws IOException {
109         writeULong(v);
110     }
111
112     public void writeUTF(String str) throws IOException {
113         writeULong(UTF16.countCodePoint(str));
114         writeChars(str);
115     }
116
117     public void writeChars(String s) throws IOException {
118         int cp = 0;
119         for (int i = 0; i < s.length(); i += UTF16.getCharCount(cp)) {
120             cp = UTF16.charAt(s, i);
121             writeULong(cp);
122         }
123     }
124
125     public void writeLong(long v) throws IOException {
126         long flag = 0; // put sign bit at the bottom, and invert
127         if (v < 0) {
128             v = ~v;
129             flag = 1;
130         }
131         v <<= 1;
132         v |= flag;
133         while (true) {
134             if ((v & ~0x7FL) == 0) {
135                 dataOutput.writeByte((byte) v);
136                 break;
137             }
138             dataOutput.writeByte((byte) (0x80L | v));
139             v >>>= 7;
140         }
141     }
142
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);
147                 break;
148             }
149             dataOutput.writeByte((byte) (0x80L | v));
150             v >>>= 7;
151         }
152     }
153
154     /**
155      * 
156      */
157     public void writeStringSet(SortedSet c, Map object_index) throws IOException {
158         if (SHOW) System.out.println("writeStringSet");
159         writeUInt(c.size());
160         int i = 0;
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
168             lastString = s;
169             String piece = s.substring(common);
170             if (SHOW) System.out.println(common);
171             common <<= 1;
172             int inPool = trailingPool.getIndex(piece);
173             if (inPool < 0) {
174                 writeUInt(common);
175                 writeUTF(piece);
176                 trailingPool.put(piece);
177             } else {
178                 writeUInt(common | 1);
179                 writeUInt(inPool);
180                 if (SHOW) System.out.println("\t" + inPool);
181             }
182             if (SHOW) System.out.println("\t\t" + lastString);
183         }
184     }
185     
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();
193         }
194         public void put(Object o) {
195             trailingPool.put(o, new Integer(poolCount++));
196         }
197     }
198
199     /**
200      * @throws IOException
201      * 
202      */
203     public void writeCollection(Collection c, Map object_index) throws IOException {
204         writeUInt(c.size());
205         int i = 0;
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++));
211         }
212     }
213 }
214
215 //#endif