📄 serializationmanager.java
字号:
/* * @(#)$Id: SerializationManager.java,v 1.15 2005/03/07 17:24:44 huebsch Exp $ * * Copyright (c) 2001-2004 Regents of the University of California. * All rights reserved. * * This file is distributed under the terms in the attached BERKELEY-LICENSE * file. If you do not find these files, copies can be found by writing to: * Computer Science Division, Database Group, Universite of California, * 617 Soda Hall #1776, Berkeley, CA 94720-1776. Attention: Berkeley License * * Copyright (c) 2003-2004 Intel Corporation. All rights reserved. * * This file is distributed under the terms in the attached INTEL-LICENSE file. * If you do not find these files, copies can be found by writing to: * Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, * Berkeley, CA, 94704. Attention: Intel License Inquiry. */package util.network.serialization;import org.apache.log4j.Logger;import services.Output;import services.network.Payload;import util.BitID;import util.logging.LogMessage;import java.io.ObjectStreamClass;import java.lang.reflect.Constructor;import java.math.BigDecimal;import java.math.BigInteger;import java.net.InetAddress;import java.net.InetSocketAddress;import java.sql.Time;import java.sql.Timestamp;import java.util.*;/** * Class SerializationManager * */public class SerializationManager { private static Logger logger = Logger.getLogger(SerializationManager.class); private static Hashtable classes; // Primitives (1-8 Reserved) public static final int SERIALUID_BOOLEAN = 1; public static final int SERIALUID_BYTE = 2; public static final int SERIALUID_CHARACTER = 3; public static final int SERIALUID_DOUBLE = 4; public static final int SERIALUID_FLOAT = 5; public static final int SERIALUID_INTEGER = 6; public static final int SERIALUID_LONG = 7; public static final int SERIALUID_SHORT = 8; // Common Types (9-64 Reserved) public static final int SERIALUID_BIGDECIMAL = 9; public static final int SERIALUID_BIGINTEGER = 10; public static final int SERIALUID_DATE = 11; public static final int SERIALUID_INETADDRESS = 12; public static final int SERIALUID_INETSOCKETADDRESS = 13; public static final int SERIALUID_STRING = 14; public static final int SERIALUID_TIME = 15; public static final int SERIALUID_TIMESTAMP = 16; // Common Data Strucutres (65-96 Reserved) public static final int SERIALUID_VECTOR = 65; public static final int SERIALUID_ARRAYLIST = 66; public static final int SERIALUID_HASHTABLE = 67; public static final int SERIALUID_HASHMAP = 68; public static final int SERIALUID_BYTEARRAY = 69; // Common Data Strucutres (97-128 Reserved) public static final int SERIALUID_BITID = 97; // Settings public static boolean USE_REAL_SIZE = true; public static boolean DEBUG_SIZE_WARNING = false; /** * Method getPayloadSize * * @param theObject * @return */ public static int getPayloadSize(Payload theObject) { if (USE_REAL_SIZE) { CountingByteBuffer buffer = new CountingByteBuffer(); serialize(buffer, theObject); int size = buffer.position(); if (DEBUG_SIZE_WARNING) { int estSize = theObject.getSize(); if ((estSize != size) && (Output.debuggingEnabled)) { logger.debug(new LogMessage(new Object[]{ "WARNING: Size mistach for:", theObject.getClass().getName(), ", Actual:", String.valueOf(size), ", Estimated:", String.valueOf(estSize)})); } } return size; } else { return theObject.getSize(); } } /** * Method serialize * * @param outputBuffer * @param theObject * @return */ public static long serialize(GenericByteBuffer outputBuffer, Payload theObject) { int startingPos = outputBuffer.position(); long serialUID = serialize(outputBuffer, theObject, 0L); outputBuffer.putLong(startingPos, serialUID); return serialUID; } /** * Method serializeNoHeader * * @param outputBuffer * @param theObject * @return */ public static long serializeNoHeader(GenericByteBuffer outputBuffer, Payload theObject) { long serialUID = serialize(outputBuffer, theObject, 0L); return serialUID; } /** * Add the payload object to the GenericByteBuffer with the proper header. Returns the UID * * @param outputBuffer * @param theObject * @param serialUID * @return */ public static long serialize(GenericByteBuffer outputBuffer, Payload theObject, long serialUID) { outputBuffer.putLong(serialUID); if (theObject != null) { return theObject.serialize(outputBuffer); } else { return 0L; } } /** * Method serializeExtended * * @param outputBuffer * @param theObject * @return */ public static long serializeExtended(GenericByteBuffer outputBuffer, Object theObject) { int startingPos = outputBuffer.position(); long serialUID = serializeExtended(outputBuffer, theObject, 0L); outputBuffer.putLong(startingPos, serialUID); return serialUID; } /** * Add the object to the GenericByteBuffer with the proper header. Returns the UID * * @param outputBuffer * @param theObject * @param serialUID * @return */ public static long serializeExtended(GenericByteBuffer outputBuffer, Object theObject, long serialUID) { if (theObject instanceof Payload) { return serialize(outputBuffer, (Payload) theObject, serialUID); } outputBuffer.putLong(serialUID); if (theObject == null) { return 0L; } if (theObject instanceof Boolean) { SerializeBoolean.serialize(outputBuffer, (Boolean) theObject); return SERIALUID_BOOLEAN; } if (theObject instanceof Byte) { SerializeByte.serialize(outputBuffer, (Byte) theObject); return SERIALUID_BYTE; } if (theObject instanceof Character) { SerializeCharacter.serialize(outputBuffer, (Character) theObject); return SERIALUID_CHARACTER; } if (theObject instanceof Double) { SerializeDouble.serialize(outputBuffer, (Double) theObject); return SERIALUID_DOUBLE; } if (theObject instanceof Float) { SerializeFloat.serialize(outputBuffer, (Float) theObject); return SERIALUID_FLOAT; } if (theObject instanceof Integer) { SerializeInteger.serialize(outputBuffer, (Integer) theObject); return SERIALUID_INTEGER; } if (theObject instanceof Long) { SerializeLong.serialize(outputBuffer, (Long) theObject); return SERIALUID_LONG; } if (theObject instanceof Short) { SerializeShort.serialize(outputBuffer, (Short) theObject); return SERIALUID_SHORT; } if (theObject instanceof BigDecimal) { SerializeBigDecimal.serialize(outputBuffer, (BigDecimal) theObject); return SERIALUID_BIGDECIMAL; } if (theObject instanceof BigInteger) { SerializeBigInteger.serialize(outputBuffer, (BigInteger) theObject); return SERIALUID_BIGINTEGER; } if (theObject instanceof Date) { SerializeDate.serialize(outputBuffer, (Date) theObject); return SERIALUID_DATE; } if (theObject instanceof InetAddress) { SerializeInetAddress.serialize(outputBuffer, (InetAddress) theObject); return SERIALUID_INETADDRESS; } if (theObject instanceof InetSocketAddress) { SerializeInetSocketAddress.serialize(outputBuffer, (InetSocketAddress) theObject); return SERIALUID_INETSOCKETADDRESS; } if (theObject instanceof String) { SerializeString.serialize(outputBuffer, (String) theObject); return SERIALUID_STRING; } if (theObject instanceof Time) { SerializeTime.serialize(outputBuffer, (Time) theObject);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -