首页

通过JavaSerializationUtils实现对象及字符串序列化和反序列化过程工具类

标签:工具类,字符串序列化,ByteArrayOutputStream,serialize,对象反序列化,deserialize,apache,ClassLoaderObjectInputStream,common-io     发布时间:2017-10-25   

一、前言

1.JavaSerializationUtils类基于java.io.ObjectOutputStream实现对象的序列化,再通过apache的ClassLoaderObjectInputStream(commons-io-1.4.jar)对象流类加载器实现对象化,代码如下

import java.io.ByteArrayInputStream;@b@import java.io.ByteArrayOutputStream;@b@import java.io.IOException;@b@import java.io.ObjectInputStream;@b@import java.io.ObjectOutputStream;@b@@b@import org.apache.commons.io.input.ClassLoaderObjectInputStream;@b@@b@public class JavaSerializationUtils {@b@	@b@	public static byte[] serialize(Object object) {@b@		if (object == null) {@b@			return null;@b@		}@b@		ByteArrayOutputStream baos = new ByteArrayOutputStream();@b@		try {@b@			ObjectOutputStream oos = new ObjectOutputStream(baos);@b@			oos.writeObject(object);@b@			oos.flush();@b@		}@b@		catch (Exception ex) {@b@			throw new IllegalArgumentException("Failed to serialize object of type: " + object.getClass(), ex);@b@		}@b@		return baos.toByteArray();@b@	}@b@	@b@	public static Object deserialize(byte[] bytes,ClassLoader classLoader) {@b@		if (bytes == null) {@b@			return null;@b@		}@b@		try {@b@			ObjectInputStream ois = new ClassLoaderObjectInputStream(classLoader,new ByteArrayInputStream(bytes));@b@			try{@b@				return ois.readObject();@b@			}finally{@b@				ois.close();@b@			}@b@		}@b@		catch (IOException ex) {@b@			throw new IllegalArgumentException("Failed to deserialize object  by classloader="+classLoader, ex);@b@		}@b@		catch (ClassNotFoundException ex) {@b@			throw new IllegalStateException("Failed to deserialize object type by classloader="+classLoader, ex);@b@		}@b@	}@b@@b@	@b@	public static Object deserialize(byte[] bytes) {@b@		return deserialize(bytes,getDefaultClassLoader());@b@	}@b@	@b@	public static ClassLoader getDefaultClassLoader() {@b@		ClassLoader cl = null;@b@		try {@b@			cl = Thread.currentThread().getContextClassLoader();@b@		}@b@		catch (Throwable ex) {@b@		}@b@		if (cl == null) {@b@			cl = JavaSerializationUtils.class.getClassLoader();@b@		}@b@		return cl;@b@	}@b@	@b@}

2.JavaSerialization简单转换使用

public class JavaSerialization implements Serialization{@b@@b@	@Override@b@	public Object deserialize(Class<?> clazz, byte[] bytes) {@b@		return JavaSerializationUtils.deserialize(bytes);@b@	}@b@@b@	@Override@b@	public byte[] serialize(Object value) {@b@		return JavaSerializationUtils.serialize(value);@b@	} @b@	@b@}

3.StringSerialization字符串序列化

import java.nio.charset.Charset;@b@@b@public class StringSerialization implements Serialization{@b@	@b@	@b@	private Charset charset;@b@	@b@	public StringSerialization(){@b@		this.charset=Charset.forName("UTF-8");@b@	}@b@	@b@	public StringSerialization(Charset charset){@b@		this.charset=charset;@b@	}@b@@b@	@Override@b@	public Object deserialize(Class<?> clazz,byte[] datas)  {@b@		return new String(datas,charset);@b@	}@b@@b@	@Override@b@	public byte[] serialize(Object value) {@b@		return value.toString().getBytes(charset);@b@	}@b@	@b@}