[infinispan-commits] Infinispan SVN: r2138 - in trunk/core/src: main/java/org/infinispan/marshall/jboss and 1 other directories.

infinispan-commits at lists.jboss.org infinispan-commits at lists.jboss.org
Tue Aug 3 11:24:12 EDT 2010


Author: galder.zamarreno at jboss.com
Date: 2010-08-03 11:24:12 -0400 (Tue, 03 Aug 2010)
New Revision: 2138

Added:
   trunk/core/src/main/java/org/infinispan/marshall/AbstractMarshaller.java
Modified:
   trunk/core/src/main/java/org/infinispan/marshall/AbstractStreamingMarshaller.java
   trunk/core/src/main/java/org/infinispan/marshall/VersionAwareMarshaller.java
   trunk/core/src/main/java/org/infinispan/marshall/jboss/GenericJBossMarshaller.java
   trunk/core/src/main/java/org/infinispan/marshall/jboss/JBossMarshaller.java
   trunk/core/src/test/java/org/infinispan/marshall/TestObjectStreamMarshaller.java
Log:
[ISPN-566] (Group marshaller logic in AbstractMarshaller) Done.

Added: trunk/core/src/main/java/org/infinispan/marshall/AbstractMarshaller.java
===================================================================
--- trunk/core/src/main/java/org/infinispan/marshall/AbstractMarshaller.java	                        (rev 0)
+++ trunk/core/src/main/java/org/infinispan/marshall/AbstractMarshaller.java	2010-08-03 15:24:12 UTC (rev 2138)
@@ -0,0 +1,52 @@
+package org.infinispan.marshall;
+
+import org.infinispan.io.ByteBuffer;
+
+import java.io.IOException;
+
+/**
+ * Abstract Marshaller implementation containing shared implementations.
+ *
+ * @author Galder Zamarreño
+ * @since 4.1
+ */
+public abstract class AbstractMarshaller implements Marshaller {
+
+   protected static final int DEFAULT_BUF_SIZE = 512;
+
+   /**
+    * This is a convenience method for converting an object into a {@link org.infinispan.io.ByteBuffer} which takes
+    * an estimated size as parameter. A {@link org.infinispan.io.ByteBuffer} allows direct access to the byte
+    * array with minimal array copying
+    *
+    * @param o object to marshall
+    * @param estimatedSize an estimate of how large the resulting byte array may be
+    * @return a ByteBuffer
+    * @throws Exception
+    */
+   protected abstract ByteBuffer objectToBuffer(Object o, int estimatedSize) throws IOException;
+
+   @Override
+   public ByteBuffer objectToBuffer(Object obj) throws IOException {
+      return objectToBuffer(obj, DEFAULT_BUF_SIZE);
+   }
+
+   @Override
+   public byte[] objectToByteBuffer(Object o) throws IOException {
+      return objectToByteBuffer(o, DEFAULT_BUF_SIZE);
+   }
+
+   @Override
+   public byte[] objectToByteBuffer(Object obj, int estimatedSize) throws IOException {
+      ByteBuffer b = objectToBuffer(obj, estimatedSize);
+      byte[] bytes = new byte[b.getLength()];
+      System.arraycopy(b.getBuf(), b.getOffset(), bytes, 0, b.getLength());
+      return bytes;
+   }
+
+   @Override
+   public Object objectFromByteBuffer(byte[] buf) throws IOException, ClassNotFoundException {
+      return objectFromByteBuffer(buf, 0, buf.length);
+   }
+
+}

Modified: trunk/core/src/main/java/org/infinispan/marshall/AbstractStreamingMarshaller.java
===================================================================
--- trunk/core/src/main/java/org/infinispan/marshall/AbstractStreamingMarshaller.java	2010-08-03 13:11:51 UTC (rev 2137)
+++ trunk/core/src/main/java/org/infinispan/marshall/AbstractStreamingMarshaller.java	2010-08-03 15:24:12 UTC (rev 2138)
@@ -11,10 +11,9 @@
  * @author Manik Surtani
  * @since 4.0
  */
-public abstract class AbstractStreamingMarshaller implements StreamingMarshaller {
+public abstract class AbstractStreamingMarshaller extends AbstractMarshaller implements StreamingMarshaller {
 
-   protected static final int DEFAULT_BUF_SIZE = 512;
-
+   @Override
    public Object objectFromInputStream(InputStream inputStream) throws IOException, ClassNotFoundException {
       // TODO: available() call commented until https://issues.apache.org/jira/browse/HTTPCORE-199 httpcore-nio issue is fixed. 
       // int len = inputStream.available();
@@ -25,7 +24,4 @@
       return objectFromByteBuffer(bytes.getRawBuffer(), 0, bytes.size());
    }
 
-   public byte[] objectToByteBuffer(Object o) throws IOException {
-      return objectToByteBuffer(o, DEFAULT_BUF_SIZE);
-   }
 }

Modified: trunk/core/src/main/java/org/infinispan/marshall/VersionAwareMarshaller.java
===================================================================
--- trunk/core/src/main/java/org/infinispan/marshall/VersionAwareMarshaller.java	2010-08-03 13:11:51 UTC (rev 2137)
+++ trunk/core/src/main/java/org/infinispan/marshall/VersionAwareMarshaller.java	2010-08-03 15:24:12 UTC (rev 2138)
@@ -34,7 +34,6 @@
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.NotSerializableException;
 import java.io.ObjectInput;
 import java.io.ObjectOutput;
 import java.io.OutputStream;
@@ -86,13 +85,13 @@
       return CUSTOM_MARSHALLER;
    }
 
-   private ByteBuffer objectToBuffer(Object obj, int estimatedSize) throws IOException {
+   @Override
+   protected ByteBuffer objectToBuffer(Object obj, int estimatedSize) throws IOException {
       ExposedByteArrayOutputStream baos = new ExposedByteArrayOutputStream(estimatedSize);
       ObjectOutput out = startObjectOutput(baos, false);
       try {
          defaultMarshaller.objectToObjectStream(obj, out);
-      } catch (
-              java.io.NotSerializableException nse) {
+      } catch (java.io.NotSerializableException nse) {
          if (log.isTraceEnabled()) log.trace("Object is not serializable", nse);
          throw new org.infinispan.marshall.NotSerializableException(nse.getMessage(), nse.getCause());
       } catch (IOException ioe) {
@@ -104,10 +103,7 @@
       return new ByteBuffer(baos.getRawBuffer(), 0, baos.size());
    }
 
-   public ByteBuffer objectToBuffer(Object obj) throws IOException {
-      return objectToBuffer(obj, DEFAULT_BUF_SIZE);
-   }
-
+   @Override
    public Object objectFromByteBuffer(byte[] bytes, int offset, int len) throws IOException, ClassNotFoundException {
       ByteArrayInputStream is = new ByteArrayInputStream(bytes, offset, len);
       ObjectInput in = startObjectInput(is, false);
@@ -120,6 +116,7 @@
       return o;
    }
 
+   @Override
    public ObjectOutput startObjectOutput(OutputStream os, boolean isReentrant) throws IOException {
       ObjectOutput out = defaultMarshaller.startObjectOutput(os, isReentrant);
       try {
@@ -133,10 +130,12 @@
       return out;
    }
 
+   @Override
    public void finishObjectOutput(ObjectOutput oo) {
       defaultMarshaller.finishObjectOutput(oo);
    }
 
+   @Override
    public void objectToObjectStream(Object obj, ObjectOutput out) throws IOException {
       /* No need to write version here. Clients should either be calling either:
        * - startObjectOutput() -> objectToObjectStream() -> finishObjectOutput()  
@@ -148,6 +147,7 @@
       defaultMarshaller.objectToObjectStream(obj, out);
    }
 
+   @Override   
    public ObjectInput startObjectInput(InputStream is, boolean isReentrant) throws IOException {
       ObjectInput in = defaultMarshaller.startObjectInput(is, isReentrant);
       int versionId;
@@ -163,10 +163,12 @@
       return in;
    }
 
+   @Override
    public void finishObjectInput(ObjectInput oi) {
       defaultMarshaller.finishObjectInput(oi);
    }
 
+   @Override   
    public Object objectFromObjectStream(ObjectInput in) throws IOException, ClassNotFoundException {
       /* No need to read version here. Clients should either be calling either:
        * - startObjectInput() -> objectFromObjectStream() -> finishObjectInput()
@@ -178,17 +180,6 @@
       return defaultMarshaller.objectFromObjectStream(in);
    }
 
-   public byte[] objectToByteBuffer(Object obj, int estimatedSize) throws IOException {
-      ByteBuffer b = objectToBuffer(obj, estimatedSize);
-      byte[] bytes = new byte[b.getLength()];
-      System.arraycopy(b.getBuf(), b.getOffset(), bytes, 0, b.getLength());
-      return bytes;
-   }
-
-   public Object objectFromByteBuffer(byte[] buf) throws IOException, ClassNotFoundException {
-      return objectFromByteBuffer(buf, 0, buf.length);
-   }
-
    @Override
    public boolean isMarshallable(Object o) {
       return defaultMarshaller.isMarshallable(o);

Modified: trunk/core/src/main/java/org/infinispan/marshall/jboss/GenericJBossMarshaller.java
===================================================================
--- trunk/core/src/main/java/org/infinispan/marshall/jboss/GenericJBossMarshaller.java	2010-08-03 13:11:51 UTC (rev 2137)
+++ trunk/core/src/main/java/org/infinispan/marshall/jboss/GenericJBossMarshaller.java	2010-08-03 15:24:12 UTC (rev 2138)
@@ -3,6 +3,7 @@
 import org.infinispan.CacheException;
 import org.infinispan.io.ByteBuffer;
 import org.infinispan.io.ExposedByteArrayOutputStream;
+import org.infinispan.marshall.AbstractMarshaller;
 import org.infinispan.marshall.Marshaller;
 import org.infinispan.util.Util;
 import org.infinispan.util.logging.Log;
@@ -36,9 +37,8 @@
  * @version 4.1
  * @see http://www.jboss.org/jbossmarshalling
  */
-public class GenericJBossMarshaller implements Marshaller {
+public class GenericJBossMarshaller extends AbstractMarshaller {
 
-   protected static final int DEFAULT_BUF_SIZE = 512;
    protected static final Log log = LogFactory.getLog(JBossMarshaller.class);
    protected static final String DEFAULT_MARSHALLER_FACTORY = "org.jboss.marshalling.river.RiverMarshallerFactory";
    protected ClassLoader defaultCl = this.getClass().getClassLoader();
@@ -89,18 +89,6 @@
       }
    };
 
-
-   public byte[] objectToByteBuffer(Object obj, int estimatedSize) throws IOException {
-      ByteBuffer b = objectToBuffer(obj, estimatedSize);
-      byte[] bytes = new byte[b.getLength()];
-      System.arraycopy(b.getBuf(), b.getOffset(), bytes, 0, b.getLength());
-      return bytes;
-   }
-
-   public ByteBuffer objectToBuffer(Object o) throws IOException {
-      return objectToBuffer(o, DEFAULT_BUF_SIZE);
-   }
-
    public void objectToObjectStream(Object obj, ObjectOutput out) throws IOException {
       ClassLoader toUse = defaultCl;
       Thread current = Thread.currentThread();
@@ -116,7 +104,8 @@
       }
    }
 
-   private ByteBuffer objectToBuffer(Object o, int estimatedSize) throws IOException {
+   @Override
+   protected ByteBuffer objectToBuffer(Object o, int estimatedSize) throws IOException {
       ExposedByteArrayOutputStream baos = new ExposedByteArrayOutputStream(estimatedSize);
       ObjectOutput marshaller = startObjectOutput(baos, false);
       try {
@@ -145,11 +134,7 @@
       }
    }
 
-
-   public Object objectFromByteBuffer(byte[] buf) throws IOException, ClassNotFoundException {
-      return objectFromByteBuffer(buf, 0, buf.length);
-   }
-
+   @Override
    public Object objectFromByteBuffer(byte[] buf, int offset, int length) throws IOException,
            ClassNotFoundException {
       ByteArrayInputStream is = new ByteArrayInputStream(buf, offset, length);
@@ -185,20 +170,6 @@
       }
    }
 
-   public Object objectFromInputStream(InputStream inputStream) throws IOException, ClassNotFoundException {
-      // TODO: available() call commented until https://issues.apache.org/jira/browse/HTTPCORE-199 httpcore-nio issue is fixed.
-      // int len = inputStream.available();
-      ExposedByteArrayOutputStream bytes = new ExposedByteArrayOutputStream(DEFAULT_BUF_SIZE);
-      byte[] buf = new byte[Math.min(DEFAULT_BUF_SIZE, 1024)];
-      int bytesRead;
-      while ((bytesRead = inputStream.read(buf, 0, buf.length)) != -1) bytes.write(buf, 0, bytesRead);
-      return objectFromByteBuffer(bytes.getRawBuffer(), 0, bytes.size());
-   }
-
-   public byte[] objectToByteBuffer(Object o) throws IOException {
-      return objectToByteBuffer(o, DEFAULT_BUF_SIZE);
-   }
-
    @Override
    public boolean isMarshallable(Object o) {
       return (o instanceof Serializable || o instanceof Externalizable);
@@ -209,12 +180,14 @@
       private static final Class[] EMPTY_CLASSES = {};
       private static final Object[] EMPTY_OBJECTS = {};
 
+      @Override
       public void handleMarshallingException(Throwable problem, Object subject) {
          if (log.isDebugEnabled()) {
             TraceInformation.addUserInformation(problem, "toString = " + subject.toString());
          }
       }
 
+      @Override
       public void handleUnmarshallingException(Throwable problem, Class<?> subjectClass) {
          if (log.isDebugEnabled()) {
             StringBuilder builder = new StringBuilder();
@@ -239,6 +212,7 @@
          }
       }
 
+      @Override
       public void handleUnmarshallingException(Throwable problem) {
          // no-op
       }

Modified: trunk/core/src/main/java/org/infinispan/marshall/jboss/JBossMarshaller.java
===================================================================
--- trunk/core/src/main/java/org/infinispan/marshall/jboss/JBossMarshaller.java	2010-08-03 13:11:51 UTC (rev 2137)
+++ trunk/core/src/main/java/org/infinispan/marshall/jboss/JBossMarshaller.java	2010-08-03 15:24:12 UTC (rev 2138)
@@ -82,6 +82,17 @@
    }
 
    @Override
+   public Object objectFromInputStream(InputStream inputStream) throws IOException, ClassNotFoundException {
+      // TODO: available() call commented until https://issues.apache.org/jira/browse/HTTPCORE-199 httpcore-nio issue is fixed.
+      // int len = inputStream.available();
+      ExposedByteArrayOutputStream bytes = new ExposedByteArrayOutputStream(DEFAULT_BUF_SIZE);
+      byte[] buf = new byte[Math.min(DEFAULT_BUF_SIZE, 1024)];
+      int bytesRead;
+      while ((bytesRead = inputStream.read(buf, 0, buf.length)) != -1) bytes.write(buf, 0, bytesRead);
+      return objectFromByteBuffer(bytes.getRawBuffer(), 0, bytes.size());
+   }
+
+   @Override
    public boolean isMarshallable(Object o) {
       return super.isMarshallable(o) || ReflectionUtil.isAnnotationPresent(o.getClass(), Marshallable.class);
    }

Modified: trunk/core/src/test/java/org/infinispan/marshall/TestObjectStreamMarshaller.java
===================================================================
--- trunk/core/src/test/java/org/infinispan/marshall/TestObjectStreamMarshaller.java	2010-08-03 13:11:51 UTC (rev 2137)
+++ trunk/core/src/test/java/org/infinispan/marshall/TestObjectStreamMarshaller.java	2010-08-03 15:24:12 UTC (rev 2138)
@@ -24,30 +24,36 @@
    public TestObjectStreamMarshaller() {
    }
 
+   @Override
    public ObjectOutput startObjectOutput(OutputStream os, boolean isReentrant) throws IOException {
       return new ObjectOutputStream(os);
    }
 
+   @Override
    public void finishObjectOutput(ObjectOutput oo) {
       Util.flushAndCloseOutput(oo);
    }
 
+   @Override
    public void objectToObjectStream(Object obj, ObjectOutput out) throws IOException {
       String xml = xs.toXML(obj);
       debug("Writing: \n" + xml);
       out.writeObject(xml);
    }
 
+   @Override
    public Object objectFromObjectStream(ObjectInput in) throws IOException, ClassNotFoundException {
       String xml = (String) in.readObject();
       debug("Reading: \n" + xml);
       return xs.fromXML(xml);
    }
 
+   @Override
    public ObjectInput startObjectInput(InputStream is, boolean isReentrant) throws IOException {
       return new ObjectInputStream(is);
    }
 
+   @Override
    public void finishObjectInput(ObjectInput oi) {
       if (oi != null) {
          try {
@@ -57,28 +63,22 @@
       }
    }
 
-   public ByteBuffer objectToBuffer(Object o) throws IOException {
-      byte[] b = objectToByteBuffer(o);
-      return new ByteBuffer(b, 0, b.length);
-   }
-
-   public Object objectFromByteBuffer(byte[] buf, int offset, int length) throws IOException, ClassNotFoundException {
-      byte[] newBytes = new byte[length];
-      System.arraycopy(buf, offset, newBytes, 0, length);
-      return objectFromByteBuffer(newBytes);
-   }
-
-   public byte[] objectToByteBuffer(Object obj, int estimatedSize) throws IOException {
+   @Override
+   protected ByteBuffer objectToBuffer(Object o, int estimatedSize) throws IOException {
       ExposedByteArrayOutputStream baos = new ExposedByteArrayOutputStream(estimatedSize);
       ObjectOutputStream oos = new ObjectOutputStream(baos);
-      objectToObjectStream(obj, oos);
+      objectToObjectStream(o, oos);
       oos.flush();
       oos.close();
       baos.close();
-      return baos.toByteArray();
+      byte[] b = baos.toByteArray();
+      return new ByteBuffer(b, 0, b.length);
    }
 
-   public Object objectFromByteBuffer(byte[] buf) throws IOException, ClassNotFoundException {
+   @Override
+   public Object objectFromByteBuffer(byte[] buf, int offset, int length) throws IOException, ClassNotFoundException {
+      byte[] newBytes = new byte[length];
+      System.arraycopy(buf, offset, newBytes, 0, length);
       return objectFromObjectStream(new ObjectInputStream(new ByteArrayInputStream(buf)));
    }
 



More information about the infinispan-commits mailing list