首页

分享实现常见数据类型对象序列化和反序列化工具类SimpleSerializeUtil针对不同类型差异化序列化示例代码

标签:对象序列化,对象反序列化,ByteArrayInputStream,SimpleSerializeUtil,序列化工具类     发布时间:2018-11-18   

一、前言

定义简单SimpleSerializeUtil序列化工具类,用于针对不同数据类型(Boolean/Byte/Character/Double/Float/Integer/Long/Short/String等)对象选用不同方式进行序列化,详情示例说明。

二、示例说明

1.  SimpleSerializeUtil类

import java.io.ByteArrayInputStream;@b@import java.io.ByteArrayOutputStream;@b@import java.io.DataInputStream;@b@import java.io.DataOutputStream;@b@import java.io.IOException;@b@import java.io.InputStream;@b@import java.io.ObjectInputStream;@b@import java.io.ObjectOutputStream;@b@import java.io.OutputStream;@b@import java.util.HashMap;@b@import java.util.Map;@b@@b@public class SimpleSerializeUtil@b@{@b@  private static Map PRIMITIVE_TYPES = new HashMap(10);@b@  private static final byte TYPE_NULL = 0;@b@  private static final byte TYPE_SERIALIZABLE = 2;@b@  private static final byte TYPE_BOOLEAN = 10;@b@  private static final byte TYPE_BYTE = 11;@b@  private static final byte TYPE_CHAR = 12;@b@  private static final byte TYPE_DOUBLE = 13;@b@  private static final byte TYPE_FLOAT = 14;@b@  private static final byte TYPE_INT = 15;@b@  private static final byte TYPE_LONG = 16;@b@  private static final byte TYPE_SHORT = 17;@b@  private static final byte TYPE_STRING = 18;@b@@b@  public static Object objectFromByteBuffer(byte[] buffer)@b@    throws Exception@b@  {@b@    int offset = 0;@b@@b@    if (buffer == null) return null;@b@    int length = buffer.length;@b@@b@    Object retval = null;@b@    InputStream in = null;@b@    ByteArrayInputStream in_stream = new ByteArrayInputStream(buffer, offset, length);@b@    byte b = (byte)in_stream.read();@b@    try@b@    {@b@      Object ois;@b@      switch (b)@b@      {@b@      case 0:@b@        Object localObject1 = null; jsr 388; return localObject1;@b@      case 2:@b@        in = new ContextObjectInputStream(in_stream);@b@        retval = ((ContextObjectInputStream)in).readObject();@b@        break;@b@      case 10:@b@        in = new DataInputStream(in_stream);@b@        retval = Boolean.valueOf(((DataInputStream)in).readBoolean());@b@        break;@b@      case 11:@b@        in = new DataInputStream(in_stream);@b@        retval = new Byte(((DataInputStream)in).readByte());@b@        break;@b@      case 12:@b@        in = new DataInputStream(in_stream);@b@        retval = new Character(((DataInputStream)in).readChar());@b@        break;@b@      case 13:@b@        in = new DataInputStream(in_stream);@b@        retval = new Double(((DataInputStream)in).readDouble());@b@        break;@b@      case 14:@b@        in = new DataInputStream(in_stream);@b@        retval = new Float(((DataInputStream)in).readFloat());@b@        break;@b@      case 15:@b@        in = new DataInputStream(in_stream);@b@        retval = new Integer(((DataInputStream)in).readInt());@b@        break;@b@      case 16:@b@        in = new DataInputStream(in_stream);@b@        retval = new Long(((DataInputStream)in).readLong());@b@        break;@b@      case 17:@b@        in = new DataInputStream(in_stream);@b@        retval = new Short(((DataInputStream)in).readShort());@b@        break;@b@      case 18:@b@        in = new DataInputStream(in_stream);@b@        if (((DataInputStream)in).readBoolean()) {@b@          ois = new ObjectInputStream(in);@b@          try {@b@            Object localObject2 = ((ObjectInputStream)ois).readObject();@b@@b@            ((ObjectInputStream)ois).close(); return localObject2; } finally { ((ObjectInputStream)ois).close();@b@          }@b@        }@b@@b@        retval = ((DataInputStream)in).readUTF();@b@@b@        break;@b@      case 1:@b@      case 3:@b@      case 4:@b@      case 5:@b@      case 6:@b@      case 7:@b@      case 8:@b@      case 9:@b@      default:@b@        throw new IllegalArgumentException("type " + b + " is invalid");@b@      }@b@      return retval;@b@    }@b@    finally {@b@      if (in != null)@b@        try {@b@          in.close();@b@        }@b@        catch (IOException e)@b@        {@b@        }@b@    }@b@  }@b@@b@  public static byte[] objectToByteBuffer(Object obj)@b@    throws Exception@b@  {@b@    Byte type;@b@    byte[] result = null;@b@    ByteArrayOutputStream out_stream = new ByteArrayOutputStream(512);@b@@b@    if (obj == null) {@b@      out_stream.write(0);@b@      out_stream.flush();@b@      return out_stream.toByteArray();@b@    }@b@@b@    OutputStream out = null;@b@    try@b@    {@b@      if ((type = (Byte)PRIMITIVE_TYPES.get(obj.getClass())) != null) {@b@        out_stream.write(type.byteValue());@b@        out = new DataOutputStream(out_stream); }@b@      switch (type.byteValue())@b@      {@b@      case 10:@b@        ((DataOutputStream)out).writeBoolean(((Boolean)obj).booleanValue());@b@        break;@b@      case 11:@b@        ((DataOutputStream)out).writeByte(((Byte)obj).byteValue());@b@        break;@b@      case 12:@b@        ((DataOutputStream)out).writeChar(((Character)obj).charValue());@b@        break;@b@      case 13:@b@        ((DataOutputStream)out).writeDouble(((Double)obj).doubleValue());@b@        break;@b@      case 14:@b@        ((DataOutputStream)out).writeFloat(((Float)obj).floatValue());@b@        break;@b@      case 15:@b@        ((DataOutputStream)out).writeInt(((Integer)obj).intValue());@b@        break;@b@      case 16:@b@        ((DataOutputStream)out).writeLong(((Long)obj).longValue());@b@        break;@b@      case 17:@b@        ((DataOutputStream)out).writeShort(((Short)obj).shortValue());@b@        break;@b@      case 18:@b@        String str = (String)obj;@b@        if (str.length() > 32767) {@b@          ((DataOutputStream)out).writeBoolean(true);@b@          ObjectOutputStream oos = new ObjectOutputStream(out);@b@          try {@b@            oos.writeObject(str);@b@          }@b@          finally {@b@            oos.close();@b@          }@b@        }@b@        else {@b@          ((DataOutputStream)out).writeBoolean(false);@b@          ((DataOutputStream)out).writeUTF(str);@b@        }@b@        break;@b@      default:@b@        throw new IllegalArgumentException("type " + type + " is invalid");@b@@b@        out_stream.write(2);@b@        out = new ObjectOutputStream(out_stream);@b@        ((ObjectOutputStream)out).writeObject(obj);@b@      }@b@    }@b@    finally {@b@      if (out != null)@b@        try {@b@          out.close();@b@        } catch (IOException e) {@b@        }@b@    }@b@    result = out_stream.toByteArray();@b@    return result;@b@  }@b@@b@  static@b@  {@b@    PRIMITIVE_TYPES.put(Boolean.class, new Byte(10));@b@    PRIMITIVE_TYPES.put(Byte.class, new Byte(11));@b@    PRIMITIVE_TYPES.put(Character.class, new Byte(12));@b@    PRIMITIVE_TYPES.put(Double.class, new Byte(13));@b@    PRIMITIVE_TYPES.put(Float.class, new Byte(14));@b@    PRIMITIVE_TYPES.put(Integer.class, new Byte(15));@b@    PRIMITIVE_TYPES.put(Long.class, new Byte(16));@b@    PRIMITIVE_TYPES.put(Short.class, new Byte(17));@b@    PRIMITIVE_TYPES.put(String.class, new Byte(18));@b@  }@b@}

2. ContextObjectInputStream类

import java.io.IOException;@b@import java.io.InputStream;@b@import java.io.ObjectInputStream;@b@import java.io.ObjectStreamClass;@b@import java.util.HashMap;@b@@b@public class ContextObjectInputStream extends ObjectInputStream@b@{@b@  private static final HashMap classCache = new HashMap();@b@  private static final HashMap primClasses = new HashMap(9, 1.0F);@b@@b@  public ContextObjectInputStream(InputStream is)@b@    throws IOException@b@  {@b@    super(is);@b@  }@b@@b@  protected Class resolveClass(ObjectStreamClass v) throws IOException, ClassNotFoundException@b@  {@b@    String className = v.getName();@b@    Class resolvedClass = null;@b@@b@    synchronized (classCache) {@b@      resolvedClass = (Class)classCache.get(className);@b@    }@b@@b@    if (resolvedClass == null) {@b@      try {@b@        resolvedClass = loadClass(className, getClass());@b@      }@b@      catch (ClassNotFoundException )@b@      {@b@      }@b@      if (resolvedClass == null)@b@      {@b@        resolvedClass = (Class)primClasses.get(className);@b@        if (resolvedClass == null)@b@        {@b@          resolvedClass = super.resolveClass(v);@b@        }@b@      }@b@      synchronized (classCache) {@b@        classCache.put(className, resolvedClass);@b@      }@b@    }@b@    return ((Class)resolvedClass);@b@  }@b@@b@  public static Class loadClass(String classname, Class clazz) throws ClassNotFoundException@b@  {@b@    ClassLoader loader;@b@    try {@b@      loader = Thread.currentThread().getContextClassLoader();@b@      if (loader != null)@b@        return loader.loadClass(classname);@b@    }@b@    catch (Throwable t)@b@    {@b@    }@b@@b@    if (clazz != null)@b@      try {@b@        loader = clazz.getClassLoader();@b@        if (loader != null)@b@          return loader.loadClass(classname);@b@      }@b@      catch (Throwable t)@b@      {@b@      }@b@@b@    try@b@    {@b@      loader = ClassLoader.getSystemClassLoader();@b@      if (loader != null)@b@        return loader.loadClass(classname);@b@    }@b@    catch (Throwable t)@b@    {@b@    }@b@@b@    throw new ClassNotFoundException(classname);@b@  }@b@@b@  static@b@  {@b@    primClasses.put("boolean", Boolean.TYPE);@b@    primClasses.put("byte", Byte.TYPE);@b@    primClasses.put("char", Character.TYPE);@b@    primClasses.put("short", Short.TYPE);@b@    primClasses.put("int", Integer.TYPE);@b@    primClasses.put("long", Long.TYPE);@b@    primClasses.put("float", Float.TYPE);@b@    primClasses.put("double", Double.TYPE);@b@    primClasses.put("void", Void.TYPE);@b@  }@b@}