package com.basic.security.utils;
|
|
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayOutputStream;
|
import java.io.IOException;
|
import java.io.ObjectInput;
|
import java.io.ObjectInputStream;
|
import java.io.ObjectOutput;
|
import java.io.ObjectOutputStream;
|
|
public class ObjectUtil {
|
public static boolean stringEquals(String str1, String str2) {
|
if (str1 == null) {
|
if (str2 == null) {
|
return true;
|
} else {
|
return false;
|
}
|
} else {
|
return str1.equals(str2);
|
}
|
}
|
|
public static byte[] toByteArray(Object yourObject) {
|
try {
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
ObjectOutput out = null;
|
try {
|
out = new ObjectOutputStream(bos);
|
out.writeObject(yourObject);
|
out.flush();
|
byte[] yourBytes = bos.toByteArray();
|
return yourBytes;
|
} finally {
|
try {
|
bos.close();
|
} catch (IOException ex) {
|
// ignore close exception
|
}
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return new byte[]{};
|
}
|
|
public static Object toObject(byte[] yourBytes) {
|
try {
|
ByteArrayInputStream bis = new ByteArrayInputStream(yourBytes);
|
ObjectInput in = null;
|
try {
|
in = new ObjectInputStream(bis);
|
Object o = in.readObject();
|
return o;
|
} finally {
|
try {
|
if (in != null) {
|
in.close();
|
}
|
} catch (IOException ex) {
|
// ignore close exception
|
}
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
}
|