📄 serialhashtable.java
字号:
package inline.sys;
import java.util.*;
import javax.microedition.rms.*;
import java.io.*;
public class SerialHashtable extends Hashtable
{
private final static byte SH_TYPE_STRING = 100;
private final static byte SH_TYPE_INT = 101;
private final static byte SH_TYPE_LONG = 102;
private final static byte SH_TYPE_BYTE = 103;
private final static byte SH_ARRAY_NO = 20;
private final static byte SH_ARRAY_YES = 21;
public SerialHashtable()
{
initDefaults();
}
protected void initDefaults() // please override me
{
}
protected final byte[] serializeBytes() throws IOException
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
serializeStream(dos);
dos.close();
return baos.toByteArray();
}
// write self to stream
public final void serializeStream(DataOutputStream dos) throws IOException
{
for(Enumeration enumeration = this.keys();
enumeration.hasMoreElements();
)
{
String key = (String)enumeration.nextElement();
// don't process string elements with first _
// they're temporary
/*#!MakeResources#*///<editor-fold>
if (key.startsWith("_")) continue;
/*$!MakeResources$*///</editor-fold>
//System.err.println(this+":"+key);
dos.writeUTF(key);
Object obj = this.get(key);
Object[] objs = {obj};
byte[] bytes = {};
byte type = 0;
if (obj.getClass().isArray())
{
/*
* How shame there's no Array object in CLDC 1.0/1.1 :-(
*/
int sz;
if (obj.getClass().getName().startsWith("[B"))
{
bytes = (byte[])obj;
sz = bytes.length;
type = SH_TYPE_BYTE;
}
else
{
objs = (Object[])obj;
sz = objs.length;
}
dos.writeByte(SH_ARRAY_YES);
dos.writeInt(sz);
if (objs.length==0) continue;
}
else
{
dos.writeByte(SH_ARRAY_NO);
}
if (objs[0] instanceof String)
{
type = SH_TYPE_STRING;
}
else if (objs[0] instanceof Integer)
{
type = SH_TYPE_INT;
}
else if (objs[0] instanceof Long)
{
type = SH_TYPE_LONG;
}
else if (type == SH_TYPE_BYTE)
{
}
else
{
System.err.println("Unsupported hashtable object");
}
dos.writeByte(type);
for(int i=0;i<objs.length;i++)
{
if (type == SH_TYPE_STRING)
{
String s = (String)objs[i];
if (s==null) s = "\u001b";
dos.writeUTF(s);
}
else if (type == SH_TYPE_INT)
{
Integer ii = (Integer)objs[i]; // harsh
dos.writeInt(ii.intValue());
}
else if (type == SH_TYPE_LONG)
{
Long ll = (Long)objs[i]; // harsh
dos.writeLong(ll.longValue());
}
}
if (type == SH_TYPE_BYTE)
{
dos.write(bytes);
}
}
}
protected final void deserializeBytes(byte[] ab) throws IOException
{
ByteArrayInputStream bais = new ByteArrayInputStream(ab);
DataInputStream dis = new DataInputStream(bais);
deserializeStream(dis);
dis.close();
bais.close();
}
// load self from stream
public final void deserializeStream(DataInputStream dis) throws IOException
{
while(true)
{
String key;
try
{
key = dis.readUTF();
}
catch(EOFException eofe)
{
//that's Ok. as far we red all what we had
return;
}
byte isArray = dis.readByte();
int size = 1;
if (isArray==SH_ARRAY_YES)
{
size = dis.readInt();
if (size == 0)
{
continue;
}
}
byte type = dis.readByte();
Object[] objs = {};
if (type == SH_TYPE_STRING)
{
objs = new String[size];
}
else if (type == SH_TYPE_INT)
{
objs = new Integer[size];
}
else if (type == SH_TYPE_LONG)
{
objs = new Long[size];
}
else if (type == SH_TYPE_BYTE)
{
byte[] bytes = new byte[size];
dis.read(bytes);
size = 0;
put(key, bytes);
}
else
{
System.err.println("Unsupported serial object");
}
//System.err.println(this.getClass().toString()+":key="+key+", size="+size+", type="+type);
for(int i=0;i<size;i++)
{
if (type == SH_TYPE_STRING)
{
objs[i] = dis.readUTF();
String x = (String)objs[i];
if (x.compareTo("\u001b")==0) objs[i] = null;
}
else if (type == SH_TYPE_INT)
{
objs[i] = new Integer(dis.readInt());
}
else if (type == SH_TYPE_LONG)
{
objs[i] = new Long(dis.readLong());
}
}
if (isArray==SH_ARRAY_YES)
{
if (size>0) //escape for _BYTE (it is not Object)
{
put(key, objs);
}
}
else
{
put(key, objs[0]);
}
}
}
public final int getIntDefault(String key, int def)
{
Object obj = this.get(key);
int gt = def;
if (obj!=null && obj instanceof Integer)
{
Integer ii = (Integer)obj;
gt = ii.intValue();
}
return gt;
}
public final void putInt(String key, int value)
{
this.put(key,new Integer(value));
//System.err.println(key+"="+value);
}
public final void loadFromInputStream(InputStream is)
{
if (is!=null)
{
try
{
DataInputStream dis = new DataInputStream(is);
deserializeStream(dis);
dis.close();
}
catch(Exception e)
{
Log.fire("Can't load sht from is");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -