Author: julien(a)jboss.com
Date: 2008-02-07 12:37:56 -0500 (Thu, 07 Feb 2008)
New Revision: 9836
Added:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/io/Serialization.java
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/io/SerializationTestCase.java
Modified:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/io/IOTools.java
Log:
added concept of Serialization for known types (such as the recurrent
Map<String,String[]>) with a compact format
Modified:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/io/IOTools.java
===================================================================
---
modules/common/trunk/common/src/main/java/org/jboss/portal/common/io/IOTools.java 2008-02-07
16:47:40 UTC (rev 9835)
+++
modules/common/trunk/common/src/main/java/org/jboss/portal/common/io/IOTools.java 2008-02-07
17:37:56 UTC (rev 9836)
@@ -40,6 +40,8 @@
import java.io.ObjectInputStream;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
+import java.io.DataOutputStream;
+import java.util.Map;
/**
* IO tools.
@@ -384,6 +386,49 @@
return (Serializable)ois.readObject();
}
+ public static <T> byte[] serialize(Serialization<T> serialization, T t)
+ {
+ if (serialization == null)
+ {
+ throw new IllegalArgumentException();
+ }
+ if (t == null)
+ {
+ throw new IllegalArgumentException("No null object to serialize");
+ }
+ try
+ {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ serialization.serialize(t, baos);
+ return baos.toByteArray();
+ }
+ catch (IOException e)
+ {
+ throw new UndeclaredIOException(e);
+ }
+ }
+
+ public static <T> T unserialize(Serialization<T> serialization, byte[]
bytes)
+ {
+ if (serialization == null)
+ {
+ throw new IllegalArgumentException();
+ }
+ if (bytes == null)
+ {
+ throw new IllegalArgumentException("No null bytes to unserialize");
+ }
+ try
+ {
+ ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
+ return serialization.unserialize(bais);
+ }
+ catch (IOException e)
+ {
+ throw new UndeclaredIOException(e);
+ }
+ }
+
/**
* Check that the provided input stream is buffered. If the argument is already an
instance of <code>BufferedInputStream</code>
* no operation will be performed, otherwise a instance of
<code>BufferedInputStream</code> will be created and returned.
Added:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/io/Serialization.java
===================================================================
---
modules/common/trunk/common/src/main/java/org/jboss/portal/common/io/Serialization.java
(rev 0)
+++
modules/common/trunk/common/src/main/java/org/jboss/portal/common/io/Serialization.java 2008-02-07
17:37:56 UTC (rev 9836)
@@ -0,0 +1,121 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2008, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software 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 *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.common.io;
+
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.io.DataInputStream;
+import java.io.OutputStream;
+import java.io.InputStream;
+import java.util.Map;
+import java.util.HashMap;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 630 $
+ */
+public interface Serialization<T>
+{
+
+ Serialization<Map<String, String[]>> PARAMETER_MAP = new
Serialization<Map<String, String[]>>()
+ {
+ public void serialize(Map<String, String[]> map, OutputStream out) throws
IOException, IllegalArgumentException
+ {
+ if (map == null)
+ {
+ throw new IllegalArgumentException("No null map allowed");
+ }
+
+ //
+ DataOutputStream data = out instanceof DataOutputStream ? (DataOutputStream)out
: new DataOutputStream(out);
+
+ //
+ data.writeInt(map.size());
+
+ //
+ for (Map.Entry entry : map.entrySet())
+ {
+ String name = (String)entry.getKey();
+ data.writeUTF(name);
+
+ //
+ String[] values = (String[])entry.getValue();
+ if (values == null)
+ {
+ throw new IllegalArgumentException("No null values are allowed in the
map");
+ }
+
+ //
+ int length = values.length;
+ data.writeInt(length);
+
+ //
+ for (String value : values)
+ {
+ if (value == null)
+ {
+ throw new IllegalArgumentException("No null value in the String[]
are allowed in the map");
+ }
+ data.writeUTF(value);
+ }
+ }
+
+ //
+ data.flush();
+ }
+
+ public Map<String, String[]> unserialize(InputStream in) throws IOException
+ {
+ if (in == null)
+ {
+ throw new IllegalArgumentException("No null input stream");
+ }
+
+ //
+ DataInputStream data = in instanceof DataInputStream ? (DataInputStream)in : new
DataInputStream(in);
+
+ //
+ int size = data.readInt();
+ Map<String, String[]> tmp = new HashMap<String, String[]>(size);
+ while (size-- > 0)
+ {
+ String name = data.readUTF();
+ int length = data.readInt();
+ String[] values = new String[length];
+ for (int i = 0;i < length;i++)
+ {
+ values[i] = data.readUTF();
+ }
+ tmp.put(name, values);
+ }
+
+ //
+ return tmp;
+ }
+ };
+
+ public abstract void serialize(T t, OutputStream out) throws IOException,
IllegalArgumentException;
+
+ public abstract T unserialize(InputStream in) throws IOException,
IllegalArgumentException;
+
+}
Added:
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/io/SerializationTestCase.java
===================================================================
---
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/io/SerializationTestCase.java
(rev 0)
+++
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/io/SerializationTestCase.java 2008-02-07
17:37:56 UTC (rev 9836)
@@ -0,0 +1,62 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2008, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software 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 *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.test.common.io;
+
+import org.jboss.portal.common.io.Serialization;
+import org.jboss.portal.common.io.IOTools;
+import org.jboss.portal.common.junit.ExtendedAssert;
+import org.jboss.portal.common.util.MapBuilder;
+
+import java.util.Map;
+import java.util.HashMap;
+
+import junit.framework.TestCase;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 630 $
+ */
+public class SerializationTestCase extends TestCase
+{
+
+ public void testParameterMapSerialization()
+ {
+ check(new HashMap<String, String[]>());
+ check(MapBuilder.hashMap("foo", new
String[]{"foo_value"}).get());
+ check(MapBuilder.hashMap("foo", new
String[]{"foo_value1","foo_value2"}).get());
+ check(MapBuilder.hashMap("foo", new
String[]{"foo_value1","foo_value2"}).put("bar", new
String[]{"bar_value"}).get());
+ }
+
+ private void check(Map<String, String[]> map)
+ {
+ byte[] bytes = IOTools.serialize(Serialization.PARAMETER_MAP, map);
+ Map<String, String[]> copy = IOTools.unserialize(Serialization.PARAMETER_MAP,
bytes);
+ assertEquals(map.keySet(), copy.keySet());
+ for (Map.Entry<String, String[]> entry : map.entrySet())
+ {
+ String[] values = map.get(entry.getKey());
+ String[] valuesCopy = copy.get(entry.getKey());
+ ExtendedAssert.assertEquals(values, valuesCopy);
+ }
+ }
+}