source: trunk/gsdl3/src/packages/javagdbm/java/au/com/pharos/packing/StringPacking.java@ 10737

Last change on this file since 10737 was 10737, checked in by kjdon, 19 years ago

Java Wrapper for GDBM, from Martin Pool. Original website gone, so added it all in here. I have modified the Makefiles to work in greenstone, and on macs, and added windows makefiles

  • Property svn:keywords set to Author Date Id Revision
File size: 2.6 KB
Line 
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: StringPacking.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
23package au.com.pharos.packing;
24
25/** StringPacking converts Java Strings to and from byte arrays
26 * using the default UTF-8 encoding.
27 *
28 * <P>For strings containing only ASCII-7 characters, this is
29 * equivalent to <code>NativeStringPacking</code> but probably
30 * somewhat slower. However, it will safely handle non-ASCII
31 * characters, or unusual locales.
32 *
33 * @see au.com.pharos.packing.Packing
34 * @see au.com.pharos.packing.NativeStringPacking
35 **/
36public class StringPacking extends Packing
37implements java.io.Serializable
38{
39 /** Convert a String to an array of bytes using Java's default
40 * encoding.
41 *
42 * <P>If <em>obj</em> is not a String, array of bytes, or null
43 * then it's <code>toString()</code> method is called first to
44 * convert it to an array of bytes. This will lose information in
45 * many cases.
46 *
47 * @param obj The object to convert.
48 *
49 * @return <em>obj</em> converted to an array of bytes; or null if
50 * <em>obj</em> is null.
51 **/
52 public byte[] toBytes(Object obj)
53 {
54 if (obj == null)
55 return null;
56 else if (obj instanceof byte[])
57 return (byte[]) obj;
58 else if (obj instanceof String)
59 return ((String) obj).getBytes();
60 else
61 return obj.toString().getBytes();
62 }
63
64 /** Decode an array of bytes using the default String encoding.
65 *
66 * @param raw An array of bytes to decode.
67 *
68 * @return A String representation of <em>raw</em>; or null if
69 * <em>raw</em> is null.
70 **/
71 public Object fromBytes(byte[] raw)
72 {
73 if (raw == null)
74 return null;
75 else {
76 try{
77 return new String(raw, "UTF-8");
78 } catch (Exception e) {
79 System.err.println("String Packing:encoding not supported");
80 return new String(raw);
81 }
82 }
83 }
84}
85
Note: See TracBrowser for help on using the repository browser.