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