Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@apache.org Received: (qmail 54921 invoked from network); 11 Mar 2002 08:05:40 -0000 Received: from unknown (HELO nagoya.betaversion.org) (192.18.49.131) by daedalus.apache.org with SMTP; 11 Mar 2002 08:05:40 -0000 Received: (qmail 6566 invoked by uid 97); 11 Mar 2002 08:05:50 -0000 Delivered-To: qmlist-jakarta-archive-commons-dev@jakarta.apache.org Received: (qmail 6546 invoked by uid 97); 11 Mar 2002 08:05:50 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 6535 invoked from network); 11 Mar 2002 08:05:49 -0000 Sender: dlr@despot.finemaltcoding.com To: "Jakarta Commons Developers List" Subject: Re: add serialize method to lang.Objects References: <3C8A94BE.FD9074EF@collab.net> Content-Type: text/plain; charset=US-ASCII From: Daniel Rall Date: Mon, 11 Mar 2002 00:05:47 -0800 In-Reply-To: <3C8A94BE.FD9074EF@collab.net> (John McNally's message of "Sat, 09 Mar 2002 15:03:26 -0800") Message-ID: Lines: 236 User-Agent: Gnus/5.090004 (Oort Gnus v0.04) XEmacs/21.1 (Cuyahoga Valley) MIME-Version: 1.0 X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N Done, thanks John. - Dan John McNally 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 @@ > * . > */ > > -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 Object 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 @@ > > @@ -236,6 +237,15 @@ > failonerror="${test.failonerror}"> > > + > + > + > + > + > + > + + failonerror="${test.failonerror}"> > + > > > > 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 apache@apache.org. > * > * 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 > * . > */ > > 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 John McNally > */ > 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: > For additional commands, e-mail: -- To unsubscribe, e-mail: For additional commands, e-mail: