⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serializer.java

📁 j2me polish学习的经典代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Created on Mar 31, 2006 at 4:18:12 PM.
 * 
 * Copyright (c) 2006 Robert Virkus / Enough Software
 *
 * This file is part of J2ME Polish.
 *
 * J2ME Polish is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * J2ME Polish is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with J2ME Polish; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * Commercial licenses are also available, please
 * refer to the accompanying LICENSE.txt or visit
 * http://www.j2mepolish.org for details.
 */
package de.enough.polish.io;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Random;
import java.util.Stack;
import java.util.Vector;

//#if polish.midp
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Font;
//#endif
//#if polish.midp2
import javax.microedition.lcdui.Image;
//#endif

/**
 * <p>The serializer class is used for serializing and de-serializing objects in a unified way.</p>
 * <p>High level serialization components like the RmsStorage use this helper class for the actual serialization.</p>
 *
 * <p>Copyright Enough Software 2006</p>
 * <pre>
 * history
 *        Mar 31, 2006 - rob creation
 * </pre>
 * @author Robert Virkus, j2mepolish@enough.de
 */
public final class Serializer {
	
	private static final byte VERSION = 1; // version starts at 1 and is then increased up to 127 when incompatible changes occur
	private static final byte TYPE_EXTERNALIZABLE = 0;
	private static final byte TYPE_EXTERNALIZABLE_ARRAY = 1;
	private static final byte TYPE_OBJECT_ARRAY = 2;
	private static final byte TYPE_BYTE = 3;
	private static final byte TYPE_SHORT = 4;
	private static final byte TYPE_INTEGER = 5;
	private static final byte TYPE_LONG = 6;
	private static final byte TYPE_FLOAT = 7;
	private static final byte TYPE_DOUBLE = 8;
	private static final byte TYPE_STRING = 9;
	private static final byte TYPE_STRING_BUFFER = 10;
	private static final byte TYPE_CHARACTER = 11;
	private static final byte TYPE_BOOLEAN = 12;
	private static final byte TYPE_DATE = 13;
	private static final byte TYPE_CALENDAR = 14;
	private static final byte TYPE_RANDOM = 15;
	private static final byte TYPE_HASHTABLE = 16;
	private static final byte TYPE_STACK = 17;
	private static final byte TYPE_VECTOR = 18;
	private static final byte TYPE_IMAGE = 19;
	private static final byte TYPE_FONT = 20;
	private static final byte TYPE_COMMAND = 21;
	
	private Serializer() {
		// no instantiation allowed
	}
	
	
//	/**
//	 * Serializes the given object.
//	 * 
//	 * @param serializable the serializable object or null
//	 * @param out the data output stream, into which the object is serialized
//	 * @throws IOException when serialization data could not be written
//	 */
//	public static void serialize( Serializable serializable, DataOutputStream out )
//	throws IOException
//	{
//		out.writeByte( VERSION );
//		boolean isNull = (serializable == null);
//		out.writeBoolean( isNull );
//		if ( !isNull ) {
//			Externalizable extern = (Externalizable) serializable;
//			out.writeUTF( extern.getClass().getName() );
//			extern.write( out );		
//		}
//	}
	
//	/**
//	 * Serializes the given array.
//	 * <b>WARNING: </b> The specified array elements need to be of the same type, this is not checked during runtime!
//	 * 
//	 * @param serializables the array with serializable objects of the same type or null
//	 * @param out the data output stream, into which the objects are serialized
//	 * @throws IOException when serialization data could not be written
//	 */
//	public static void serializeArray( Serializable[] serializables, DataOutputStream out )
//	throws IOException
//	{
//		out.writeByte( VERSION );
//		boolean isNull = (serializables == null);
//		out.writeBoolean( isNull );
//		if ( !isNull ) {
//			out.writeInt( serializables.length );
//			if ( serializables.length > 0 ) {
//				Serializable serializable = serializables[0];
//				out.writeUTF( serializable.getClass().getName() );
//				for (int i = 0; i < serializables.length; i++) {
//					Externalizable extern = (Externalizable) serializables[i];
//					out.writeUTF( extern.getClass().getName() );
//					extern.write( out );		
//				}			
//			}
//		}
//	}
	/**
	 * Serializes an Object like java.lang.Integer, java.util.Date, javax.microedition.lcdui.Image etc.
	 * 
	 * @param object the object
	 * @param out the stream into which the object should be serialized
	 * @throws IOException when serialization data could not be written or when encountering an object that cannot be serialized
	 * @see #deserialize(DataInputStream) for deseralizing objects
	 */
	public static void serialize( Object object, DataOutputStream out ) 
	throws IOException 
	{
		out.writeByte( VERSION );
		boolean isNull = (object == null);
		out.writeBoolean( isNull );
		if ( !isNull ) {
			if (object instanceof Externalizable) { 
				out.writeByte(TYPE_EXTERNALIZABLE);
				out.writeUTF( object.getClass().getName() );
				((Externalizable)object).write(out);
			} else if (object instanceof Externalizable[]) { 
				out.writeByte(TYPE_EXTERNALIZABLE_ARRAY);
				Externalizable[] externalizables = (Externalizable[]) object;
				out.writeInt( externalizables.length );
				Hashtable classNames = new Hashtable();
				Class lastClass = null;
				byte lastId = 0;
				byte idCounter = 0;
				for (int i = 0; i < externalizables.length; i++) {
					Externalizable externalizable = externalizables[i];
					Class currentClass = externalizable.getClass();
					if (currentClass == lastClass ) {
						out.writeByte( lastId );
					} else {
						Byte knownId = (Byte) classNames.get( currentClass );
						if (knownId != null) {
							out.writeByte( knownId.byteValue() );
						} else {
							// this is a class that has not yet been encountered:
							out.writeByte( 0 );
							idCounter++;
							out.writeUTF( currentClass.getName() );
							lastClass = currentClass;
							lastId = idCounter;
							classNames.put( currentClass, new Byte( lastId ) );
						}
					}
					externalizable.write(out);
				}
				((Externalizable)object).write(out);
			} else if (object instanceof Object[]) { 
				out.writeByte(TYPE_OBJECT_ARRAY);
				Object[] objects = (Object[]) object;
				out.writeInt( objects.length );
				for (int i = 0; i < objects.length; i++) {
					Object obj = objects[i];
					serialize(obj, out);
				}
			} else if (object instanceof Byte) {
				out.writeByte(TYPE_BYTE);
				out.writeByte( ((Byte)object).byteValue() );
			} else if (object instanceof Short) {
				out.writeByte(TYPE_SHORT);
				out.writeShort( ((Short)object).shortValue() );
			} else if (object instanceof Integer) {
				out.writeByte(TYPE_INTEGER);
				out.writeInt( ((Integer)object).intValue() );
			} else if (object instanceof Long) {
				out.writeByte(TYPE_LONG);
				out.writeLong( ((Long)object).longValue() );
			//#if polish.hasFloatingPoint
			//# } else if (object instanceof Float) {
				//# out.writeByte(TYPE_FLOAT);
				//# out.writeFloat( ((Float)object).floatValue() );
			//# } else if (object instanceof Double) {
				//# out.writeByte(TYPE_DOUBLE);
				//# out.writeDouble( ((Double)object).doubleValue() );
			//#endif
			} else if (object instanceof String) {
				out.writeByte(TYPE_STRING);
				out.writeUTF( (String)object );
			} else if (object instanceof StringBuffer) {
				out.writeByte(TYPE_STRING_BUFFER);
				out.writeUTF( ((StringBuffer)object).toString()  );
			} else if (object instanceof Character) {
				out.writeByte(TYPE_CHARACTER);
				out.writeChar( ((Character)object).charValue() );
			} else if (object instanceof Boolean) {
				out.writeByte(TYPE_BOOLEAN);
				out.writeBoolean( ((Boolean)object).booleanValue() );
			} else if (object instanceof Date) {
				out.writeByte(TYPE_DATE);
				out.writeLong( ((Date)object).getTime() );
			} else if (object instanceof Calendar) {
				out.writeByte(TYPE_CALENDAR);
				out.writeLong( ((Calendar)object).getTime().getTime() );
			} else if (object instanceof Random) {
				out.writeByte(TYPE_RANDOM);
			} else if (object instanceof Hashtable) {
				out.writeByte(TYPE_HASHTABLE);
				Hashtable table = (Hashtable) object;
				out.writeInt( table.size() );
				Enumeration enumeration = table.keys();
				while( enumeration.hasMoreElements() ) {
					Object key = enumeration.nextElement();
					serialize(key, out);
					Object value = table.get( key );
					serialize(value, out);
				}
			} else if (object instanceof Vector) { // also serializes stacks
				if (object instanceof Stack) {
					out.writeByte(TYPE_STACK);					
				} else {
					out.writeByte(TYPE_VECTOR);
				}
				Vector vector = (Vector) object;
				int size = vector.size();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -