/*
This file is part of the software framework JStatCom,
a library for building applications for data based analysis.
Copyright (C) 2007 Markus Kraetzig
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA
*/
package com.jstatcom.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;
/**
* Helper methods for (de)serialization and de/encoding to strings.
*
* @author Markus Kraetzig
*/
public final class SerializationHelper {
/**
* Serializes o to a byte array representation.
*
* @param o
* object to serialize
* @return byte array with serialized o
* @throws IllegalArgumentException
* if (!(o instanceof Serializable))
*/
public static byte[] getByteFromObject(Object o) {
if (!(o instanceof Serializable))
throw new IllegalArgumentException("Argument was not Serializable.");
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
try {
ObjectOutputStream out = new ObjectOutputStream(byteOut);
out.writeObject(o);
out.close();
return byteOut.toByteArray();
} catch (IOException e) {
throw new RuntimeException("Error setting Object " + o
+ " to byte[].", e);
}
}
/**
* Deserializes an object from the byte array representation of the
* serialized version.
*
* @param byteArray
* byte array representation of serialized object
* @return deserialized object
*/
public static T getObjectFromByteArray(byte[] byteArray) {
ByteArrayInputStream byteIn = new ByteArrayInputStream(byteArray);
try {
ObjectInputStream in = new ObjectInputStream(byteIn);
T rtn = (T) in.readObject();
in.close();
return rtn;
} catch (Exception e) {
throw new RuntimeException("Error retrieving Object from byte[].",
e);
}
}
/**
* Base64 encoding of serializable objects.
*
* @param o
* serializable object, may be null
* @return Base64 eoncoded string
* @throws IllegalArgumentException
* if (o != null && !(o instanceof Serializable))
*/
public static String encode(Object o) {
if (o == null)
return null;
if (!(o instanceof Serializable))
throw new IllegalArgumentException("Argument was not Serializable.");
String encGZDontBreakLines = Base64.encodeObject((Serializable) o,
Base64.GZIP | Base64.DONT_BREAK_LINES);
return encGZDontBreakLines;
}
/**
* Decode Base64 encoded string (with encode).
*
* @param encodedString
* may be null
* @return instance of T or null if argument
* was null
*/
public static T decode(String encodedString) {
if (encodedString == null)
return null;
return (T) Base64.decodeToObject(encodedString);
}
}
Ads help maintain this website.