1 | /*
|
---|
2 | * module: pip/java/packing -- Strategy objects for converting
|
---|
3 | * Java objects to and from stored data.
|
---|
4 | *
|
---|
5 | * Copyright (C) 1997 Pharos IP Pty Ltd
|
---|
6 | * $Id: RawPacking.java 10737 2005-10-19 03:06:40Z kjdon $
|
---|
7 | *
|
---|
8 | * This program is free software; you can redistribute it and/or modify
|
---|
9 | * it under the terms of the GNU General Public License as published by
|
---|
10 | * the Free Software Foundation; either version 2 of the License, or
|
---|
11 | * (at your option) any later version.
|
---|
12 | *
|
---|
13 | * This program is distributed in the hope that it will be useful,
|
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
16 | * GNU General Public License for more details.
|
---|
17 | *
|
---|
18 | * You should have received a copy of the GNU General Public License
|
---|
19 | * along with this program; if not, write to the Free Software
|
---|
20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
21 | */
|
---|
22 |
|
---|
23 | package au.com.pharos.packing;
|
---|
24 |
|
---|
25 | /** A packing strategy which stores only raw arrays of bytes. Other
|
---|
26 | * objects cannot be stored using this strategy.
|
---|
27 | *
|
---|
28 | * @author Martin Pool
|
---|
29 | * @version $Revision: 10737 $
|
---|
30 | **/
|
---|
31 | public class RawPacking extends Packing implements java.io.Serializable
|
---|
32 | {
|
---|
33 | /** Perform the trivial packing case, casting an array of bytes
|
---|
34 | * into the same.
|
---|
35 | *
|
---|
36 | * @throw IllegalArgumentException if <em>obj</em> is neith null nor
|
---|
37 | * an array of bytes.
|
---|
38 | */
|
---|
39 | public byte[] toBytes(Object obj) throws IllegalArgumentException
|
---|
40 | {
|
---|
41 | if (obj == null)
|
---|
42 | return null;
|
---|
43 | else if (obj instanceof byte[])
|
---|
44 | return (byte[]) obj;
|
---|
45 | else
|
---|
46 | throw cantConvert(obj);
|
---|
47 | }
|
---|
48 |
|
---|
49 | /** Performs the trivial unpacking case of returning <em>raw</em>
|
---|
50 | * untouched.
|
---|
51 | *
|
---|
52 | * @return either null or <em>raw</em>; always assignable to
|
---|
53 | * <code>byte[]</code>
|
---|
54 | **/
|
---|
55 | public Object fromBytes(byte[] raw)
|
---|
56 | {
|
---|
57 | return raw;
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|