1
2
public class SerializeUtil {
private final static String CHARSET = "ISO-8859-1";
private final static String ENCODE = "UTF-8";
public static String serialize(final Object obj) throws IOException {
String result = StringUtil.EMPTY;
final ByteArrayOutputStream stream = new ByteArrayOutputStream();
final ObjectOutputStream out = new ObjectOutputStream(stream);
out.writeObject(obj);
result = stream.toString(CHARSET);
result = URLEncoder.encode(result, ENCODE);
out.flush();
out.close();
stream.close();
return result;
}
public static Object unserialize(final String str) throws IOException, ClassNotFoundException {
final String objectString = URLDecoder.decode(str, ENCODE);
final ByteArrayInputStream stream = new ByteArrayInputStream(objectString.getBytes(CHARSET));
final ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(stream));
final Object obj = in.readObject();
in.close();
return obj;
}
}
3