comparison artifact-database/src/main/java/org/dive4elements/artifactdatabase/DefaultArtifactSerializer.java @ 473:d0ac790a6c89 dive4elements-move

Moved directories to org.dive4elements
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 10:57:18 +0200
parents artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultArtifactSerializer.java@933bbc9fc11f
children 415df0fc4fa1
comparison
equal deleted inserted replaced
472:783cc1b6b615 473:d0ac790a6c89
1 /*
2 * Copyright (c) 2010 by Intevation GmbH
3 *
4 * This program is free software under the LGPL (>=v2.1)
5 * Read the file LGPL.txt coming with the software for details
6 * or visit http://www.gnu.org/licenses/ if it does not exist.
7 */
8
9 package de.intevation.artifactdatabase;
10
11 import de.intevation.artifacts.Artifact;
12 import de.intevation.artifacts.ArtifactSerializer;
13
14 import java.io.ByteArrayInputStream;
15 import java.io.ByteArrayOutputStream;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.io.ObjectInputStream;
19 import java.io.ObjectOutputStream;
20 import java.io.OutputStream;
21
22 import java.util.zip.GZIPInputStream;
23 import java.util.zip.GZIPOutputStream;
24
25 import org.apache.log4j.Logger;
26
27 /**
28 * Default implementation of the ArtifactSerializer interface.
29 * It uses serialized Java objects which are gzipped and
30 * turned into bytes.
31 *
32 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
33 */
34 public class DefaultArtifactSerializer
35 implements ArtifactSerializer
36 {
37 private static Logger logger =
38 Logger.getLogger(DefaultArtifactSerializer.class);
39
40 /**
41 * Static instance to avoid repeated creation of Serializers.
42 */
43 public static final ArtifactSerializer INSTANCE =
44 new DefaultArtifactSerializer();
45
46 /**
47 * Default constructor.
48 */
49 public DefaultArtifactSerializer() {
50 }
51
52 public Artifact fromBytes(byte [] bytes) {
53
54 if (bytes == null) {
55 return null;
56 }
57
58 ObjectInputStream ois = null;
59
60 try {
61 ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
62 GZIPInputStream gis = new GZIPInputStream(bis);
63 ois = getObjectInputStream(gis);
64
65 return (Artifact)ois.readObject();
66 }
67 catch (IOException ioe) {
68 logger.error(ioe.getLocalizedMessage(), ioe);
69 }
70 catch (ClassNotFoundException cnfe) {
71 logger.error(cnfe.getLocalizedMessage(), cnfe);
72 }
73 catch (ClassCastException cce) {
74 logger.error(cce.getLocalizedMessage(), cce);
75 }
76 finally {
77 if (ois != null) {
78 try { ois.close(); }
79 catch (IOException ioe) { }
80 }
81 }
82
83 return null;
84 }
85
86 public byte [] toBytes(Artifact artifact) {
87 try {
88 ByteArrayOutputStream bos = new ByteArrayOutputStream();
89 GZIPOutputStream gos = new GZIPOutputStream(bos);
90 ObjectOutputStream oos = getObjectOutputStream(gos);
91
92 oos.writeObject(artifact);
93 oos.flush();
94 oos.close();
95
96 return bos.toByteArray();
97 }
98 catch (IOException ioe) {
99 logger.error(ioe.getLocalizedMessage(), ioe);
100 throw new RuntimeException(ioe);
101 }
102 }
103
104 /**
105 * Wraps an input stream into an object input stream. You may
106 * overwrite this to get a more specialized deserializer.
107 * @param is The raw input stream
108 * @return An instance of a subclass of ObjectInputStream.
109 * @throws IOException Thrown if something went wrong during
110 * creation of the object input stream.
111 */
112 protected ObjectInputStream getObjectInputStream(InputStream is)
113 throws IOException
114 {
115 return new ObjectInputStream(is);
116 }
117
118 /**
119 * Wraps an output stream into an object output stream. You may
120 * overwrite this to get a more specialized serializer.
121 * @param os the raw output stream.
122 * @return An instance of a subclass of ObjectOutputStream.
123 * @throws IOException Thrown if something went wrong during
124 * creation of the object output stream.
125 */
126 protected ObjectOutputStream getObjectOutputStream(OutputStream os)
127 throws IOException
128 {
129 return new ObjectOutputStream(os);
130 }
131 }
132 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org