📄 serializearray.java
字号:
/* * @(#)$Id: SerializeArray.java,v 1.9 2004/07/02 23:59:22 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 services.network.Payload;/** * Class SerializeArray * */public class SerializeArray { public static final byte NULL_VALUE = -1; /** * Method serializePayload * * @param outputBuffer * @param theObject */ public static void serializePayload(GenericByteBuffer outputBuffer, Payload[] theObject) { int arraySize = (theObject == null) ? NULL_VALUE : theObject.length; outputBuffer.putInt(arraySize); for (int i = 0; i < arraySize; i++) { SerializationManager.serialize(outputBuffer, theObject[i]); } } /** * Method serializeHomogenousPayload * * @param outputBuffer * @param theObject */ public static void serializeHomogenousPayload( GenericByteBuffer outputBuffer, Payload[] theObject) { long serialUID = 0; if ((theObject != null) && (theObject.length > 0)) { serialUID = SerializationManager.getSerialUID( theObject[0].getClass().getName()); } outputBuffer.putLong(serialUID); int arraySize = (theObject == null) ? NULL_VALUE : theObject.length; outputBuffer.putInt(arraySize); for (int i = 0; i < arraySize; i++) { SerializationManager.serializeNoHeader(outputBuffer, theObject[i]); } } /** * Method serializeExtendedObject * * @param outputBuffer * @param theObject */ public static void serializeExtendedObject(GenericByteBuffer outputBuffer, Object[] theObject) { int arraySize = (theObject == null) ? NULL_VALUE : theObject.length; outputBuffer.putInt(arraySize); for (int i = 0; i < arraySize; i++) { SerializationManager.serializeExtended(outputBuffer, theObject[i]); } } /** * Method serializeHomogenousExtendedObject * * @param outputBuffer * @param theObject */ public static void serializeHomogenousExtendedObject( GenericByteBuffer outputBuffer, Object[] theObject) { serializeExtendedObject(outputBuffer, theObject); } /** * Method serializeBoolean * * @param outputBuffer * @param theObject */ public static void serializeBoolean(GenericByteBuffer outputBuffer, boolean[] theObject) { int arraySize = (theObject == null) ? NULL_VALUE : theObject.length; outputBuffer.putInt(arraySize); for (int i = 0; i < arraySize; i++) { if (theObject[i]) { outputBuffer.put((byte) 1); } else { outputBuffer.put((byte) 0); } } } /** * Method serializeByte * * @param outputBuffer * @param theObject */ public static void serializeByte(GenericByteBuffer outputBuffer, byte[] theObject) { int arraySize = (theObject == null) ? NULL_VALUE : theObject.length; outputBuffer.putInt(arraySize); if (theObject != null) { outputBuffer.put(theObject); } } /** * Method serializeChar * * @param outputBuffer * @param theObject */ public static void serializeChar(GenericByteBuffer outputBuffer, char[] theObject) { int arraySize = (theObject == null) ? NULL_VALUE : theObject.length; outputBuffer.putInt(arraySize); for (int i = 0; i < arraySize; i++) { outputBuffer.putChar(theObject[i]); } } /** * Method serializeDouble * * @param outputBuffer * @param theObject */ public static void serializeDouble(GenericByteBuffer outputBuffer, double[] theObject) { int arraySize = (theObject == null) ? NULL_VALUE : theObject.length; outputBuffer.putInt(arraySize); for (int i = 0; i < arraySize; i++) { outputBuffer.putDouble(theObject[i]); } } /** * Method serializeFloat * * @param outputBuffer * @param theObject */ public static void serializeFloat(GenericByteBuffer outputBuffer, float[] theObject) { int arraySize = (theObject == null) ? NULL_VALUE : theObject.length; outputBuffer.putInt(arraySize); for (int i = 0; i < arraySize; i++) { outputBuffer.putFloat(theObject[i]); } } /** * Method serializeInt * * @param outputBuffer * @param theObject */ public static void serializeInt(GenericByteBuffer outputBuffer, int[] theObject) { int arraySize = (theObject == null) ? NULL_VALUE : theObject.length; outputBuffer.putInt(arraySize); for (int i = 0; i < arraySize; i++) { outputBuffer.putInt(theObject[i]); } } /** * Method serializeLong * * @param outputBuffer * @param theObject */ public static void serializeLong(GenericByteBuffer outputBuffer, long[] theObject) { int arraySize = (theObject == null) ? NULL_VALUE : theObject.length; outputBuffer.putInt(arraySize); for (int i = 0; i < arraySize; i++) { outputBuffer.putLong(theObject[i]); } } /** * Method serializeShort * * @param outputBuffer * @param theObject */ public static void serializeShort(GenericByteBuffer outputBuffer, short[] theObject) { int arraySize = (theObject == null) ? NULL_VALUE : theObject.length; outputBuffer.putInt(arraySize); for (int i = 0; i < arraySize; i++) { outputBuffer.putShort(theObject[i]); } } /** * Method deSerializePayload * * @param inputBuffer * @return */ public static Payload[] deSerializePayload(GenericByteBuffer inputBuffer) { int arraySize = inputBuffer.getInt(); if (arraySize == NULL_VALUE) { return null; } Payload theObject[] = new Payload[arraySize]; for (int i = 0; i < arraySize; i++) { theObject[i] = SerializationManager.deSerialize(inputBuffer); } return theObject; } /** * Method deSerializeHomogenousPayload * * @param inputBuffer * @return */ public static Payload[] deSerializeHomogenousPayload( GenericByteBuffer inputBuffer) { long serialUID = inputBuffer.getLong(); int arraySize = inputBuffer.getInt(); if (arraySize == NULL_VALUE) { return null; } Payload theObject[] = new Payload[arraySize]; for (int i = 0; i < arraySize; i++) { theObject[i] = SerializationManager.deSerialize(inputBuffer, serialUID); } return theObject; } /** * Method deSerializeExtendedObject * * @param inputBuffer * @return */ public static Object[] deSerializeExtendedObject( GenericByteBuffer inputBuffer) { int arraySize = inputBuffer.getInt(); if (arraySize == NULL_VALUE) { return null; } Object theObject[] = new Object[arraySize]; for (int i = 0; i < arraySize; i++) { theObject[i] = SerializationManager.deSerializeExtended(inputBuffer); } return theObject; } /** * Method deSerializeHomogenousExtendedObject * * @param inputBuffer * @return */ public static Object[] deSerializeHomogenousExtendedObject( GenericByteBuffer inputBuffer) { return deSerializeExtendedObject(inputBuffer); } /** * Method deSerializeBoolean * * @param inputBuffer * @return */ public static boolean[] deSerializeBoolean(GenericByteBuffer inputBuffer) { int arraySize = inputBuffer.getInt(); if (arraySize == NULL_VALUE) { return null; } boolean theObject[] = new boolean[arraySize]; for (int i = 0; i < arraySize; i++) { if (inputBuffer.get() == 1) { theObject[i] = true; } else { theObject[i] = false; } } return theObject; } /** * Method deSerializeByte * * @param inputBuffer * @return */ public static byte[] deSerializeByte(GenericByteBuffer inputBuffer) { int arraySize = inputBuffer.getInt(); if (arraySize == NULL_VALUE) { return null; } byte theObject[] = new byte[arraySize]; inputBuffer.get(theObject, 0, arraySize); return theObject; } /** * Method deSerializeChar * * @param inputBuffer * @return */ public static char[] deSerializeChar(GenericByteBuffer inputBuffer) { int arraySize = inputBuffer.getInt(); if (arraySize == NULL_VALUE) { return null; } char theObject[] = new char[arraySize]; for (int i = 0; i < arraySize; i++) { theObject[i] = inputBuffer.getChar(); } return theObject; } /** * Method deSerializeDouble * * @param inputBuffer * @return */ public static double[] deSerializeDouble(GenericByteBuffer inputBuffer) { int arraySize = inputBuffer.getInt(); if (arraySize == NULL_VALUE) { return null; } double theObject[] = new double[arraySize]; for (int i = 0; i < arraySize; i++) { theObject[i] = inputBuffer.getDouble(); } return theObject; } /** * Method deSerializeFloat * * @param inputBuffer * @return */ public static float[] deSerializeFloat(GenericByteBuffer inputBuffer) { int arraySize = inputBuffer.getInt(); if (arraySize == NULL_VALUE) { return null; } float theObject[] = new float[arraySize]; for (int i = 0; i < arraySize; i++) { theObject[i] = inputBuffer.getFloat(); } return theObject; } /** * Method deSerializeInt * * @param inputBuffer * @return */ public static int[] deSerializeInt(GenericByteBuffer inputBuffer) { int arraySize = inputBuffer.getInt(); if (arraySize == NULL_VALUE) { return null; } int theObject[] = new int[arraySize]; for (int i = 0; i < arraySize; i++) { theObject[i] = inputBuffer.getInt(); } return theObject; } /** * Method deSerializeLong * * @param inputBuffer * @return */ public static long[] deSerializeLong(GenericByteBuffer inputBuffer) { int arraySize = inputBuffer.getInt(); if (arraySize == NULL_VALUE) { return null; } long theObject[] = new long[arraySize]; for (int i = 0; i < arraySize; i++) { theObject[i] = inputBuffer.getLong(); } return theObject; } /** * Method deSerializeShort * * @param inputBuffer * @return */ public static short[] deSerializeShort(GenericByteBuffer inputBuffer) { int arraySize = inputBuffer.getInt(); if (arraySize == NULL_VALUE) { return null; } short theObject[] = new short[arraySize]; for (int i = 0; i < arraySize; i++) { theObject[i] = inputBuffer.getShort(); } return theObject; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -