01: /*
02: * Lucane - a collaborative platform
03: * Copyright (C) 2004 Gilles Viguie <gilles.viguie@free.fr>
04: *
05: * This library is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU Lesser General Public
07: * License as published by the Free Software Foundation; either
08: * version 2.1 of the License, or (at your option) any later version.
09: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library; if not, write to the Free Software
17: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18: */
19: package org.lucane.common.net;
20:
21: import java.io.IOException;
22: import java.io.Serializable;
23: import java.io.ByteArrayInputStream;
24: import java.io.ObjectInputStream;
25: import java.io.ByteArrayOutputStream;
26: import java.io.ObjectOutputStream;
27: import java.util.zip.GZIPInputStream;
28: import java.util.zip.GZIPOutputStream;
29:
30: /**
31: * Convert objets to GZipped bytes
32: */
33: public class ObjectZipper {
34: /**
35: * Zip object to bytes
36: * @param object the object to zip
37: * @return the byte array
38: */
39: public static byte[] objectToBytes(Serializable object)
40: throws IOException {
41: ByteArrayOutputStream bo = new ByteArrayOutputStream();
42: GZIPOutputStream go = new GZIPOutputStream(bo);
43: ObjectOutputStream oo = new ObjectOutputStream(go);
44:
45: oo.writeObject(object);
46: oo.flush();
47: go.finish();
48:
49: return bo.toByteArray();
50: }
51:
52: /**
53: * Unzip bytes to object
54: * @param bytes the zipped bytes
55: * @return the object
56: */
57: public static Serializable bytesToObject(byte[] bytes)
58: throws IOException, ClassNotFoundException {
59: ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
60: GZIPInputStream gi = new GZIPInputStream(bi);
61: ObjectInputStream oi = new LucaneObjectInputStream(gi);
62:
63: return (Serializable) oi.readObject();
64: }
65: }
|