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