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