📄 serializer.java
字号:
out.writeInt( size );
for (int i = 0; i < size; i++) {
serialize( vector.elementAt(i), out );
}
//#if polish.midp2
} else if (object instanceof Image) {
out.writeByte(TYPE_IMAGE);
Image image = (Image) object;
int width = image.getWidth();
int height = image.getHeight();
out.writeInt( width );
out.writeInt( height );
int[] rgb = new int[ width * height ];
image.getRGB(rgb, 0, width, 0, 0, width, height);
for (int i = 0; i < rgb.length; i++) {
out.writeInt( rgb[i] );
}
//#endif
//#if polish.midp
} else if (object instanceof Font) {
out.writeByte(TYPE_FONT);
Font font = (Font) object;
out.writeInt( font.getFace() );
out.writeInt( font.getStyle() );
out.writeInt( font.getSize() );
} else if (object instanceof Command) {
out.writeByte(TYPE_COMMAND);
Command command = (Command) object;
out.writeInt( command.getCommandType() );
out.writeInt( command.getPriority() );
out.writeUTF( command.getLabel() );
//#endif
} else {
throw new IOException("Cannot serialize " + object.getClass().getName() );
}
}
/*
* Special case is the javax.microedition.lcdui.Image 鈥?we use getRGB(int[] rgbData, int offset, int scanlength, int x, int y, int width, int height) and createRGBImage(int[] rgb, int width, int height, boolean processAlpha) for serialization.
*/
}
/**
* Deserializes an object from the given stream.
*
* @param in the data input stream, from which the object is deserialized
* @return the serializable object
* @throws IOException when serialization data could not be read or the Serializable class could not get instantiated
*/
public static Object deserialize( DataInputStream in )
throws IOException
{
byte version = in.readByte();
//#if polish.debug.warn
//# if (version > VERSION) {
//#debug warn
//# System.out.println("Warning: trying to deserialize class that has been serialized with a newer version (" + version + ">" + VERSION + ").");
//# }
//#endif
boolean isNull = in.readBoolean();
if (isNull) {
return null;
}
byte type = in.readByte();
switch (type) {
case TYPE_EXTERNALIZABLE:
String className = in.readUTF();
Externalizable extern = null;
try {
extern = (Externalizable) Class.forName( className ).newInstance();
} catch (Exception e) {
//#debug error
//# System.out.println("Unable to instantiate Serializable \"" + className + "\"" + e);
throw new IOException( e.toString() );
}
extern.read( in );
return extern;
case TYPE_EXTERNALIZABLE_ARRAY:
int length = in.readInt();
Externalizable[] externalizables = new Externalizable[ length ];
Class[] classes = new Class[ Math.max( length, 7 ) ];
Class currentClass;
byte idCounter = 0;
for (int i = 0; i < externalizables.length; i++) {
int classId = in.readByte();
if (classId == 0) { // new class name
className = in.readUTF();
try {
currentClass = Class.forName( className );
} catch (ClassNotFoundException e) {
//#debug error
//# System.out.println("Unable to load Serializable class \"" + className + "\"" + e);
throw new IOException( e.toString() );
}
if (idCounter > classes.length ) {
Class[] newClasses = new Class[ classes.length + 7 ];
System.arraycopy(classes, 0, newClasses, 0, classes.length);
classes = newClasses;
}
classes[idCounter] = currentClass;
idCounter++;
} else {
currentClass = classes[ classId ];
}
Externalizable externalizable;
try {
externalizable = (Externalizable) currentClass.newInstance();
externalizable.read(in);
externalizables[i] = externalizable;
} catch (Exception e) {
//#debug error
//# System.out.println("Unable to instantiate Serializable \"" + currentClass.getName() + "\"" + e);
throw new IOException( e.toString() );
}
}
return externalizables;
case TYPE_OBJECT_ARRAY:
length = in.readInt();
Object[] objects = new Object[ length ];
for (int i = 0; i < objects.length; i++) {
objects[i] = deserialize(in);
}
return objects;
case TYPE_BYTE:
return new Byte( in.readByte() );
case TYPE_SHORT:
return new Short( in.readShort() );
case TYPE_INTEGER:
return new Integer( in.readInt() );
case TYPE_LONG:
return new Long( in.readLong() );
//#if polish.hasFloatingPoint
//# case TYPE_FLOAT:
//# return new Float( in.readFloat() );
//# case TYPE_DOUBLE:
//# return new Double( in.readDouble() );
//#endif
case TYPE_STRING:
return in.readUTF();
case TYPE_STRING_BUFFER:
return new StringBuffer( in.readUTF() );
case TYPE_CHARACTER:
return new Character( in.readChar() );
case TYPE_BOOLEAN:
return new Boolean( in.readBoolean() );
case TYPE_DATE:
return new Date( in.readLong() );
case TYPE_CALENDAR:
Calendar calendar = Calendar.getInstance();
calendar.setTime( new Date(in.readLong()) );
return calendar;
case TYPE_RANDOM:
return new Random();
case TYPE_HASHTABLE:
int size = in.readInt();
Hashtable hashtable = new Hashtable( size );
for (int i = 0; i < size; i++) {
Object key = deserialize(in);
Object value = deserialize(in);
hashtable.put( key, value );
}
return hashtable;
case TYPE_STACK:
case TYPE_VECTOR:
size = in.readInt();
Vector vector;
if (type == TYPE_STACK) {
vector = new Stack();
} else {
vector = new Vector( size );
}
for (int i = 0; i < size; i++) {
Object value = deserialize(in);
vector.addElement( value );
}
return vector;
//#if polish.midp2
case TYPE_IMAGE:
int width = in.readInt();
int height = in.readInt();
int[] rgb = new int[ width * height ];
for (int i = 0; i < rgb.length; i++) {
rgb[i] = in.readInt();
}
return Image.createRGBImage(rgb, width, height, true );
//#endif
//#if polish.midp
case TYPE_FONT:
int face = in.readInt();
int style = in.readInt();
size = in.readInt();
return Font.getFont(face, style, size);
case TYPE_COMMAND:
int cmdType = in.readInt();
int priority = in.readInt();
String label = in.readUTF();
return new Command( label, cmdType, priority );
//#endif
default:
throw new IOException("Unknown type: " + type );
}
}
// /**
// * Deserializes an object array from the given stream.
// *
// * @return the serializable object
// * @param in the data input stream, from which the object is deserialized
// * @throws IOException when serialization data could not be read or the Serializable class could not get instantiated
// */
// public static Serializable[] deserializeArray( DataInputStream in )
// throws IOException
// {
// byte version = in.readByte();
// //#if polish.debug.warn
// if (version > VERSION) {
// //#debug warn
// System.out.println("Warning: trying to deserialize array that has been serialized with a newer version (" + version + ">" + VERSION + ").");
// }
// //#endif
// boolean isNull = in.readBoolean();
// if (isNull) {
// return null;
// }
// int length = in.readInt();
// Serializable[] serializables = new Serializable[ length ];
// if ( length > 0 ) {
// String className = in.readUTF();
// try {
// Class serialClass = Class.forName( className );
// for (int i = 0; i < serializables.length; i++) {
// Externalizable extern = (Externalizable) serialClass.newInstance();
// extern.read( in );
// serializables[i] = extern;
// }
// } catch (IOException e) {
// //#debug error
// System.out.println("Unable to deserlize array of \"" + className + "\"" + e );
// throw e;
// } catch (Exception e) {
// //#debug error
// System.out.println("Unable to deserlize array of \"" + className + "\"" + e );
// throw new IOException( e.toString() );
// }
// }
// return serializables;
// }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -