mirror of
https://gitee.com/dromara/MaxKey.git
synced 2025-12-07 01:18:27 +08:00
optimize ObjectTransformer
This commit is contained in:
parent
09b7ddf797
commit
f4ac3a9af2
@ -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;
|
||||
@ -41,80 +36,13 @@ 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
|
||||
* @param Serializable Object
|
||||
* @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
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user