]> gitweb.fperrin.net Git - Dictionary.git/blobdiff - 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
old mode 100755 (executable)
new mode 100644 (file)
index fcb1acf..850a57e
-//##header\r
-//#if defined(FOUNDATION10) || defined(J2SE13)\r
-//#else\r
-/*\r
- *******************************************************************************\r
- * Copyright (C) 1996-2009, International Business Machines Corporation and    *\r
- * others. All Rights Reserved.                                                *\r
- *******************************************************************************\r
- */\r
-package com.ibm.icu.dev.test.util;\r
-\r
-import java.io.DataOutput;\r
-import java.io.IOException;\r
-import java.io.ObjectOutput;\r
-import java.util.Collection;\r
-import java.util.HashMap;\r
-import java.util.Iterator;\r
-//import java.util.LinkedHashMap;\r
-import java.util.Map;\r
-import java.util.SortedSet;\r
-//import java.util.TreeSet;\r
-\r
-//import com.ibm.icu.impl.Utility;\r
-import com.ibm.icu.text.UTF16;\r
-\r
-/**\r
- * Simple data output compressor. Nothing fancy, but much smaller footprint for ints and many strings.\r
- */\r
-public final class DataOutputCompressor implements ObjectOutput {\r
-    static final boolean SHOW = false;\r
-\r
-    private ObjectOutput dataOutput;\r
-\r
-    public DataOutputCompressor(ObjectOutput dataOutput) {\r
-        this.dataOutput = dataOutput;\r
-    }\r
-\r
-    public DataOutput getDataOutput() {\r
-        return dataOutput;\r
-    }\r
-\r
-    public void setDataOutput(ObjectOutput dataOutput) {\r
-        this.dataOutput = dataOutput;\r
-    }\r
-\r
-    public void write(byte[] b) throws IOException {\r
-        dataOutput.write(b);\r
-    }\r
-\r
-    public void write(byte[] b, int off, int len) throws IOException {\r
-        dataOutput.write(b, off, len);\r
-    }\r
-\r
-    public void write(int b) throws IOException {\r
-        dataOutput.write(b);\r
-    }\r
-\r
-    public void writeBoolean(boolean v) throws IOException {\r
-        dataOutput.writeBoolean(v);\r
-    }\r
-\r
-    public void writeByte(int v) throws IOException {\r
-        dataOutput.writeByte(v);\r
-    }\r
-\r
-    public void writeBytes(String s) throws IOException {\r
-        dataOutput.writeBytes(s);\r
-    }\r
-\r
-    public void writeDouble(double v) throws IOException {\r
-        dataOutput.writeDouble(v);\r
-    }\r
-\r
-    public void writeFloat(float v) throws IOException {\r
-        dataOutput.writeFloat(v);\r
-    }\r
-\r
-    public void close() throws IOException {\r
-        dataOutput.close();\r
-    }\r
-    public void flush() throws IOException {\r
-        dataOutput.flush();\r
-    }\r
-    public String toString() {\r
-        return dataOutput.toString();\r
-    }\r
-    public void writeObject(Object obj) throws IOException {\r
-        dataOutput.writeObject(obj);\r
-    }\r
-    // ==== New Routines ====\r
-\r
-    public void writeChar(int v) throws IOException {\r
-        writeULong(v);\r
-    }\r
-\r
-    public void writeShort(int v) throws IOException {\r
-        writeLong(v);\r
-    }\r
-\r
-    public void writeUShort(int v) throws IOException {\r
-        writeULong(v);\r
-    }\r
-\r
-    public void writeInt(int v) throws IOException {\r
-        writeLong(v);\r
-    }\r
-\r
-    public void writeUInt(int v) throws IOException {\r
-        writeULong(v);\r
-    }\r
-\r
-    public void writeUTF(String str) throws IOException {\r
-        writeULong(UTF16.countCodePoint(str));\r
-        writeChars(str);\r
-    }\r
-\r
-    public void writeChars(String s) throws IOException {\r
-        int cp = 0;\r
-        for (int i = 0; i < s.length(); i += UTF16.getCharCount(cp)) {\r
-            cp = UTF16.charAt(s, i);\r
-            writeULong(cp);\r
-        }\r
-    }\r
-\r
-    public void writeLong(long v) throws IOException {\r
-        long flag = 0; // put sign bit at the bottom, and invert\r
-        if (v < 0) {\r
-            v = ~v;\r
-            flag = 1;\r
-        }\r
-        v <<= 1;\r
-        v |= flag;\r
-        while (true) {\r
-            if ((v & ~0x7FL) == 0) {\r
-                dataOutput.writeByte((byte) v);\r
-                break;\r
-            }\r
-            dataOutput.writeByte((byte) (0x80L | v));\r
-            v >>>= 7;\r
-        }\r
-    }\r
-\r
-    public void writeULong(long v) throws IOException {\r
-        while (true) { // write sequence of 7 bits, with top bit = 1 for continuation\r
-            if ((v & ~0x7FL) == 0) {\r
-                dataOutput.writeByte((byte) v);\r
-                break;\r
-            }\r
-            dataOutput.writeByte((byte) (0x80L | v));\r
-            v >>>= 7;\r
-        }\r
-    }\r
-\r
-    /**\r
-     * \r
-     */\r
-    public void writeStringSet(SortedSet c, Map object_index) throws IOException {\r
-        if (SHOW) System.out.println("writeStringSet");\r
-        writeUInt(c.size());\r
-        int i = 0;\r
-        object_index.put(null, new Integer(i++));\r
-        WritePool trailingPool = new WritePool();\r
-        String lastString = "";\r
-        for (Iterator it = c.iterator(); it.hasNext();) {\r
-            String s = (String) it.next();\r
-            object_index.put(s, new Integer(i++));\r
-            int common = UnicodeMap.findCommon(lastString, s); // runlength encode\r
-            lastString = s;\r
-            String piece = s.substring(common);\r
-            if (SHOW) System.out.println(common);\r
-            common <<= 1;\r
-            int inPool = trailingPool.getIndex(piece);\r
-            if (inPool < 0) {\r
-                writeUInt(common);\r
-                writeUTF(piece);\r
-                trailingPool.put(piece);\r
-            } else {\r
-                writeUInt(common | 1);\r
-                writeUInt(inPool);\r
-                if (SHOW) System.out.println("\t" + inPool);\r
-            }\r
-            if (SHOW) System.out.println("\t\t" + lastString);\r
-        }\r
-    }\r
-    \r
-    public static class WritePool {\r
-        private Map trailingPool = new HashMap();\r
-        private int poolCount = 0;\r
-        public int getIndex(Object o) {\r
-            Integer inPool = (Integer) trailingPool.get(o);\r
-            if (inPool == null) return -1;\r
-            return inPool.intValue();\r
-        }\r
-        public void put(Object o) {\r
-            trailingPool.put(o, new Integer(poolCount++));\r
-        }\r
-    }\r
-\r
-    /**\r
-     * @throws IOException\r
-     * \r
-     */\r
-    public void writeCollection(Collection c, Map object_index) throws IOException {\r
-        writeUInt(c.size());\r
-        int i = 0;\r
-        object_index.put(null, new Integer(i++));\r
-        for (Iterator it = c.iterator(); it.hasNext();) {\r
-            Object s = it.next();\r
-            dataOutput.writeObject(s);\r
-            if (object_index != null) object_index.put(s, new Integer(i++));\r
-        }\r
-    }\r
-}\r
-\r
-//#endif\r
+//##header J2SE15
+//#if defined(FOUNDATION10) || defined(J2SE13)
+//#else
+/*
+ *******************************************************************************
+ * Copyright (C) 1996-2009, International Business Machines Corporation and    *
+ * others. All Rights Reserved.                                                *
+ *******************************************************************************
+ */
+package com.ibm.icu.dev.test.util;
+
+import java.io.DataOutput;
+import java.io.IOException;
+import java.io.ObjectOutput;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Iterator;
+//import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.SortedSet;
+//import java.util.TreeSet;
+
+//import com.ibm.icu.impl.Utility;
+import com.ibm.icu.text.UTF16;
+
+/**
+ * Simple data output compressor. Nothing fancy, but much smaller footprint for ints and many strings.
+ */
+public final class DataOutputCompressor implements ObjectOutput {
+    static final boolean SHOW = false;
+
+    private ObjectOutput dataOutput;
+
+    public DataOutputCompressor(ObjectOutput dataOutput) {
+        this.dataOutput = dataOutput;
+    }
+
+    public DataOutput getDataOutput() {
+        return dataOutput;
+    }
+
+    public void setDataOutput(ObjectOutput dataOutput) {
+        this.dataOutput = dataOutput;
+    }
+
+    public void write(byte[] b) throws IOException {
+        dataOutput.write(b);
+    }
+
+    public void write(byte[] b, int off, int len) throws IOException {
+        dataOutput.write(b, off, len);
+    }
+
+    public void write(int b) throws IOException {
+        dataOutput.write(b);
+    }
+
+    public void writeBoolean(boolean v) throws IOException {
+        dataOutput.writeBoolean(v);
+    }
+
+    public void writeByte(int v) throws IOException {
+        dataOutput.writeByte(v);
+    }
+
+    public void writeBytes(String s) throws IOException {
+        dataOutput.writeBytes(s);
+    }
+
+    public void writeDouble(double v) throws IOException {
+        dataOutput.writeDouble(v);
+    }
+
+    public void writeFloat(float v) throws IOException {
+        dataOutput.writeFloat(v);
+    }
+
+    public void close() throws IOException {
+        dataOutput.close();
+    }
+    public void flush() throws IOException {
+        dataOutput.flush();
+    }
+    public String toString() {
+        return dataOutput.toString();
+    }
+    public void writeObject(Object obj) throws IOException {
+        dataOutput.writeObject(obj);
+    }
+    // ==== New Routines ====
+
+    public void writeChar(int v) throws IOException {
+        writeULong(v);
+    }
+
+    public void writeShort(int v) throws IOException {
+        writeLong(v);
+    }
+
+    public void writeUShort(int v) throws IOException {
+        writeULong(v);
+    }
+
+    public void writeInt(int v) throws IOException {
+        writeLong(v);
+    }
+
+    public void writeUInt(int v) throws IOException {
+        writeULong(v);
+    }
+
+    public void writeUTF(String str) throws IOException {
+        writeULong(UTF16.countCodePoint(str));
+        writeChars(str);
+    }
+
+    public void writeChars(String s) throws IOException {
+        int cp = 0;
+        for (int i = 0; i < s.length(); i += UTF16.getCharCount(cp)) {
+            cp = UTF16.charAt(s, i);
+            writeULong(cp);
+        }
+    }
+
+    public void writeLong(long v) throws IOException {
+        long flag = 0; // put sign bit at the bottom, and invert
+        if (v < 0) {
+            v = ~v;
+            flag = 1;
+        }
+        v <<= 1;
+        v |= flag;
+        while (true) {
+            if ((v & ~0x7FL) == 0) {
+                dataOutput.writeByte((byte) v);
+                break;
+            }
+            dataOutput.writeByte((byte) (0x80L | v));
+            v >>>= 7;
+        }
+    }
+
+    public void writeULong(long v) throws IOException {
+        while (true) { // write sequence of 7 bits, with top bit = 1 for continuation
+            if ((v & ~0x7FL) == 0) {
+                dataOutput.writeByte((byte) v);
+                break;
+            }
+            dataOutput.writeByte((byte) (0x80L | v));
+            v >>>= 7;
+        }
+    }
+
+    /**
+     * 
+     */
+    public void writeStringSet(SortedSet c, Map object_index) throws IOException {
+        if (SHOW) System.out.println("writeStringSet");
+        writeUInt(c.size());
+        int i = 0;
+        object_index.put(null, new Integer(i++));
+        WritePool trailingPool = new WritePool();
+        String lastString = "";
+        for (Iterator it = c.iterator(); it.hasNext();) {
+            String s = (String) it.next();
+            object_index.put(s, new Integer(i++));
+            int common = UnicodeMap.findCommon(lastString, s); // runlength encode
+            lastString = s;
+            String piece = s.substring(common);
+            if (SHOW) System.out.println(common);
+            common <<= 1;
+            int inPool = trailingPool.getIndex(piece);
+            if (inPool < 0) {
+                writeUInt(common);
+                writeUTF(piece);
+                trailingPool.put(piece);
+            } else {
+                writeUInt(common | 1);
+                writeUInt(inPool);
+                if (SHOW) System.out.println("\t" + inPool);
+            }
+            if (SHOW) System.out.println("\t\t" + lastString);
+        }
+    }
+    
+    public static class WritePool {
+        private Map trailingPool = new HashMap();
+        private int poolCount = 0;
+        public int getIndex(Object o) {
+            Integer inPool = (Integer) trailingPool.get(o);
+            if (inPool == null) return -1;
+            return inPool.intValue();
+        }
+        public void put(Object o) {
+            trailingPool.put(o, new Integer(poolCount++));
+        }
+    }
+
+    /**
+     * @throws IOException
+     * 
+     */
+    public void writeCollection(Collection c, Map object_index) throws IOException {
+        writeUInt(c.size());
+        int i = 0;
+        object_index.put(null, new Integer(i++));
+        for (Iterator it = c.iterator(); it.hasNext();) {
+            Object s = it.next();
+            dataOutput.writeObject(s);
+            if (object_index != null) object_index.put(s, new Integer(i++));
+        }
+    }
+}
+
+//#endif