Search in sources :

Example 51 with Binary

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

the class GetNodeCommand method execute0.

@Override
protected byte[] execute0(Session session) throws RepositoryException, IOException {
    Node node = session.getNode(getPath());
    Property property;
    if (node.hasProperty("jcr:data")) {
        property = node.getProperty("jcr:data");
    } else {
        if (!node.hasNode("jcr:content")) {
            return null;
        }
        Node contentNode = node.getNode("jcr:content");
        if (!contentNode.hasProperty("jcr:data")) {
            return null;
        }
        property = contentNode.getProperty("jcr:data");
    }
    if (property.getType() == PropertyType.BINARY) {
        Binary binary = property.getBinary();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            byte[] buffer = new byte[2048];
            InputStream stream = binary.getStream();
            int read;
            while ((read = stream.read(buffer)) != -1) {
                out.write(buffer, 0, read);
            }
            return out.toByteArray();
        } finally {
            binary.dispose();
        }
    }
    return null;
}
Also used : InputStream(java.io.InputStream) Node(javax.jcr.Node) Binary(javax.jcr.Binary) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Property(javax.jcr.Property)

Aggregations

Binary (javax.jcr.Binary)51 ByteArrayInputStream (java.io.ByteArrayInputStream)28 InputStream (java.io.InputStream)22 Node (javax.jcr.Node)20 Value (javax.jcr.Value)14 Test (org.junit.Test)12 Session (javax.jcr.Session)10 ReferenceBinary (org.apache.jackrabbit.api.ReferenceBinary)8 BigDecimal (java.math.BigDecimal)7 SimpleReferenceBinary (org.apache.jackrabbit.commons.jackrabbit.SimpleReferenceBinary)7 RandomInputStream (org.apache.jackrabbit.core.data.RandomInputStream)7 IOException (java.io.IOException)5 Property (javax.jcr.Property)5 RepositoryException (javax.jcr.RepositoryException)5 NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)4 JackrabbitNode (org.apache.jackrabbit.api.JackrabbitNode)4 NamespaceRegistry (javax.jcr.NamespaceRegistry)3 ValueFactory (javax.jcr.ValueFactory)3 NodeTypeTemplate (javax.jcr.nodetype.NodeTypeTemplate)3 QValue (org.apache.jackrabbit.spi.QValue)3