view artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultArtifactSerializer.java @ 77:48d1a9a082c2 0.5

Bring @author javadoc tags in form '@author <a href="john.doe@example.com">John Doe</a>' to make the sources to be able to be formatted with jalopy (http://jalopy.sourceforge.net). artifacts/trunk@695 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 21 Feb 2010 23:05:32 +0000
parents 5e4bc24ea438
children 0f48188a6e02
line wrap: on
line source
package de.intevation.artifactdatabase;

import de.intevation.artifacts.Artifact;
import de.intevation.artifacts.ArtifactSerializer;

import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;

import java.util.zip.GZIPOutputStream;
import java.util.zip.GZIPInputStream;

import org.apache.log4j.Logger;

/**
 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
 */
public class DefaultArtifactSerializer
implements   ArtifactSerializer
{
    private static Logger logger =
        Logger.getLogger(DefaultArtifactSerializer.class);

    public static final ArtifactSerializer INSTANCE =
        new DefaultArtifactSerializer();

    public DefaultArtifactSerializer() {
    }

    public Artifact fromBytes(byte [] bytes) {

        if (bytes == null) {
            return null;
        }

        ObjectInputStream ois = null;

        try {
            ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
            GZIPInputStream      gis = new GZIPInputStream(bis);
                                 ois = getObjectInputStream(gis);

            return (Artifact)ois.readObject();
        }
        catch (IOException ioe) {
            logger.error(ioe.getLocalizedMessage(), ioe);
        }
        catch (ClassNotFoundException cnfe) {
            logger.error(cnfe.getLocalizedMessage(), cnfe);
        }
        catch (ClassCastException cce) {
            logger.error(cce.getLocalizedMessage(), cce);
        }
        finally {
            if (ois != null) {
                try { ois.close(); }
                catch (IOException ioe) { }
            }
        }

        return null;
    }

    public byte [] toBytes(Artifact artifact) {
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            GZIPOutputStream      gos = new GZIPOutputStream(bos);
            ObjectOutputStream    oos = getObjectOutputStream(gos);

            oos.writeObject(artifact);
            oos.flush();
            oos.close();

            return bos.toByteArray();
        }
        catch (IOException ioe) {
            logger.error(ioe.getLocalizedMessage(), ioe);
            throw new RuntimeException(ioe);
        }
    }

    protected ObjectInputStream getObjectInputStream(InputStream is)
    throws    IOException
    {
        return new ObjectInputStream(is);
    }

    protected ObjectOutputStream getObjectOutputStream(OutputStream os)
    throws    IOException
    {
        return new ObjectOutputStream(os);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:

http://dive4elements.wald.intevation.org