提交 f4ac3a9a 编写于 作者: M MaxKey

optimize ObjectTransformer

上级 09b7ddf7
......@@ -20,11 +20,6 @@
*/
package org.maxkey.util;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import org.maxkey.crypto.HexUtils;
......@@ -40,73 +35,6 @@ import org.maxkey.crypto.HexUtils;
*/
public class ObjectTransformer {
/**
*
*/
public ObjectTransformer() {
}
/**
* Object 2 ByteArray
* @param state is Object
* @return ByteArray
*/
public static byte[] object2Bytes(Object state) {
ObjectOutputStream oos = null;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
oos = new ObjectOutputStream(baos);
oos.writeObject(state);
oos.flush();
return baos.toByteArray();
}
catch (IOException e) {
throw new IllegalArgumentException(e);
}
finally {
if (oos != null) {
try {
oos.close();
}
catch (IOException e) {
// eat it
}
}
}
}
/**
* ByteArray 2 Object
* @param byteArray
* @return Object
*/
public static <T> T bytes2Object(byte[] byteArray) {
ObjectInputStream oip = null;
try {
oip = new ObjectInputStream(new ByteArrayInputStream(byteArray));
@SuppressWarnings("unchecked")
T result = (T) oip.readObject();
return result;
}
catch (IOException e) {
throw new IllegalArgumentException(e);
}
catch (ClassNotFoundException e) {
throw new IllegalArgumentException(e);
}
finally {
if (oip != null) {
try {
oip.close();
}
catch (IOException e) {
// eat it
}
}
}
}
/**
* serialize Serializable Object 2 HEX String
......@@ -114,7 +42,7 @@ public class ObjectTransformer {
* @return String
*/
public static final String serialize(Serializable s){
return HexUtils.hex2String(object2Bytes(s));
return HexUtils.hex2String(SerializationUtils.serialize(s));
}
/**
......@@ -123,7 +51,7 @@ public class ObjectTransformer {
* @return Object
*/
public static final <T> T deserialize(String hex) {
return bytes2Object(HexUtils.hex2Bytes(hex));
return SerializationUtils.deserialize(HexUtils.hex2Bytes(hex));
}
......
......@@ -39,7 +39,7 @@ public class SerializationUtils {
oos.close();
}
catch (IOException e) {
// eat it
}
}
}
......@@ -65,7 +65,7 @@ public class SerializationUtils {
oip.close();
}
catch (IOException e) {
// eat it
}
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册