Done, thanks John. - Dan
John McNally <[EMAIL PROTECTED]> writes: > Here is a patch to add a serialize method to Objects to complement the > deserialize method. Also included are unit tests for Objects. > > john mcnallyIndex: Objects.java > =================================================================== > RCS file: >/home/cvspublic/jakarta-commons-sandbox/lang/src/java/org/apache/commons/lang/Objects.java,v > retrieving revision 1.1 > diff -u -u -r1.1 Objects.java > --- Objects.java 22 Feb 2002 05:57:15 -0000 1.1 > +++ Objects.java 9 Mar 2002 22:56:58 -0000 > @@ -54,11 +54,14 @@ > * <http://www.apache.org/>. > */ > > -import java.io.BufferedInputStream; > import java.io.ByteArrayInputStream; > import java.io.IOException; > import java.io.InputStream; > import java.io.ObjectInputStream; > +import java.io.Serializable; > +import java.io.ByteArrayOutputStream; > +import java.io.ObjectOutputStream; > +import java.io.IOException; > > /** > * Common <code>Object</code> manipulation routines. > @@ -121,6 +124,39 @@ > } > } > return object; > + } > + > + > + /** > + * Converts a object to a byte array for storage/serialization. > + * > + * @param obj The Serializable to convert. > + * @return A byte[] with the converted Serializable. > + * @exception IOException, if conversion to a byte[] fails. > + */ > + public static byte[] serialize(Serializable obj) > + throws IOException > + { > + byte[] byteArray = null; > + ByteArrayOutputStream baos = null; > + ObjectOutputStream out = null; > + try > + { > + // These objects are closed in the finally. > + baos = new ByteArrayOutputStream(); > + out = new ObjectOutputStream(baos); > + > + out.writeObject(obj); > + byteArray = baos.toByteArray(); > + } > + finally > + { > + if (out != null) > + { > + out.close(); > + } > + } > + return byteArray; > } > > /** > Index: build.xml > =================================================================== > RCS file: /home/cvspublic/jakarta-commons-sandbox/lang/build.xml,v > retrieving revision 1.4 > diff -u -u -r1.4 build.xml > --- build.xml 9 Mar 2002 17:11:38 -0000 1.4 > +++ build.xml 9 Mar 2002 23:03:21 -0000 > @@ -224,6 +224,7 @@ > > <target name="test" depends="compile.tests, > test.strings, > + test.objects, > test.numberRange, > test.exception > " > @@ -236,6 +237,15 @@ > <java classname="${test.runner}" fork="yes" > failonerror="${test.failonerror}"> > <arg value="org.apache.commons.lang.StringsTest"/> > + <classpath refid="test.classpath"/> > + </java> > + </target> > + > + <target name="test.objects" depends="compile.tests"> > + <echo message="Running Objects tests ..."/> > + <java classname="${test.runner}" fork="yes" > + failonerror="${test.failonerror}"> > + <arg value="org.apache.commons.lang.ObjectsTest"/> > <classpath refid="test.classpath"/> > </java> > </target> > package org.apache.commons.lang; > > /* ==================================================================== > * The Apache Software License, Version 1.1 > * > * Copyright (c) 2001 The Apache Software Foundation. All rights > * reserved. > * > * Redistribution and use in source and binary forms, with or without > * modification, are permitted provided that the following conditions > * are met: > * > * 1. Redistributions of source code must retain the above copyright > * notice, this list of conditions and the following disclaimer. > * > * 2. Redistributions in binary form must reproduce the above copyright > * notice, this list of conditions and the following disclaimer in > * the documentation and/or other materials provided with the > * distribution. > * > * 3. The end-user documentation included with the redistribution, > * if any, must include the following acknowledgment: > * "This product includes software developed by the > * Apache Software Foundation (http://www.apache.org/)." > * Alternately, this acknowledgment may appear in the software itself, > * if and wherever such third-party acknowledgments normally appear. > * > * 4. The names "Apache" and "Apache Software Foundation" and > * "Apache Turbine" must not be used to endorse or promote products > * derived from this software without prior written permission. For > * written permission, please contact [EMAIL PROTECTED] > * > * 5. Products derived from this software may not be called "Apache", > * "Apache Turbine", nor may "Apache" appear in their name, without > * prior written permission of the Apache Software Foundation. > * > * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED > * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES > * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE > * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR > * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, > * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT > * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF > * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND > * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, > * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT > * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF > * SUCH DAMAGE. > * ==================================================================== > * > * This software consists of voluntary contributions made by many > * individuals on behalf of the Apache Software Foundation. For more > * information on the Apache Software Foundation, please see > * <http://www.apache.org/>. > */ > > import java.util.HashMap; > import java.io.IOException; > > import junit.framework.Test; > import junit.framework.TestCase; > import junit.framework.TestSuite; > > /** > * Unit tests {@link org.apache.commons.lang.Objects}. > * > * @author <a href="mailto:[EMAIL PROTECTED]">John McNally</a> > */ > public class ObjectsTest extends TestCase > { > private static final String FOO = "foo"; > private static final String BAR = "bar"; > private static final String MAP_ERROR = > "Map did not serialize and deserialize to equivalent map"; > > public ObjectsTest(String name) > { > super(name); > } > > public void testIsNull() > { > Object o = FOO; > Object dflt = BAR; > assertEquals("dflt was not returned when o was null", dflt, > Objects.isNull(null, dflt)); > assertEquals("dflt was returned when o was not null", o, > Objects.isNull(o, dflt)); > } > > public void testDeserialize() > throws IOException > { > serializeDeserialize(); > } > > public void testSerialize() > throws IOException > { > serializeDeserialize(); > } > > private void serializeDeserialize() > throws IOException > { > HashMap original = new HashMap(); > original.put(FOO, BAR); > original.put(BAR, FOO); > byte[] ser = Objects.serialize(original); > HashMap deser = (HashMap)Objects.deserialize(ser); > assertEquals(MAP_ERROR, original.get(FOO), deser.get(FOO)); > assertEquals(MAP_ERROR, original.get(BAR), deser.get(BAR)); > } > > public void testEquals() > { > assertTrue("Objects.equals(null, null) returned false", > Objects.equals(null, null)); > assertTrue("Objects.equals(\"foo\", null) returned true", > !Objects.equals(FOO, null)); > assertTrue("Objects.equals(null, \"bar\") returned true", > !Objects.equals(null, BAR)); > assertTrue("Objects.equals(\"foo\", \"bar\") returned true", > !Objects.equals(FOO, BAR)); > assertTrue("Objects.equals(\"foo\", \"foo\") returned false", > Objects.equals(FOO, FOO)); > } > > } > > -- > To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> > For additional commands, e-mail: <mailto:[EMAIL PROTECTED]> -- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>
