/* * module: pip/java/packing -- Strategy objects for converting * Java objects to and from stored data. * * Copyright (C) 1997 Pharos IP Pty Ltd * $Id: StringPacking.java,v 1.3 1997-10-23 21:09:59+10 mbp Exp $ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ package au.com.pharos.packing; /** StringPacking converts Java Strings to and from byte arrays * using the default UTF-8 encoding. * *

For strings containing only ASCII-7 characters, this is * equivalent to NativeStringPacking but probably * somewhat slower. However, it will safely handle non-ASCII * characters, or unusual locales. * * @see au.com.pharos.packing.Packing * @see au.com.pharos.packing.NativeStringPacking **/ public class StringPacking extends Packing implements java.io.Serializable { /** Convert a String to an array of bytes using Java's default * encoding. * *

If obj is not a String, array of bytes, or null * then it's toString() method is called first to * convert it to an array of bytes. This will lose information in * many cases. * * @param obj The object to convert. * * @return obj converted to an array of bytes; or null if * obj is null. **/ public byte[] toBytes(Object obj) { if (obj == null) return null; else if (obj instanceof byte[]) return (byte[]) obj; else if (obj instanceof String) return ((String) obj).getBytes(); else return obj.toString().getBytes(); } /** Decode an array of bytes using the default String encoding. * * @param raw An array of bytes to decode. * * @return A String representation of raw; or null if * raw is null. **/ public Object fromBytes(byte[] raw) { if (raw == null) return null; else return new String(raw); } }