comparison artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultArtifactSerializer.java @ 41:5e4bc24ea438

Made serilization more flexible. DB update required!!! Fixed problem with touching artifacts in database. artifacts/trunk@119 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 23 Sep 2009 16:55:12 +0000
parents
children 48d1a9a082c2
comparison
equal deleted inserted replaced
40:af22d4de275c 41:5e4bc24ea438
1 package de.intevation.artifactdatabase;
2
3 import de.intevation.artifacts.Artifact;
4 import de.intevation.artifacts.ArtifactSerializer;
5
6 import java.io.InputStream;
7 import java.io.IOException;
8 import java.io.OutputStream;
9 import java.io.ObjectInputStream;
10 import java.io.ObjectOutputStream;
11 import java.io.ByteArrayOutputStream;
12 import java.io.ByteArrayInputStream;
13
14 import java.util.zip.GZIPOutputStream;
15 import java.util.zip.GZIPInputStream;
16
17 import org.apache.log4j.Logger;
18
19 /**
20 * @author Sascha L. Teichmann (sascha.teichmann@intevation.de)
21 */
22 public class DefaultArtifactSerializer
23 implements ArtifactSerializer
24 {
25 private static Logger logger =
26 Logger.getLogger(DefaultArtifactSerializer.class);
27
28 public static final ArtifactSerializer INSTANCE =
29 new DefaultArtifactSerializer();
30
31 public DefaultArtifactSerializer() {
32 }
33
34 public Artifact fromBytes(byte [] bytes) {
35
36 if (bytes == null) {
37 return null;
38 }
39
40 ObjectInputStream ois = null;
41
42 try {
43 ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
44 GZIPInputStream gis = new GZIPInputStream(bis);
45 ois = getObjectInputStream(gis);
46
47 return (Artifact)ois.readObject();
48 }
49 catch (IOException ioe) {
50 logger.error(ioe.getLocalizedMessage(), ioe);
51 }
52 catch (ClassNotFoundException cnfe) {
53 logger.error(cnfe.getLocalizedMessage(), cnfe);
54 }
55 catch (ClassCastException cce) {
56 logger.error(cce.getLocalizedMessage(), cce);
57 }
58 finally {
59 if (ois != null) {
60 try { ois.close(); }
61 catch (IOException ioe) { }
62 }
63 }
64
65 return null;
66 }
67
68 public byte [] toBytes(Artifact artifact) {
69 try {
70 ByteArrayOutputStream bos = new ByteArrayOutputStream();
71 GZIPOutputStream gos = new GZIPOutputStream(bos);
72 ObjectOutputStream oos = getObjectOutputStream(gos);
73
74 oos.writeObject(artifact);
75 oos.flush();
76 oos.close();
77
78 return bos.toByteArray();
79 }
80 catch (IOException ioe) {
81 logger.error(ioe.getLocalizedMessage(), ioe);
82 throw new RuntimeException(ioe);
83 }
84 }
85
86 protected ObjectInputStream getObjectInputStream(InputStream is)
87 throws IOException
88 {
89 return new ObjectInputStream(is);
90 }
91
92 protected ObjectOutputStream getObjectOutputStream(OutputStream os)
93 throws IOException
94 {
95 return new ObjectOutputStream(os);
96 }
97 }
98 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:

http://dive4elements.wald.intevation.org