jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mreut...@apache.org
Subject svn commit: r512786 - in /jackrabbit/trunk/contrib/spi/commons/src: main/java/org/apache/jackrabbit/value/QValueFactoryImpl.java test/java/org/apache/jackrabbit/value/QValueTest.java
Date Wed, 28 Feb 2007 14:56:09 GMT
Author: mreutegg
Date: Wed Feb 28 06:56:09 2007
New Revision: 512786

URL: http://svn.apache.org/viewvc?view=rev&rev=512786
Log:
Implement Serializable for QValue classes. Added test case for binary QValue.

Modified:
    jackrabbit/trunk/contrib/spi/commons/src/main/java/org/apache/jackrabbit/value/QValueFactoryImpl.java
    jackrabbit/trunk/contrib/spi/commons/src/test/java/org/apache/jackrabbit/value/QValueTest.java

Modified: jackrabbit/trunk/contrib/spi/commons/src/main/java/org/apache/jackrabbit/value/QValueFactoryImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/spi/commons/src/main/java/org/apache/jackrabbit/value/QValueFactoryImpl.java?view=diff&rev=512786&r1=512785&r2=512786
==============================================================================
--- jackrabbit/trunk/contrib/spi/commons/src/main/java/org/apache/jackrabbit/value/QValueFactoryImpl.java
(original)
+++ jackrabbit/trunk/contrib/spi/commons/src/main/java/org/apache/jackrabbit/value/QValueFactoryImpl.java
Wed Feb 28 06:56:09 2007
@@ -38,6 +38,9 @@
 import java.io.ByteArrayOutputStream;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
+import java.io.Serializable;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
 
 /**
  * <code>QValueFactoryImpl</code>...
@@ -150,7 +153,7 @@
      * except for BINARY.
      * @see QValueFactoryImpl.BinaryQValue
      */
-    private static class QValueImpl implements QValue {
+    private static class QValueImpl implements QValue, Serializable {
         /**
          * the default encoding
          */
@@ -294,6 +297,9 @@
         public int hashCode() {
             return val.hashCode();
         }
+
+        //---------------------------------------------------< Serializable >---
+
     }
 
 
@@ -304,7 +310,7 @@
      * state, i.e. the <code>getStream()</code> method always returns a fresh
      * <code>InputStream</code> instance.
      */
-    private static class BinaryQValue implements QValue {
+    private static class BinaryQValue implements QValue, Serializable {
         /**
          * empty array
          */
@@ -318,14 +324,14 @@
         /**
          * underlying file
          */
-        private final File file;
+        private transient File file;
 
         /**
          * flag indicating if this instance represents a <i>temporary</i> value
          * whose dynamically allocated resources can be explicitly freed on
          * {@link #discard()}.
          */
-        private final boolean temp;
+        private transient boolean temp;
 
         /**
          * Buffer for small-sized data
@@ -335,7 +341,7 @@
         /**
          * Converted text
          */
-        private String text = null;
+        private transient String text = null;
 
         /**
          * Creates a new <code>BinaryQValue</code> instance from an
@@ -640,6 +646,55 @@
                 } catch (IOException ignore) {
                 }
             }
+        }
+
+        //-----------------------------< Serializable >-------------------------
+
+        private void writeObject(ObjectOutputStream out)
+                throws IOException {
+            out.defaultWriteObject();
+            // write hasFile marker
+            out.writeBoolean(file != null);
+            // then write file if necessary
+            if (file != null) {
+                byte[] buffer = new byte[4096];
+                int bytes;
+                InputStream stream = new FileInputStream(file);
+                while ((bytes = stream.read(buffer)) >= 0) {
+                    // Write a segment of the input stream
+                    if (bytes > 0) {
+                        // just to ensure that no 0 is written
+                        out.writeInt(bytes);
+                        out.write(buffer, 0, bytes);
+                    }
+                }
+                // Write the end of stream marker
+                out.writeInt(0);
+                // close stream
+                stream.close();
+            }
+        }
+
+        private void readObject(ObjectInputStream in)
+                throws IOException, ClassNotFoundException {
+            in.defaultReadObject();
+            boolean hasFile = in.readBoolean();
+            if (hasFile) {
+                file = File.createTempFile("binary-qvalue", "bin");
+
+                OutputStream out = new FileOutputStream(file);
+                byte[] buffer = new byte[4096];
+                for (int bytes = in.readInt(); bytes > 0; bytes = in.readInt()) {
+                    if (buffer.length < bytes) {
+                        buffer = new byte[bytes];
+                    }
+                    in.readFully(buffer, 0, bytes);
+                    out.write(buffer, 0, bytes);
+                }
+                out.close();
+            }
+            // deserialized value is always temp
+            temp = true;
         }
     }
 }

Modified: jackrabbit/trunk/contrib/spi/commons/src/test/java/org/apache/jackrabbit/value/QValueTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/spi/commons/src/test/java/org/apache/jackrabbit/value/QValueTest.java?view=diff&rev=512786&r1=512785&r2=512786
==============================================================================
--- jackrabbit/trunk/contrib/spi/commons/src/test/java/org/apache/jackrabbit/value/QValueTest.java
(original)
+++ jackrabbit/trunk/contrib/spi/commons/src/test/java/org/apache/jackrabbit/value/QValueTest.java
Wed Feb 28 06:56:09 2007
@@ -21,9 +21,18 @@
 import javax.jcr.PropertyType;
 import javax.jcr.RepositoryException;
 import java.util.Calendar;
+import java.util.Arrays;
 import java.io.InputStream;
 import java.io.IOException;
 import java.io.File;
+import java.io.FileOutputStream;
+import java.io.OutputStream;
+import java.io.FileInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.ObjectOutputStream;
+import java.io.ByteArrayInputStream;
+import java.io.ObjectInputStream;
+import java.io.BufferedInputStream;
 
 import org.apache.jackrabbit.util.ISO8601;
 import org.apache.jackrabbit.uuid.UUID;
@@ -190,5 +199,52 @@
     public void testBinaryValueType() throws IOException {
         QValue v = factory.create(new byte[] {'a', 'b', 'c'});
         assertTrue(v.getType() == PropertyType.BINARY);
+    }
+
+    public void testBinarySerializable() throws Exception {
+        runBinarySerializableTest(1); // 1k
+        runBinarySerializableTest(10); // 10k
+        runBinarySerializableTest(100); // 100k
+        runBinarySerializableTest(1000); // 1M
+    }
+
+    /**
+     * Runs binary serializable test using a stream with a size of kBytes.
+     * @param size in kBytes.
+     */
+    private void runBinarySerializableTest(int size) throws Exception {
+        File tmp = File.createTempFile("test", "bin");
+        OutputStream out = new FileOutputStream(tmp);
+        byte[] stuff = new byte[1024];
+        Arrays.fill(stuff, (byte) 7);
+        for (int i = 0; i < size; i++) {
+            out.write(stuff);
+        }
+        out.close();
+        InputStream in = new FileInputStream(tmp);
+        QValue v = factory.create(in);
+        in.close();
+        tmp.delete();
+        ByteArrayOutputStream bout = new ByteArrayOutputStream();
+        ObjectOutputStream oout = new ObjectOutputStream(bout);
+        oout.writeObject(v);
+        oout.close();
+        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
+        ObjectInputStream oin = new ObjectInputStream(bin);
+        QValue serValue = (QValue) oin.readObject();
+        try {
+            InputStream in1 = new BufferedInputStream(v.getStream());
+            InputStream in2 = new BufferedInputStream(serValue.getStream());
+            int i;
+            while ((i = in1.read()) > -1) {
+                assertEquals(i, in2.read());
+            }
+            assertEquals(in2.read(), -1);
+            in1.close();
+            in2.close();
+        } finally {
+            v.discard();
+            serValue.discard();
+        }
     }
 }



Mime
View raw message