Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@apache.org Received: (qmail 58555 invoked from network); 11 Mar 2002 02:20:21 -0000 Received: from unknown (HELO nagoya.betaversion.org) (192.18.49.131) by daedalus.apache.org with SMTP; 11 Mar 2002 02:20:21 -0000 Received: (qmail 21436 invoked by uid 97); 11 Mar 2002 02:20:27 -0000 Delivered-To: qmlist-jakarta-archive-commons-dev@jakarta.apache.org Received: (qmail 21403 invoked by uid 97); 11 Mar 2002 02:20:27 -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 21392 invoked from network); 11 Mar 2002 02:20:26 -0000 Sender: jmcnally@localhost.localdomain Message-ID: <3C8C1350.DD4ADED7@collab.net> Date: Sun, 10 Mar 2002 18:15:44 -0800 From: John McNally Reply-To: Jakarta Commons Developers List X-Mailer: Mozilla 4.78 [en] (X11; U; Linux 2.4.7-10 i686) X-Accept-Language: en MIME-Version: 1.0 To: commons-dev@jakarta.apache.org Subject: [lang] PATCH add serialize method to lang.Objects Content-Type: multipart/mixed; boundary="------------9EE00575C176CFA2DADDEF01" X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N --------------9EE00575C176CFA2DADDEF01 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Here is another try with a better subject. Also changed the serialize method to return null instead of throwing an exception, so it behaves similarly to the deserialize method. Though I think both methods throwing an exception would be better. previous message: Here is a patch to add a serialize method to Objects to complement the deserialize method. Also included are unit tests for Objects. john mcnally --------------9EE00575C176CFA2DADDEF01 Content-Type: text/plain; charset=us-ascii; name="ObjectsTest.java" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="ObjectsTest.java" 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 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() { serializeDeserialize(); } public void testSerialize() { serializeDeserialize(); } private void serializeDeserialize() { 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)); } } --------------9EE00575C176CFA2DADDEF01 Content-Type: text/plain; charset=us-ascii; name="lang.diff" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="lang.diff" ? build.properties ? lang.diff ? target ? src/java/org/apache/commons/lang/Objects.patch ? src/test/org/apache/commons/lang/ObjectsTest.java Index: build.xml =================================================================== RCS file: /home/cvspublic/jakarta-commons-sandbox/lang/build.xml,v retrieving revision 1.4 diff -u -r1.4 build.xml --- build.xml 9 Mar 2002 17:11:38 -0000 1.4 +++ build.xml 11 Mar 2002 02:17:02 -0000 @@ -224,6 +224,7 @@ + + + + + + + + Index: src/java/org/apache/commons/lang/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 -r1.1 Objects.java --- src/java/org/apache/commons/lang/Objects.java 22 Feb 2002 05:57:15 -0000 1.1 +++ src/java/org/apache/commons/lang/Objects.java 11 Mar 2002 02:17:03 -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,45 @@ } } 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. + */ + public static byte[] serialize(Serializable obj) + { + 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(); + } + catch (IOException e) + { + byteArray = null; + } + finally + { + try + { + out.close(); + } + catch (Exception e) + { + // ignore; + } + } + return byteArray; } /** --------------9EE00575C176CFA2DADDEF01 Content-Type: text/plain; charset=us-ascii -- To unsubscribe, e-mail: For additional commands, e-mail: --------------9EE00575C176CFA2DADDEF01--