Search in sources :

Example 1 with Binary

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

the class AbstractValue method getBinary.

/**
     * Returns the binary representation of this value. The default
     * implementation uses the UTF-8 serialization of the string returned
     * by {@link #getString()}. Subclasses 
     */
public Binary getBinary() throws RepositoryException {
    try {
        final byte[] value = getString().getBytes("UTF-8");
        return new Binary() {

            public int read(byte[] b, long position) {
                if (position >= value.length) {
                    return -1;
                } else {
                    int p = (int) position;
                    int n = Math.min(b.length, value.length - p);
                    System.arraycopy(value, p, b, 0, n);
                    return n;
                }
            }

            public InputStream getStream() {
                return new ByteArrayInputStream(value);
            }

            public long getSize() {
                return value.length;
            }

            public void dispose() {
            }
        };
    } catch (UnsupportedEncodingException e) {
        throw new RepositoryException("UTF-8 is not supported", e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RepositoryException(javax.jcr.RepositoryException) Binary(javax.jcr.Binary)

Example 2 with Binary

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

the class RepositoryTest method testReferenceBinary.

@Test
public void testReferenceBinary() throws RepositoryException {
    ValueFactory valueFactory = getAdminSession().getValueFactory();
    Binary binary = valueFactory.createBinary(new RandomInputStream(1, 256 * 1024));
    String reference = binary instanceof ReferenceBinary ? ((ReferenceBinary) binary).getReference() : null;
    assumeTrue(reference != null);
    Session session = createAdminSession();
    try {
        valueFactory = session.getValueFactory();
        assertEquals(binary, valueFactory.createValue(new SimpleReferenceBinary(reference)).getBinary());
    } finally {
        session.logout();
    }
}
Also used : SimpleReferenceBinary(org.apache.jackrabbit.commons.jackrabbit.SimpleReferenceBinary) ValueFactory(javax.jcr.ValueFactory) SimpleReferenceBinary(org.apache.jackrabbit.commons.jackrabbit.SimpleReferenceBinary) ReferenceBinary(org.apache.jackrabbit.api.ReferenceBinary) Binary(javax.jcr.Binary) JUnitMatchers.containsString(org.junit.matchers.JUnitMatchers.containsString) SimpleReferenceBinary(org.apache.jackrabbit.commons.jackrabbit.SimpleReferenceBinary) ReferenceBinary(org.apache.jackrabbit.api.ReferenceBinary) RandomInputStream(org.apache.jackrabbit.core.data.RandomInputStream) Session(javax.jcr.Session) Test(org.junit.Test)

Example 3 with Binary

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

the class RepositoryTest method addSmallBinaryProperty.

@Test
public void addSmallBinaryProperty() throws RepositoryException, IOException {
    Node parentNode = getNode(TEST_PATH);
    InputStream is = new NumberStream(1234);
    Binary bin = getAdminSession().getValueFactory().createBinary(is);
    addProperty(parentNode, "bigBinary", getAdminSession().getValueFactory().createValue(bin));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) RandomInputStream(org.apache.jackrabbit.core.data.RandomInputStream) InputStream(java.io.InputStream) JackrabbitNode(org.apache.jackrabbit.api.JackrabbitNode) Node(javax.jcr.Node) SimpleReferenceBinary(org.apache.jackrabbit.commons.jackrabbit.SimpleReferenceBinary) ReferenceBinary(org.apache.jackrabbit.api.ReferenceBinary) Binary(javax.jcr.Binary) Test(org.junit.Test)

Example 4 with Binary

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

the class RepositoryTest method addBigBinaryProperty.

@Test
public void addBigBinaryProperty() throws RepositoryException, IOException {
    Node parentNode = getNode(TEST_PATH);
    InputStream is = new NumberStream(123456);
    Binary bin = getAdminSession().getValueFactory().createBinary(is);
    addProperty(parentNode, "bigBinary", getAdminSession().getValueFactory().createValue(bin));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) RandomInputStream(org.apache.jackrabbit.core.data.RandomInputStream) InputStream(java.io.InputStream) JackrabbitNode(org.apache.jackrabbit.api.JackrabbitNode) Node(javax.jcr.Node) SimpleReferenceBinary(org.apache.jackrabbit.commons.jackrabbit.SimpleReferenceBinary) ReferenceBinary(org.apache.jackrabbit.api.ReferenceBinary) Binary(javax.jcr.Binary) Test(org.junit.Test)

Example 5 with Binary

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

the class OperationLoggerTest method testBinaryLogger.

public void testBinaryLogger() throws Exception {
    Node node1 = testRootNode.addNode(nodeName1);
    start();
    byte[] data = "hello".getBytes();
    Binary b = superuser.getValueFactory().createBinary(new ByteArrayInputStream(data));
    node1.setProperty("foo", b);
    stop();
    boolean binaryLog = false;
    for (ILoggingEvent e : logs.list) {
        if (e.getLoggerName().startsWith(OPS_BINARY)) {
            if (e.getMessage().contains("Created binary property")) {
                assertEquals(Long.valueOf(data.length), e.getArgumentArray()[0]);
                binaryLog = true;
            }
        }
    }
    assertTrue("Did not find binary upload log", binaryLog);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Node(javax.jcr.Node) Binary(javax.jcr.Binary) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent)

Aggregations

Binary (javax.jcr.Binary)69 Node (javax.jcr.Node)36 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