Externalizable extern = (Externalizable) serializable; out.writeUTF( extern.getClass().getName() ); extern.write( out );
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 {  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() );  } 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() );  } 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) { if (object instanceof Stack) { out.writeByte(TYPE_STACK); } else { out.writeByte(TYPE_VECTOR); } Vector vector = (Vector) object; int size = vector.size(); out.writeInt( size ); for (int i = 0; i < size; i++) { serialize( vector.elementAt(i), out ); }  } 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] ); }   } 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() );  } else { throw new IOException("Cannot serialize " + object.getClass().getName() ); }
	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 );				}	}