All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.darkmist.alib.io.Serializer Maven / Gradle / Ivy

/*
 *  Copyright (C) 2012 Ed Schaller 
 *
 *  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 net.darkmist.alib.io;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

// should look at org.apache.commons.lang.SerializationUtils
public abstract class Serializer
{
	private static final Class CLASS = Serializer.class;
	@SuppressWarnings("unused")
	private static final Logger logger = LoggerFactory.getLogger(CLASS);

	/** Read one serialized object from a input stream.
	 * @param in InputStream to read from. This will be closed!
	 * @param type The type that is to be read.
	 * @return The serialized object cast as type.
	 */
	public static  T deserializeFromStream(InputStream in, Class type) throws ClassNotFoundException, IOException
	{
		ObjectInputStream objIn;
		T obj;

		if(in instanceof ObjectInputStream)
			objIn = (ObjectInputStream)in;
		else
			objIn = new ObjectInputStream(in);

		obj = type.cast(objIn.readObject());
		// to close or not to close... That is the question...
		// we close... It doesn't make too much sense to have a stream with more afterwards...There would be two
		// stream headers...
		objIn.close();
		return obj;
	}

	public static  T deserializeFromFile(File file, Class type) throws ClassNotFoundException, IOException
	{
		return deserializeFromStream(new FileInputStream(file), type);
	}

	public static  T deserializeFromBytes(byte[] bytes, Class type) throws ClassNotFoundException, IOException
	{
		return deserializeFromStream(new ByteArrayInputStream(bytes), type);
	}

	/** Write one object serilized to a output stream.
	 * @param obj The Object to serilize.
	 * @param out The output stream to write it to. This will be closed!
	 */
	public static  void serializeToStream(T obj, OutputStream out) throws IOException
	{
		ObjectOutputStream objOut;

		if(out instanceof ObjectOutputStream)
			objOut = (ObjectOutputStream)out;
		else
			objOut = new ObjectOutputStream(out);
		objOut.writeObject(obj);
		objOut.close();
	}

	public static  void serializeToFile(T obj, File file) throws IOException
	{
		serializeToStream(obj, new FileOutputStream(file));
	}

	/** Serializes an object to a temporary file.
	 * @param obj The object to serialize.
	 * @return The file the object is serialized in. This file will already be set to be deleted on process exit.
	 */
        public static  File serializeToTempFile(T obj) throws IOException
        {
                File file = File.createTempFile(Serializer.class.getName().replace(".*\\.",""), null);
                file.deleteOnExit();
                serializeToFile(obj,file);
		return file;
        }

	public static  byte[] serializeToBytes(T obj) throws IOException
	{
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		serializeToStream(obj, out);
		return out.toByteArray();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy