jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r290458 - in /incubator/jackrabbit/trunk/contrib/jcr-rmi/src: java/org/apache/jackrabbit/rmi/value/BinaryValue.java test/org/apache/jackrabbit/test/rmi/value/ test/org/apache/jackrabbit/test/rmi/value/SerialValueFactoryTest.java
Date Tue, 20 Sep 2005 14:36:07 GMT
Author: jukka
Date: Tue Sep 20 07:35:59 2005
New Revision: 290458

URL: http://svn.apache.org/viewcvs?rev=290458&view=rev
Log:
JCR-199: Use a sequence of smaller byte arrays instead of one large
byte array to avoid memory problems when serializing binary values.

Added:
    incubator/jackrabbit/trunk/contrib/jcr-rmi/src/test/org/apache/jackrabbit/test/rmi/value/
    incubator/jackrabbit/trunk/contrib/jcr-rmi/src/test/org/apache/jackrabbit/test/rmi/value/SerialValueFactoryTest.java
  (with props)
Modified:
    incubator/jackrabbit/trunk/contrib/jcr-rmi/src/java/org/apache/jackrabbit/rmi/value/BinaryValue.java

Modified: incubator/jackrabbit/trunk/contrib/jcr-rmi/src/java/org/apache/jackrabbit/rmi/value/BinaryValue.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/jcr-rmi/src/java/org/apache/jackrabbit/rmi/value/BinaryValue.java?rev=290458&r1=290457&r2=290458&view=diff
==============================================================================
--- incubator/jackrabbit/trunk/contrib/jcr-rmi/src/java/org/apache/jackrabbit/rmi/value/BinaryValue.java
(original)
+++ incubator/jackrabbit/trunk/contrib/jcr-rmi/src/java/org/apache/jackrabbit/rmi/value/BinaryValue.java
Tue Sep 20 07:35:59 2005
@@ -17,11 +17,14 @@
 package org.apache.jackrabbit.rmi.value;
 
 import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
+import java.io.OutputStream;
 import java.io.Serializable;
 import java.io.UnsupportedEncodingException;
 import java.util.Calendar;
@@ -178,50 +181,70 @@
 
     /**
      * Writes the contents of the underlying stream to the
-     * <code>ObjectOutputStream</code> by first copying to an internal byte
-     * array.
+     * <code>ObjectOutputStream</code>.
      *
      * @param out The <code>ObjectOutputStream</code> to where the binary
      *      data is copied.
-     *
      * @throws IOException If an error occurrs writing the binary data.
-     * @throws OutOfMemoryError If not enough memory is available to store the
-     *      binary data in the internal byte array.
      */
     private void writeObject(ObjectOutputStream out)
             throws IOException, OutOfMemoryError {
-        // read the input into a byte array - limited by memory available !!
-        ByteArrayOutputStream bos =
-            new ByteArrayOutputStream(stream.available());
-        byte[] buf = new byte[2048];
-        int rd = 0;
-        while ((rd = stream.read(buf)) >= 0) {
-            bos.write(buf, 0, rd);
+        byte[] buffer = new byte[4096];
+        int bytes = 0;
+        while ((bytes = stream.read(buffer)) >= 0) {
+            // Write a segment of the input stream
+            out.writeInt(bytes);
+            out.write(buffer, 0, bytes);
         }
-
-        // stream the data to the object output
-        out.writeInt(bos.size());
-        out.write(bos.toByteArray());
+        // Write the end of stream marker
+        out.writeInt(0);
     }
 
     /**
-     * Reads the binary data from the <code>ObjectInputStream</code> into an
-     * internal byte array, which is then provided through a
-     * <code>ByteArrayInputStream</code>.
+     * Reads the binary data from the <code>ObjectInputStream</code> into
+     * a temporary file that is used to back up the binary stream contents
+     * of the constructed value instance. The temporary file gets deleted
+     * when the binary stream is closed or garbage collected.
      *
      * @param in The <code>ObjectInputStream</code> from where to get the
      *      binary data.
-     *
      * @throws IOException If an error occurrs reading the binary data.
-     * @throws OutOfMemoryError If not enouhg memory is available to store the
-     *      binary data in the internal byte array.
      */
     private void readObject(ObjectInputStream in)
             throws IOException, OutOfMemoryError {
-        int size = in.readInt();
-        byte[] buf = new byte[size];
-        in.readFully(buf);
-        stream = new ByteArrayInputStream(buf);
+        final File file = File.createTempFile("jcr-value", "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();
+
+        stream = new FileInputStream(file) {
+
+            private boolean closed = false;
+
+            public void close() throws IOException {
+                super.close();
+                closed = true;
+                file.delete();
+            }
+
+            protected void finalize() throws IOException {
+                try {
+                    if (!closed) {
+                        file.delete();
+                    }
+                } finally {
+                    super.finalize();
+                }
+            }
+        };
     }
 
 }

Added: incubator/jackrabbit/trunk/contrib/jcr-rmi/src/test/org/apache/jackrabbit/test/rmi/value/SerialValueFactoryTest.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/jcr-rmi/src/test/org/apache/jackrabbit/test/rmi/value/SerialValueFactoryTest.java?rev=290458&view=auto
==============================================================================
--- incubator/jackrabbit/trunk/contrib/jcr-rmi/src/test/org/apache/jackrabbit/test/rmi/value/SerialValueFactoryTest.java
(added)
+++ incubator/jackrabbit/trunk/contrib/jcr-rmi/src/test/org/apache/jackrabbit/test/rmi/value/SerialValueFactoryTest.java
Tue Sep 20 07:35:59 2005
@@ -0,0 +1,71 @@
+/*
+ * Copyright 2004-2005 The Apache Software Foundation or its licensors,
+ *                     as applicable.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.test.rmi.value;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+
+import javax.jcr.Value;
+import javax.jcr.ValueFactory;
+
+import org.apache.jackrabbit.rmi.value.SerialValueFactory;
+
+import junit.framework.TestCase;
+
+/**
+ * TODO
+ */
+public class SerialValueFactoryTest extends TestCase {
+
+    private static final String TEST_STRING = "test string";
+
+    public void testStringValueSerialization() throws Exception {
+        ValueFactory factory = SerialValueFactory.getInstance();
+        Value value = factory.createValue(TEST_STRING);
+
+        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+        ObjectOutputStream output = new ObjectOutputStream(buffer);
+        output.writeObject(value);
+        output.close();
+
+        ObjectInputStream input = new ObjectInputStream(
+                new ByteArrayInputStream(buffer.toByteArray()));
+        Value copy = (Value) input.readObject();
+
+        assertEquals(copy.getString(), TEST_STRING);
+    }
+
+    public void testBinaryValueSerialization() throws Exception {
+        ValueFactory factory = SerialValueFactory.getInstance();
+        Value value = factory.createValue(
+                new ByteArrayInputStream(TEST_STRING.getBytes()));
+
+        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+        ObjectOutputStream output = new ObjectOutputStream(buffer);
+        output.writeObject(value);
+        output.close();
+
+        ObjectInputStream input = new ObjectInputStream(
+                new ByteArrayInputStream(buffer.toByteArray()));
+        Value copy = (Value) input.readObject();
+
+        assertEquals(copy.getString(), TEST_STRING);
+    }
+
+}

Propchange: incubator/jackrabbit/trunk/contrib/jcr-rmi/src/test/org/apache/jackrabbit/test/rmi/value/SerialValueFactoryTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message