You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by st...@apache.org on 2009/05/28 18:12:55 UTC

svn commit: r779642 - in /jackrabbit/trunk: jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/ jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/value/ jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/ ...

Author: stefan
Date: Thu May 28 16:12:54 2009
New Revision: 779642

URL: http://svn.apache.org/viewvc?rev=779642&view=rev
Log:
JCR-2056: Binary interfaces


Modified:
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/BLOBFileValue.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/BLOBInTempFile.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/BLOBValue.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/InternalValue.java
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/value/BinaryImpl.java
    jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/QValueFactoryImpl.java
    jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/ValueFactoryQImpl.java
    jackrabbit/trunk/jackrabbit-spi/src/main/java/org/apache/jackrabbit/spi/QValue.java
    jackrabbit/trunk/jackrabbit-spi/src/main/java/org/apache/jackrabbit/spi/QValueFactory.java
    jackrabbit/trunk/jackrabbit-spi2dav/src/main/java/org/apache/jackrabbit/spi2davex/QValueFactoryImpl.java

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/BLOBFileValue.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/BLOBFileValue.java?rev=779642&r1=779641&r2=779642&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/BLOBFileValue.java (original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/BLOBFileValue.java Thu May 28 16:12:54 2009
@@ -23,6 +23,7 @@
 import java.io.UnsupportedEncodingException;
 
 import javax.jcr.RepositoryException;
+import javax.jcr.Binary;
 
 import org.apache.commons.io.IOUtils;
 import org.apache.jackrabbit.core.data.DataIdentifier;
@@ -37,14 +38,7 @@
  * This interface is for Jackrabbit-internal use only. Applications should
  * use <code>javax.jcr.ValueFactory</code> to create binary values.
  */
-public abstract class BLOBFileValue {
-
-    /**
-     * Returns an InputStream representation of this value.
-     *
-     * @return An InputStream representation of this value.
-     */
-    public abstract InputStream getStream() throws RepositoryException;
+public abstract class BLOBFileValue implements Binary {
 
     /**
      * Returns a String representation of this value.
@@ -163,4 +157,25 @@
         return null;
     }
 
+    //-----------------------------------------------------< javax.jcr.Binary >
+    /**
+     * {@inheritDoc}
+     */
+    public abstract InputStream getStream() throws RepositoryException;
+
+    /**
+     * {@inheritDoc}
+     */
+    public int read(byte[] b, long position) throws IOException, RepositoryException {
+        InputStream in = getStream();
+        in.skip(position);
+        return in.read(b);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public long getSize() throws RepositoryException {
+        return getLength();
+    }
 }

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/BLOBInTempFile.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/BLOBInTempFile.java?rev=779642&r1=779641&r2=779642&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/BLOBInTempFile.java (original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/BLOBInTempFile.java Thu May 28 16:12:54 2009
@@ -26,6 +26,7 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.io.RandomAccessFile;
 
 import javax.jcr.RepositoryException;
 
@@ -187,4 +188,12 @@
         return false;
     }
 
+    /**
+     * {@inheritDoc}
+     */
+    public int read(byte[] b, long position) throws IOException, RepositoryException {
+        RandomAccessFile raf = new RandomAccessFile(file, "r");
+        raf.seek(position);
+        return raf.read(b);
+    }
 }

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/BLOBValue.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/BLOBValue.java?rev=779642&r1=779641&r2=779642&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/BLOBValue.java (original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/BLOBValue.java Thu May 28 16:12:54 2009
@@ -30,6 +30,7 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.io.RandomAccessFile;
 import java.util.Arrays;
 
 import javax.jcr.RepositoryException;
@@ -345,8 +346,14 @@
     /**
      * {@inheritDoc}
      */
-    public InputStream getStream()
-            throws IllegalStateException, RepositoryException {
+    public boolean isSmall() {
+        return false;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public InputStream getStream() throws RepositoryException {
         // always return a 'fresh' stream
         if (file != null) {
             // this instance is backed by a 'real' file
@@ -372,8 +379,34 @@
     /**
      * {@inheritDoc}
      */
-    public boolean isSmall() {
-        return false;
+    public int read(byte[] b, long position) throws IOException, RepositoryException {
+        if (file != null) {
+            // this instance is backed by a temp file
+            RandomAccessFile raf = new RandomAccessFile(file, "r");
+            raf.seek(position);
+            return raf.read(b);
+        } else if (fsResource != null) {
+            // this instance is backed by a resource in the virtual file system
+            InputStream in;
+            try {
+                in = fsResource.getInputStream();
+            } catch (FileSystemException fse) {
+                throw new RepositoryException(fsResource.getPath()
+                        + ": the specified resource does not exist", fse);
+            }
+            in.skip(position);
+            return in.read(b);
+        } else {
+            // this instance is backed by an in-memory buffer
+            int length = Math.min(b.length, buffer.length - (int) position);
+            if (length > 0) {
+                System.arraycopy(buffer, (int) position, b, 0, length);
+                return length;
+            } else {
+                return -1;
+            }
+        }
     }
 
+
 }

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/InternalValue.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/InternalValue.java?rev=779642&r1=779641&r2=779642&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/InternalValue.java (original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/InternalValue.java Thu May 28 16:12:54 2009
@@ -42,6 +42,7 @@
 import javax.jcr.ValueFormatException;
 import javax.jcr.Session;
 import javax.jcr.ValueFactory;
+import javax.jcr.Binary;
 
 import java.io.ByteArrayInputStream;
 import java.io.File;
@@ -759,4 +760,20 @@
             }
         }
     }
+
+    /**
+     * @see org.apache.jackrabbit.spi.QValue#getBinary()
+     */
+    public Binary getBinary() throws RepositoryException {
+        if (type == PropertyType.BINARY) {
+            return (BLOBFileValue) val;
+        } else {
+            try {
+                // convert via string
+                return new BLOBValue(getString().getBytes(InternalValueFactory.DEFAULT_ENCODING));
+            } catch (UnsupportedEncodingException e) {
+                throw new RepositoryException(InternalValueFactory.DEFAULT_ENCODING + " is not supported encoding on this platform", e);
+            }
+        }
+    }
 }

Modified: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/value/BinaryImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/value/BinaryImpl.java?rev=779642&r1=779641&r2=779642&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/value/BinaryImpl.java (original)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/value/BinaryImpl.java Thu May 28 16:12:54 2009
@@ -149,9 +149,13 @@
             return raf.read(b);
         } else {
             // this instance is backed by an in-memory buffer
-            int length = buffer.length - (int) position;
-            System.arraycopy(buffer, (int) position, b, 0, length);
-            return length;
+            int length = Math.min(b.length, buffer.length - (int) position);
+            if (length > 0) {
+                System.arraycopy(buffer, (int) position, b, 0, length);
+                return length;
+            } else {
+                return -1;
+            }
         }
     }
 

Modified: jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/QValueFactoryImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/QValueFactoryImpl.java?rev=779642&r1=779641&r2=779642&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/QValueFactoryImpl.java (original)
+++ jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/QValueFactoryImpl.java Thu May 28 16:12:54 2009
@@ -26,6 +26,7 @@
 import javax.jcr.PropertyType;
 import javax.jcr.RepositoryException;
 import javax.jcr.ValueFormatException;
+import javax.jcr.Binary;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
@@ -39,6 +40,7 @@
 import java.io.OutputStream;
 import java.io.Serializable;
 import java.io.UnsupportedEncodingException;
+import java.io.RandomAccessFile;
 import java.util.Arrays;
 import java.util.Calendar;
 import java.math.BigDecimal;
@@ -276,6 +278,28 @@
         }
 
         /**
+         * @see QValue#getBinary()
+         */
+        public Binary getBinary() throws RepositoryException {
+            // TODO FIXME consolidate Binary implementations
+            return new Binary() {
+                public InputStream getStream() throws RepositoryException {
+                    return QValueImpl.this.getStream();
+                }
+
+                public int read(byte[] b, long position) throws IOException, RepositoryException {
+                    InputStream in = getStream();
+                    in.skip(position);
+                    return in.read(b);
+                }
+
+                public long getSize() throws RepositoryException {
+                    return getLength();
+                }
+            };
+        }
+
+        /**
          * @see QValue#getStream()
          */
         public InputStream getStream() throws RepositoryException {
@@ -343,7 +367,7 @@
      * state, i.e. the <code>getStream()</code> method always returns a fresh
      * <code>InputStream</code> instance.
      */
-    private static class BinaryQValue implements QValue, Serializable {
+    private static class BinaryQValue implements QValue, Binary, Serializable {
         /**
          * empty array
          */
@@ -608,7 +632,7 @@
          * @see QValue#getBoolean()
          */
         public boolean getBoolean() throws RepositoryException {
-            return Boolean.valueOf(getString()).booleanValue();
+            return Boolean.valueOf(getString());
         }
 
         /**
@@ -641,6 +665,13 @@
         }
 
         /**
+         * @see QValue#getBinary()
+         */
+        public Binary getBinary() throws RepositoryException {
+            return this;
+        }
+
+        /**
          * Frees temporarily allocated resources such as temporary file, buffer, etc.
          * If this <code>BinaryQValue</code> is backed by a persistent resource
          * calling this method will have no effect.
@@ -745,6 +776,35 @@
             }
         }
 
+        //-----------------------------< javx.jcr.Binary >----------------------
+        /**
+         * {@inheritDoc}
+         */
+        public int read(byte[] b, long position) throws IOException, RepositoryException {
+            if (file != null) {
+                // this instance is backed by a temp file
+                RandomAccessFile raf = new RandomAccessFile(file, "r");
+                raf.seek(position);
+                return raf.read(b);
+            } else {
+                // this instance is backed by an in-memory buffer
+                int length = Math.min(b.length, buffer.length - (int) position);
+                if (length > 0) {
+                    System.arraycopy(buffer, (int) position, b, 0, length);
+                    return length;
+                } else {
+                    return -1;
+                }
+            }
+        }
+
+        /**
+         * {@inheritDoc}
+         */
+        public long getSize() throws RepositoryException {
+            return getLength();
+        }
+
         //-----------------------------< Serializable >-------------------------
 
         private void writeObject(ObjectOutputStream out)

Modified: jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/ValueFactoryQImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/ValueFactoryQImpl.java?rev=779642&r1=779641&r2=779642&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/ValueFactoryQImpl.java (original)
+++ jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/ValueFactoryQImpl.java Thu May 28 16:12:54 2009
@@ -200,13 +200,24 @@
     }
 
     public Binary createBinary(InputStream stream) throws RepositoryException {
-        // TODO
-        throw new RuntimeException("Not implemented yet, see JCR-2056");
+        // TODO review/optimize/refactor
+        try {
+            QValue qvalue = qfactory.create(stream);
+            return qvalue.getBinary();
+        } catch (IOException ex) {
+            throw new RuntimeException(ex);
+        } catch (RepositoryException ex) {
+            throw new RuntimeException(ex);
+        }
     }
 
     public Value createValue(Binary value) {
-        // TODO
-        throw new RuntimeException("Not implemented yet, see JCR-2056");
+        // TODO review/optimize/refactor
+        try {
+            return createValue(value.getStream());
+        } catch (RepositoryException ex) {
+            throw new RuntimeException(ex);
+        }
     }
 
     public Value createValue(BigDecimal value) {

Modified: jackrabbit/trunk/jackrabbit-spi/src/main/java/org/apache/jackrabbit/spi/QValue.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-spi/src/main/java/org/apache/jackrabbit/spi/QValue.java?rev=779642&r1=779641&r2=779642&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-spi/src/main/java/org/apache/jackrabbit/spi/QValue.java (original)
+++ jackrabbit/trunk/jackrabbit-spi/src/main/java/org/apache/jackrabbit/spi/QValue.java Thu May 28 16:12:54 2009
@@ -24,6 +24,7 @@
 import javax.jcr.Property;
 import javax.jcr.PropertyType;
 import javax.jcr.RepositoryException;
+import javax.jcr.Binary;
 
 /**
  * <code>QValue</code> is the qualified SPI representation of a
@@ -89,6 +90,15 @@
     public InputStream getStream() throws RepositoryException;
 
     /**
+     * Returns a <code>Binary</code> representation of this <code>QValue</code>
+     * object.
+     *
+     * @return A <code>Binary</code> representation of this value.
+     * @throws RepositoryException
+     */
+    public Binary getBinary() throws RepositoryException;
+
+    /**
      * Returns a <code>Calendar</code> representation of this value.
      *
      * @return A <code>Calendar</code> representation of this value.

Modified: jackrabbit/trunk/jackrabbit-spi/src/main/java/org/apache/jackrabbit/spi/QValueFactory.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-spi/src/main/java/org/apache/jackrabbit/spi/QValueFactory.java?rev=779642&r1=779641&r2=779642&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-spi/src/main/java/org/apache/jackrabbit/spi/QValueFactory.java (original)
+++ jackrabbit/trunk/jackrabbit-spi/src/main/java/org/apache/jackrabbit/spi/QValueFactory.java Thu May 28 16:12:54 2009
@@ -139,7 +139,7 @@
      * Create a new <code>QValue</code> with type {@link javax.jcr.PropertyType#BINARY}.
      *
      * @param value
-     * @return a new binarly <code>QValue</code>.
+     * @return a new binary <code>QValue</code>.
      * @throws IOException
      */
     public QValue create(File value) throws RepositoryException, IOException;

Modified: jackrabbit/trunk/jackrabbit-spi2dav/src/main/java/org/apache/jackrabbit/spi2davex/QValueFactoryImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-spi2dav/src/main/java/org/apache/jackrabbit/spi2davex/QValueFactoryImpl.java?rev=779642&r1=779641&r2=779642&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-spi2dav/src/main/java/org/apache/jackrabbit/spi2davex/QValueFactoryImpl.java (original)
+++ jackrabbit/trunk/jackrabbit-spi2dav/src/main/java/org/apache/jackrabbit/spi2davex/QValueFactoryImpl.java Thu May 28 16:12:54 2009
@@ -40,6 +40,7 @@
 import javax.jcr.RepositoryException;
 import javax.jcr.Value;
 import javax.jcr.ValueFormatException;
+import javax.jcr.Binary;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.ParserConfigurationException;
 import java.io.ByteArrayInputStream;
@@ -399,6 +400,29 @@
                 }
             }
         }
+
+        /**
+         * @see QValue#getBinary()
+         */
+        public Binary getBinary() throws RepositoryException {
+            // TODO FIXME consolidate Binary implementations
+            return new Binary() {
+                public InputStream getStream() throws RepositoryException {
+                    return QValueImpl.this.getStream();
+                }
+
+                public int read(byte[] b, long position) throws IOException, RepositoryException {
+                    InputStream in = getStream();
+                    in.skip(position);
+                    return in.read(b);
+                }
+
+                public long getSize() throws RepositoryException {
+                    return getLength();
+                }
+            };
+        }
+
     }
 
     //--------------------------------------------------------< Inner Class >---
@@ -791,6 +815,29 @@
         }
 
         /**
+         * @see QValue#getBinary()
+         */
+        public Binary getBinary() throws RepositoryException {
+            // TODO FIXME consolidate Binary implementations
+            // TODO optimize
+            return new Binary() {
+                public InputStream getStream() throws RepositoryException {
+                    return BinaryQValue.this.getStream();
+                }
+
+                public int read(byte[] b, long position) throws IOException, RepositoryException {
+                    InputStream in = getStream();
+                    in.skip(position);
+                    return in.read(b);
+                }
+
+                public long getSize() throws RepositoryException {
+                    return getLength();
+                }
+            };
+        }
+
+        /**
          * Frees temporarily allocated resources such as temporary file, buffer, etc.
          * If this <code>BinaryQValue</code> is backed by a persistent resource
          * calling this method will have no effect.