ICU-12450 move com.ibm.icu.dev.util.DataInputCompressor & DataOutputCompressor to org.unicode.unused
X-SVN-Rev: 38647
This commit is contained in:
parent
0157724f4a
commit
f99b4ece25
@ -1,229 +0,0 @@
|
||||
/*
|
||||
*******************************************************************************
|
||||
* Copyright (C) 1996-2012, International Business Machines Corporation and *
|
||||
* others. All Rights Reserved. *
|
||||
*******************************************************************************
|
||||
*/
|
||||
package com.ibm.icu.dev.util;
|
||||
|
||||
import java.io.DataInput;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInput;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
|
||||
import com.ibm.icu.text.UTF16;
|
||||
|
||||
/**
|
||||
* Simple data input compressor. Nothing fancy, but much smaller footprint for
|
||||
* ints and many strings.
|
||||
*/
|
||||
public final class DataInputCompressor implements ObjectInput {
|
||||
static final boolean SHOW = false;
|
||||
|
||||
private ObjectInput dataInput;
|
||||
|
||||
private transient StringBuffer stringBuffer = new StringBuffer();
|
||||
|
||||
public DataInputCompressor(ObjectInput dataInput) {
|
||||
this.dataInput = dataInput;
|
||||
}
|
||||
|
||||
public DataInput getDataInput() {
|
||||
return dataInput;
|
||||
}
|
||||
|
||||
public void setDataInput(ObjectInput dataInput) {
|
||||
this.dataInput = dataInput;
|
||||
}
|
||||
|
||||
public boolean readBoolean() throws IOException {
|
||||
return dataInput.readBoolean();
|
||||
}
|
||||
|
||||
public byte readByte() throws IOException {
|
||||
return dataInput.readByte();
|
||||
}
|
||||
|
||||
public int readUnsignedByte() throws IOException {
|
||||
return dataInput.readUnsignedByte();
|
||||
}
|
||||
|
||||
public double readDouble() throws IOException {
|
||||
return dataInput.readDouble();
|
||||
}
|
||||
|
||||
public float readFloat() throws IOException {
|
||||
return dataInput.readFloat();
|
||||
}
|
||||
|
||||
public void readFully(byte[] b) throws IOException {
|
||||
dataInput.readFully(b);
|
||||
}
|
||||
|
||||
public void readFully(byte[] b, int off, int len) throws IOException {
|
||||
dataInput.readFully(b, off, len);
|
||||
}
|
||||
|
||||
public int skipBytes(int n) throws IOException {
|
||||
return dataInput.skipBytes(n);
|
||||
}
|
||||
|
||||
public String readLine() throws IOException {
|
||||
return dataInput.readLine();
|
||||
}
|
||||
|
||||
public int available() throws IOException {
|
||||
return dataInput.available();
|
||||
}
|
||||
public void close() throws IOException {
|
||||
dataInput.close();
|
||||
}
|
||||
public int read() throws IOException {
|
||||
return dataInput.read();
|
||||
}
|
||||
public int read(byte[] b) throws IOException {
|
||||
return dataInput.read(b);
|
||||
}
|
||||
public int read(byte[] b, int off, int len) throws IOException {
|
||||
return dataInput.read(b, off, len);
|
||||
}
|
||||
public Object readObject() throws ClassNotFoundException, IOException {
|
||||
return dataInput.readObject();
|
||||
}
|
||||
public long skip(long n) throws IOException {
|
||||
return dataInput.skip(n);
|
||||
}
|
||||
public String toString() {
|
||||
return dataInput.toString();
|
||||
}
|
||||
// ==== New Routines ====
|
||||
|
||||
public char readChar() throws IOException {
|
||||
return (char) readULong();
|
||||
}
|
||||
|
||||
public short readShort() throws IOException {
|
||||
return (short) readLong();
|
||||
}
|
||||
|
||||
public int readUnsignedShort() throws IOException {
|
||||
return (int) readULong();
|
||||
}
|
||||
|
||||
public int readUShort() throws IOException {
|
||||
return (int) readULong();
|
||||
}
|
||||
|
||||
public int readInt() throws IOException {
|
||||
return (int) readLong();
|
||||
}
|
||||
|
||||
public int readUInt() throws IOException {
|
||||
return (int) readULong();
|
||||
}
|
||||
|
||||
public String readChars(int len) throws IOException {
|
||||
stringBuffer.setLength(0);
|
||||
for (int i = 0; i < len; ++i) {
|
||||
int cp = (int) readULong();
|
||||
UTF16.append(stringBuffer, cp);
|
||||
}
|
||||
return stringBuffer.toString();
|
||||
}
|
||||
|
||||
public String readUTF() throws IOException {
|
||||
int len = (int) readULong();
|
||||
return readChars(len);
|
||||
}
|
||||
|
||||
public long readLong() throws IOException {
|
||||
long result = 0;
|
||||
int offset = 0;
|
||||
while (true) {
|
||||
long input = readByte();
|
||||
result |= (input & 0x7F) << offset;
|
||||
if ((input & 0x80) == 0)
|
||||
break;
|
||||
offset += 7;
|
||||
}
|
||||
boolean negative = (result & 1) != 0; // get sign bit from the bottom,
|
||||
// and invert
|
||||
result >>>= 1;
|
||||
if (negative)
|
||||
result = ~result;
|
||||
return result;
|
||||
}
|
||||
|
||||
public long readULong() throws IOException {
|
||||
long result = 0;
|
||||
int offset = 0;
|
||||
while (true) { // read sequence of 7 bits, with top bit = 1 for
|
||||
// continuation
|
||||
int input = readByte();
|
||||
result |= (input & 0x7F) << offset;
|
||||
if ((input & 0x80) == 0)
|
||||
return result;
|
||||
offset += 7;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public Object[] readStringSet(Collection availableValues)
|
||||
throws IOException {
|
||||
int size = readUInt();
|
||||
if (SHOW) System.out.println("readStringSet");
|
||||
Object[] valuesList = new Object[size + 1];
|
||||
// first item is null
|
||||
String lastString = "";
|
||||
ReadPool trailingPool = new ReadPool();
|
||||
for (int i = 0; i < size; ++i) {
|
||||
int common = readUInt();
|
||||
boolean inPool = (common & 1) != 0;
|
||||
common >>>= 1;
|
||||
if (SHOW) System.out.println(common);
|
||||
String current;
|
||||
if (inPool) {
|
||||
int poolIndex = readUInt();
|
||||
if (SHOW) System.out.println("\t" + poolIndex);
|
||||
current = (String) trailingPool.get(poolIndex);
|
||||
} else {
|
||||
current = readUTF();
|
||||
trailingPool.add(current);
|
||||
}
|
||||
valuesList[i + 1] = lastString = lastString.substring(0, common)
|
||||
+ current;
|
||||
if (SHOW) System.out.println("\t\t" + lastString);
|
||||
if (availableValues != null) availableValues.add(current);
|
||||
}
|
||||
return valuesList;
|
||||
}
|
||||
|
||||
public static class ReadPool {
|
||||
private List trailingPool = new ArrayList();
|
||||
public Object get(int index) {
|
||||
return trailingPool.get(index);
|
||||
}
|
||||
public void add(Object o) {
|
||||
trailingPool.add(o);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws IOException
|
||||
* @throws ClassNotFoundException
|
||||
*
|
||||
*/
|
||||
public Object[] readCollection(LinkedHashSet availableValues) throws ClassNotFoundException, IOException {
|
||||
int size = readUInt();
|
||||
Object[] valuesList = new Object[size + 1];
|
||||
for (int i = 0; i < size; ++i) {
|
||||
valuesList[i + 1] = readObject();
|
||||
}
|
||||
return valuesList;
|
||||
}
|
||||
}
|
@ -1,207 +0,0 @@
|
||||
/*
|
||||
*******************************************************************************
|
||||
* Copyright (C) 1996-2012, International Business Machines Corporation and *
|
||||
* others. All Rights Reserved. *
|
||||
*******************************************************************************
|
||||
*/
|
||||
package com.ibm.icu.dev.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.Map;
|
||||
import java.util.SortedSet;
|
||||
|
||||
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.findCommonPrefix(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++));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user