1 | /*
|
---|
2 | * module: pip/java/packing -- Packing strategy objects for converting
|
---|
3 | * Java objects to and from stored data.
|
---|
4 | * class: Packing -- Abstract strategy class for packing/unpacking data
|
---|
5 | *
|
---|
6 | * $Id: Packing.java 10737 2005-10-19 03:06:40Z kjdon $
|
---|
7 | * Copyright (C) 1997 Pharos IP Pty Ltd
|
---|
8 | *
|
---|
9 | * This program is free software; you can redistribute it and/or modify
|
---|
10 | * it under the terms of the GNU General Public License as published by
|
---|
11 | * the Free Software Foundation; either version 2 of the License, or
|
---|
12 | * (at your option) any later version.
|
---|
13 | *
|
---|
14 | * This program is distributed in the hope that it will be useful,
|
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
17 | * GNU General Public License for more details.
|
---|
18 | *
|
---|
19 | * You should have received a copy of the GNU General Public License
|
---|
20 | * along with this program; if not, write to the Free Software
|
---|
21 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
22 | */
|
---|
23 |
|
---|
24 | package au.com.pharos.packing;
|
---|
25 |
|
---|
26 | /** A packing Packing defines a method of converting an object to
|
---|
27 | * and from an array of bytes for storage in a database.
|
---|
28 | *
|
---|
29 | * <P>Generally, each packing Packing should be reflexive: that is,
|
---|
30 | * converting any object to and from an array of bytes should yield
|
---|
31 | * the same object, or an object appropriately equivalent.
|
---|
32 | *
|
---|
33 | * <P>Some Packing subclasses may not be able to pack all
|
---|
34 | * objects, or may not be able to unpack an object from some byte
|
---|
35 | * arrays. For example, only Strings can safely be stored as string
|
---|
36 | * representations, and not all arrays are valid Java serialization
|
---|
37 | * forms. In this case, the Packing object should throw an
|
---|
38 | * <code>IllegalArgumentException</code> from <code>toBytes()</code>
|
---|
39 | * or <code>toBytes()</code>.
|
---|
40 | *
|
---|
41 | * <P>In both cases, null references should be passed through
|
---|
42 | * unchanged.
|
---|
43 | *
|
---|
44 | * <P>Example:
|
---|
45 | * <PRE>
|
---|
46 | * Packing strategy = new StringPacking();
|
---|
47 | * byte[] raw = strategy.toBytes("Hello world");
|
---|
48 | * ... store and retrieve raw[]
|
---|
49 | * String result = (String) strategy.fromBytes(raw)
|
---|
50 | * </PRE>
|
---|
51 | *
|
---|
52 | *
|
---|
53 | * @see au.com.pharos.packing.StringPacking
|
---|
54 | * @see au.com.pharos.packing.NativeStringPacking
|
---|
55 | * @see au.com.pharos.packing.RawPacking
|
---|
56 | * @see au.com.pharos.packing.SerializationPacking
|
---|
57 | *
|
---|
58 | * @version $Revision: 10737 $
|
---|
59 | * @author Martin Pool
|
---|
60 | **/
|
---|
61 | public abstract class Packing {
|
---|
62 | /** Convert an object to an array of bytes. **/
|
---|
63 | public abstract byte[] toBytes(Object obj) throws IllegalArgumentException;
|
---|
64 |
|
---|
65 | /** Reconstitute an array of bytes into an object. **/
|
---|
66 | public abstract Object fromBytes(byte[] raw) throws IllegalArgumentException;
|
---|
67 |
|
---|
68 | /** Helper function that generates an IllegalArgumentException */
|
---|
69 | protected IllegalArgumentException cantConvert(Object obj) {
|
---|
70 | return new IllegalArgumentException
|
---|
71 | ("Can't pack object \"" + obj.toString() +
|
---|
72 | "\" (" + obj.getClass().toString() + ")" +
|
---|
73 | "\n\tusing strategy " + this.toString());
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|