... Serializable?
, . Serializable java.io.Serializable. , ObjectInputStream/ObjectOutputStream .
, :
public static byte[] toByteArray(Object object) throws IOException {
if (!isSerializable(object)) {
throw new IOException("Object '" + object.getClass().getName() + "' is not serializable.");
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(baos);
oos.writeObject(object);
oos.flush();
} finally {
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
logger.error("Closing of ObjectOutputStream failed.", e);
}
}
}
return baos.toByteArray();
}
public static Object toObject(byte[] bytes) throws IOException, ClassNotFoundException {
Object object = null;
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new ByteArrayInputStream(bytes));
object = ois.readObject();
} finally {
if (ois != null) {
try {
ois.close();
} catch (IOException e) {
logger.error("Closing of ObjectInputStream failed.", e);
}
}
}
return object;
}