source: trunk/gsdl3/src/packages/javagdbm/java/au/com/pharos/packing/NativeStringPacking.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.5 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: NativeStringPacking.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/** Convert an array of bytes in a Java string without using String
26 * encodings, just using expanding 8-bit bytes to 16-bit chars.
27 *
28 * <P>This will only work for ASCII characters, of course, but for
29 * moving bulk data around it is considerably faster than using
30 * UTF-8 encoding strategies. Naively compressing Unicode into
31 * 8-bit characters in this manner is deprecated in JDK1.1 to
32 * help ensure that programs are Unicode-safe.
33 *
34 * @author Martin Pool
35 * @version $Revision: 10737 $
36 *
37 * @see au.com.pharos.packing.Packing
38 * @see au.com.pharos.packing.StringPacking
39 */
40public class NativeStringPacking extends Packing
41{
42 /** Convert an object to an ASCII String representation.
43 *
44 * @return null if obj is null; obj is obj is already a
45 * byte[]; otherwise an native 8-bite encoded version of
46 * obj.toString().
47 **/
48 public byte[] toBytes(final Object obj)
49 {
50 String str;
51 if (obj == null)
52 return null;
53 else if (obj instanceof byte[])
54 return (byte[]) obj;
55 else if (obj instanceof String)
56 str = (String) obj;
57 else
58 str = obj.toString();
59
60 int len = str.length();
61 byte[] rawArray = new byte[len];
62 str.getBytes(0, len, rawArray, 0);
63 return rawArray;
64 }
65
66 /** Convert an array of bytes into a String
67 *
68 * @param raw the raw data to be decoded.
69 *
70 * @return null if <em>raw</em> is null; otherwise a string with the low
71 * 8 bits of each character taken from the corresponding byte in
72 * <em>raw</em>.
73 **/
74 public Object fromBytes(final byte[] raw)
75 {
76 if (raw == null)
77 return null;
78 else
79 return new String(raw, 0);
80 }
81}
82
Note: See TracBrowser for help on using the repository browser.