Search in sources :

Example 61 with Binary

use of javax.jcr.Binary in project jackrabbit by apache.

the class BinaryPropertyTest method testGetLengthJcr2.

/**
 * Tests the Binary.getSize() method.
 */
public void testGetLengthJcr2() throws RepositoryException {
    Value val = PropertyUtil.getValue(prop);
    Binary binary = val.getBinary();
    long length;
    try {
        length = binary.getSize();
    } finally {
        binary.dispose();
    }
    long bytes = PropertyUtil.countBytes(val);
    if (bytes != -1) {
        assertEquals("Binary.getSize() returns wrong number of bytes.", bytes, length);
    }
}
Also used : Value(javax.jcr.Value) Binary(javax.jcr.Binary)

Example 62 with Binary

use of javax.jcr.Binary in project jackrabbit by apache.

the class BinaryPropertyTest method testValueJcr2.

/**
 * Tests that Value.getStream() delivers the same as Value.getBinary.getStream().
 * We check this by reading each byte of the two streams and assuring that
 * they are equal.
 */
public void testValueJcr2() throws IOException, RepositoryException {
    Value val = PropertyUtil.getValue(prop);
    InputStream in = val.getStream();
    Binary bin = val.getBinary();
    try {
        InputStream in2 = bin.getStream();
        try {
            int b = in.read();
            while (b != -1) {
                int b2 = in2.read();
                assertEquals("Value.getStream() and Value.getBinary().getStream() " + "return different values.", b, b2);
                b = in.read();
            }
            assertEquals("Value.getStream() and Value.getBinary().getStream() " + "return different values.", -1, in2.read());
        } finally {
            try {
                in.close();
            } catch (IOException ignore) {
            }
            try {
                in2.close();
            } catch (IOException ignore) {
            }
        }
    } finally {
        bin.dispose();
    }
}
Also used : InputStream(java.io.InputStream) Value(javax.jcr.Value) Binary(javax.jcr.Binary) IOException(java.io.IOException)

Example 63 with Binary

use of javax.jcr.Binary in project jackrabbit by apache.

the class ValueFactoryTest method testInputStream.

/**
 * Tests whether a passed <code>InputStream</code> is closed
 * by the implementation.
 *
 * @throws RepositoryException
 */
public void testInputStream() throws RepositoryException {
    InputStreamWrapper in = new InputStreamWrapper(new ByteArrayInputStream(binaryValue));
    valueFactory.createValue(in);
    assertTrue("ValueFactory.createValue(InputStream) is expected to close the passed input stream", in.isClosed());
    in = new InputStreamWrapper(new ByteArrayInputStream(binaryValue));
    Binary bin = valueFactory.createBinary(in);
    assertTrue("ValueFactory.createBinary(InputStream) is expected to close the passed input stream", in.isClosed());
    bin.dispose();
}
Also used : InputStreamWrapper(org.apache.jackrabbit.test.api.util.InputStreamWrapper) ByteArrayInputStream(java.io.ByteArrayInputStream) Binary(javax.jcr.Binary)

Example 64 with Binary

use of javax.jcr.Binary in project jackrabbit by apache.

the class BinaryTest method testRevertSettingExistingBinary.

public void testRevertSettingExistingBinary() throws Exception {
    Node test = testRootNode.addNode("test");
    Binary b = superuser.getValueFactory().createBinary(generateValue());
    Property p = test.setProperty("prop", b);
    QValue qv1 = getQValue(p);
    superuser.save();
    Binary b2 = superuser.getValueFactory().createBinary(generateValue());
    test.setProperty("prop", b2);
    QValue qv2 = getQValue(p);
    assertFalse(qv1.equals(qv2));
    superuser.refresh(false);
    assertEquals(qv1, getQValue(p));
    assertSame(qv1, getQValue(p));
    assertFalse(qv2.equals(getQValue(p)));
}
Also used : QValue(org.apache.jackrabbit.spi.QValue) Node(javax.jcr.Node) Binary(javax.jcr.Binary) Property(javax.jcr.Property)

Example 65 with Binary

use of javax.jcr.Binary in project jackrabbit by apache.

the class AbstractQValue method getBinary.

/**
 * 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 {
                long skip = position;
                while (skip > 0) {
                    long skipped = in.skip(skip);
                    if (skipped <= 0) {
                        return -1;
                    }
                    skip -= skipped;
                }
                return in.read(b);
            } finally {
                in.close();
            }
        }

        public long getSize() throws RepositoryException {
            return getLength();
        }

        public void dispose() {
        }
    };
}
Also used : InputStream(java.io.InputStream) Binary(javax.jcr.Binary)

Aggregations

Binary (javax.jcr.Binary)70 Node (javax.jcr.Node)37 ByteArrayInputStream (java.io.ByteArrayInputStream)32 InputStream (java.io.InputStream)29 Value (javax.jcr.Value)18 Session (javax.jcr.Session)15 Test (org.junit.Test)13 RepositoryException (javax.jcr.RepositoryException)12 BigDecimal (java.math.BigDecimal)10 ReferenceBinary (org.apache.jackrabbit.api.ReferenceBinary)8 Property (javax.jcr.Property)7 ValueFactory (javax.jcr.ValueFactory)7 SimpleReferenceBinary (org.apache.jackrabbit.commons.jackrabbit.SimpleReferenceBinary)7 RandomInputStream (org.apache.jackrabbit.core.data.RandomInputStream)7 IOException (java.io.IOException)6 NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)5 Calendar (java.util.Calendar)4 JackrabbitNode (org.apache.jackrabbit.api.JackrabbitNode)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Date (java.util.Date)3