1 | /*
|
---|
2 | * class: SerializationPacking -- Store an object as serialized data
|
---|
3 | *
|
---|
4 | * Copyright (C) 1997 Pharos IP Pty Ltd
|
---|
5 | * $Id: SerializationPacking.java 10737 2005-10-19 03:06:40Z kjdon $
|
---|
6 | *
|
---|
7 | * This program is free software; you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation; either version 2 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * This program is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with this program; if not, write to the Free Software
|
---|
19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
20 | */
|
---|
21 |
|
---|
22 | package au.com.pharos.packing;
|
---|
23 |
|
---|
24 | import java.io.ByteArrayOutputStream;
|
---|
25 | import java.io.ObjectOutputStream;
|
---|
26 | import java.io.ByteArrayInputStream;
|
---|
27 | import java.io.ObjectInputStream;
|
---|
28 | import java.io.IOException;
|
---|
29 |
|
---|
30 | /** A packing strategy which stores and retrieves objects using
|
---|
31 | * <a href="http://www.javasoft.com/products/jdk/1.1/docs/guide/serialization/index.html">Java serialization</a>.
|
---|
32 | *
|
---|
33 | * @version $Revision: 10737 $
|
---|
34 | * @author Martin Pool
|
---|
35 | * @see au.com.pharos.packing.Packing
|
---|
36 | **/
|
---|
37 | public class SerializationPacking extends Packing
|
---|
38 | implements java.io.Serializable
|
---|
39 | {
|
---|
40 | /** Convert an object to a Java serialization format.
|
---|
41 | *
|
---|
42 | * @returns a buffer containing a a serialized representation of <em>obj</em>;
|
---|
43 | * or null if <em>obj</em> if null
|
---|
44 | * @exception IllegalArgumentException if <em>obj</em> (or any object
|
---|
45 | * referred to by <em>obj</em>) cannot be serialized
|
---|
46 | **/
|
---|
47 | public byte[] toBytes(Object obj) throws IllegalArgumentException
|
---|
48 | {
|
---|
49 | if (obj == null)
|
---|
50 | return null;
|
---|
51 |
|
---|
52 | // Serialize via a stream into a buffer
|
---|
53 | ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
|
---|
54 | try {
|
---|
55 | ObjectOutputStream objStream = new ObjectOutputStream(byteStream);
|
---|
56 | objStream.writeObject(obj);
|
---|
57 | // Don't close the stream, or you will lose the byte array
|
---|
58 | objStream.flush();
|
---|
59 | } catch (IOException e) {
|
---|
60 | throw new IllegalArgumentException("IOException during serialization: " +
|
---|
61 | e.toString());
|
---|
62 | }
|
---|
63 |
|
---|
64 | return byteStream.toByteArray();
|
---|
65 | }
|
---|
66 |
|
---|
67 | /** Interprets <em>raw</em> as a Java serialization stream, and
|
---|
68 | * returns <B>the first</B> object read from that stream.
|
---|
69 | *
|
---|
70 | * <P><B>Note:</B>There is currently no means by which any later
|
---|
71 | * objects in the can be retrieved: it is assumed that the object
|
---|
72 | * written to the buffer contains references to all the
|
---|
73 | * information required. This is consistent with the fact that
|
---|
74 | * toBytes() stores only a single object, so this packing strategy
|
---|
75 | * is completely reversable.
|
---|
76 | **/
|
---|
77 | // FIXME: Is the correct word 'reversable', 'reflexive', or
|
---|
78 | // something else? -- [email protected]
|
---|
79 | public Object fromBytes(byte[] raw)
|
---|
80 | {
|
---|
81 | if (raw == null)
|
---|
82 | return null;
|
---|
83 |
|
---|
84 | Object obj;
|
---|
85 | try {
|
---|
86 | ByteArrayInputStream byteStream = new ByteArrayInputStream(raw);
|
---|
87 | ObjectInputStream objStream = new ObjectInputStream(byteStream);
|
---|
88 | obj = objStream.readObject();
|
---|
89 | objStream.close();
|
---|
90 | } catch (IOException e) {
|
---|
91 | throw new IllegalArgumentException("Exception during deserialization: " +
|
---|
92 | e.toString());
|
---|
93 | } catch (ClassNotFoundException e) {
|
---|
94 | throw new IllegalArgumentException("Exception during deserialization: " +
|
---|
95 | e.toString());
|
---|
96 | }
|
---|
97 | return obj;
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|