Index: jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/BinaryTest.java =================================================================== --- jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/BinaryTest.java (revision 0) +++ jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/BinaryTest.java (revision 0) @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.jcr2spi; + +import java.util.Random; +import java.io.ByteArrayInputStream; + +import javax.jcr.Node; +import javax.jcr.Session; +import javax.jcr.Property; +import javax.jcr.Binary; + +import org.apache.jackrabbit.test.AbstractJCRTest; + +/** + * BinaryTest... + */ +public class BinaryTest extends AbstractJCRTest { + + public void testStreamBinary() throws Exception { + byte[] data = new byte[10 * 1024 * 1024]; + new Random().nextBytes(data); + Node test = testRootNode.addNode("test"); + Property p = test.setProperty("prop", new ByteArrayInputStream(data)); + // check before save + checkBinary(p); + superuser.save(); + // check after save + checkBinary(p); + + // check from other session + Session s = getHelper().getReadOnlySession(); + try { + p = s.getNode(testRoot).getNode("test").getProperty("prop"); + checkBinary(p); + } finally { + s.logout(); + } + } + + protected void checkBinary(Property p) throws Exception { + for (int i = 0; i < 3; i++) { + Binary bin = p.getBinary(); + try { + //System.out.println(bin.getClass() + "@" + System.identityHashCode(bin)); + bin.read(new byte[1], 0); + } finally { + bin.dispose(); + } + } + } +} Property changes on: jackrabbit-jcr2spi\src\test\java\org\apache\jackrabbit\jcr2spi\BinaryTest.java ___________________________________________________________________ Added: svn:eol-style + native Index: jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/TestAll.java =================================================================== --- jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/TestAll.java (revision 802697) +++ jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/TestAll.java (working copy) @@ -47,6 +47,7 @@ suite.addTestSuite(AddNewPropertyTest.class); suite.addTestSuite(SingleValuedPropertyTest.class); suite.addTestSuite(MultiValuedPropertyTest.class); + suite.addTestSuite(BinaryTest.class); // change mixin types suite.addTestSuite(MixinModificationTest.class); Index: jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/AbstractQValue.java =================================================================== --- jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/AbstractQValue.java (revision 802697) +++ jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/AbstractQValue.java (working copy) @@ -25,6 +25,8 @@ import javax.jcr.PropertyType; import javax.jcr.RepositoryException; import javax.jcr.ValueFormatException; +import javax.jcr.Binary; + import java.util.Calendar; import java.util.TimeZone; import java.math.BigDecimal; @@ -351,6 +353,38 @@ } /** + * This implementation creates a binary instance that uses + * {@link #getStream()} and skipping on the given stream as its underlying + * mechanism to provide random access defined on {@link Binary}. + * + * @see QValue#getBinary() + */ + public Binary getBinary() throws RepositoryException { + return new Binary() { + public InputStream getStream() throws RepositoryException { + return AbstractQValue.this.getStream(); + } + + public int read(byte[] b, long position) throws IOException, RepositoryException { + InputStream in = getStream(); + try { + in.skip(position); + return in.read(b); + } finally { + in.close(); + } + } + + public long getSize() throws RepositoryException { + return getLength(); + } + + public void dispose() { + } + }; + } + + /** * @see QValue#discard() */ public void discard() { Index: jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/DefaultQValue.java =================================================================== --- jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/DefaultQValue.java (revision 802697) +++ jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/DefaultQValue.java (working copy) @@ -17,7 +17,6 @@ package org.apache.jackrabbit.spi.commons.value; import java.io.Serializable; -import java.io.IOException; import java.io.InputStream; import java.io.ByteArrayInputStream; import java.io.UnsupportedEncodingException; @@ -25,13 +24,11 @@ import java.net.URI; import java.util.Calendar; -import javax.jcr.Binary; import javax.jcr.RepositoryException; import org.apache.jackrabbit.spi.QValue; import org.apache.jackrabbit.spi.Name; import org.apache.jackrabbit.spi.Path; -import org.apache.jackrabbit.value.BinaryImpl; /** * QValue implementation for all valid PropertyTypes @@ -83,19 +80,6 @@ //-------------------------------------------------------------< QValue >--- /** - * @see QValue#getBinary() - */ - public Binary getBinary() throws RepositoryException { - try { - // convert via string - return new BinaryImpl(getString().getBytes( - AbstractQValueFactory.DEFAULT_ENCODING)); - } catch (IOException e) { - throw new RepositoryException(e); - } - } - - /** * @see QValue#getStream() */ public InputStream getStream() throws RepositoryException { Index: jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/QValueFactoryImpl.java =================================================================== --- jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/QValueFactoryImpl.java (revision 802697) +++ jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/QValueFactoryImpl.java (working copy) @@ -24,7 +24,6 @@ import javax.jcr.PropertyType; import javax.jcr.RepositoryException; -import javax.jcr.Binary; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; @@ -36,7 +35,6 @@ import java.io.ObjectOutputStream; import java.io.OutputStream; import java.io.Serializable; -import java.io.RandomAccessFile; import java.util.Arrays; /** @@ -91,7 +89,7 @@ * state, i.e. the getStream() method always returns a fresh * InputStream instance. */ - private static class BinaryQValue extends AbstractQValue implements Binary, Serializable { + private static class BinaryQValue extends AbstractQValue implements Serializable { /** * A dummy value for calling the constructor of AbstractQValue @@ -300,13 +298,6 @@ } /** - * @see QValue#getBinary() - */ - public Binary getBinary() throws RepositoryException { - return this; - } - - /** * Frees temporarily allocated resources such as temporary file, buffer, etc. * If this BinaryQValue is backed by a persistent resource * calling this method will have no effect. @@ -327,10 +318,6 @@ } } - public void dispose() { - discard(); - } - //-----------------------------------------------< java.lang.Object >--- /** * Returns a string representation of this BinaryQValue @@ -377,35 +364,6 @@ return 0; } - //-----------------------------< 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) Index: jackrabbit-spi2dav/src/main/java/org/apache/jackrabbit/spi2davex/QValueFactoryImpl.java =================================================================== --- jackrabbit-spi2dav/src/main/java/org/apache/jackrabbit/spi2davex/QValueFactoryImpl.java (revision 802698) +++ jackrabbit-spi2dav/src/main/java/org/apache/jackrabbit/spi2davex/QValueFactoryImpl.java (working copy) @@ -36,7 +36,6 @@ import javax.jcr.PropertyType; import javax.jcr.RepositoryException; import javax.jcr.Value; -import javax.jcr.Binary; import javax.jcr.ValueFactory; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.ParserConfigurationException; @@ -299,37 +298,6 @@ } /** - * @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(); - try { - in.skip(position); - return in.read(b); - } finally { - in.close(); - } - } - - public long getSize() throws RepositoryException { - return getLength(); - } - - public void dispose() { - } - - }; - } - - /** * Frees temporarily allocated resources such as temporary file, buffer, etc. * If this BinaryQValue is backed by a persistent resource * calling this method will have no effect.