1 45 package org.exolab.jms.net.util; 46 47 import java.io.IOException ; 48 import java.io.ObjectInput ; 49 import java.io.ObjectOutput ; 50 51 52 59 public final class SerializationHelper { 60 61 64 private SerializationHelper() { 65 } 66 67 75 public static void write(Class type, Object object, ObjectOutput out) 76 throws IOException { 77 78 if (type.isPrimitive()) { 79 if (type == boolean.class) { 80 out.writeBoolean(((Boolean ) object).booleanValue()); 81 } else if (type == byte.class) { 82 out.writeByte(((Byte ) object).byteValue()); 83 } else if (type == char.class) { 84 out.writeChar(((Character ) object).charValue()); 85 } else if (type == short.class) { 86 out.writeShort(((Short ) object).shortValue()); 87 } else if (type == int.class) { 88 out.writeInt(((Integer ) object).intValue()); 89 } else if (type == long.class) { 90 out.writeLong(((Long ) object).longValue()); 91 } else if (type == float.class) { 92 out.writeFloat(((Float ) object).floatValue()); 93 } else if (type == double.class) { 94 out.writeDouble(((Double ) object).doubleValue()); 95 } else { 96 throw new IOException ("Unsupported primitive type: " + type); 97 } 98 } else { 99 out.writeObject(object); 100 } 101 } 102 103 113 public static Object read(Class type, ObjectInput in) 114 throws ClassNotFoundException , IOException { 115 Object result; 116 if (type.isPrimitive()) { 117 if (type == boolean.class) { 118 result = new Boolean (in.readBoolean()); 119 } else if (type == byte.class) { 120 result = new Byte (in.readByte()); 121 } else if (type == char.class) { 122 result = new Character (in.readChar()); 123 } else if (type == short.class) { 124 result = new Short (in.readShort()); 125 } else if (type == int.class) { 126 result = new Integer (in.readInt()); 127 } else if (type == long.class) { 128 result = new Long (in.readLong()); 129 } else if (type == float.class) { 130 result = new Float (in.readFloat()); 131 } else if (type == double.class) { 132 result = new Double (in.readDouble()); 133 } else { 134 throw new IOException ("Unsupported primitive type: " + type); 135 } 136 } else { 137 result = in.readObject(); 138 } 139 return result; 140 } 141 142 } 143 | Popular Tags |