Search in sources :

Example 41 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 42 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)

Example 43 with Binary

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

the class QValueValue method getBinary.

/**
     * @see javax.jcr.Value#getBinary()
     */
public Binary getBinary() throws RepositoryException {
    // JCR-2511 Value#getBinary() and #getStream() return internal representation for type PATH and NAME
    if (getType() == PropertyType.NAME || getType() == PropertyType.PATH) {
        // delegate conversion to getString() method
        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 ex) {
            throw new RepositoryException("UTF-8 is not supported", ex);
        }
    } else {
        return qvalue.getBinary();
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RepositoryException(javax.jcr.RepositoryException) Binary(javax.jcr.Binary)

Example 44 with Binary

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

the class DefaultSyncContext method createValue.

/**
     * Creates a new JCR value of the given object, checking the internal type.
     * @param v the value
     * @return the JCR value or null
     * @throws RepositoryException if an error occurs
     */
@CheckForNull
protected Value createValue(@Nullable Object v) throws RepositoryException {
    if (v == null) {
        return null;
    } else if (v instanceof Boolean) {
        return valueFactory.createValue((Boolean) v);
    } else if (v instanceof Byte || v instanceof Short || v instanceof Integer || v instanceof Long) {
        return valueFactory.createValue(((Number) v).longValue());
    } else if (v instanceof Float || v instanceof Double) {
        return valueFactory.createValue(((Number) v).doubleValue());
    } else if (v instanceof BigDecimal) {
        return valueFactory.createValue((BigDecimal) v);
    } else if (v instanceof Calendar) {
        return valueFactory.createValue((Calendar) v);
    } else if (v instanceof Date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime((Date) v);
        return valueFactory.createValue(cal);
    } else if (v instanceof byte[]) {
        Binary bin = valueFactory.createBinary(new ByteArrayInputStream((byte[]) v));
        return valueFactory.createValue(bin);
    } else if (v instanceof Binary) {
        return valueFactory.createValue((Binary) v);
    } else if (v instanceof InputStream) {
        return valueFactory.createValue((InputStream) v);
    } else if (v instanceof char[]) {
        return valueFactory.createValue(new String((char[]) v));
    } else {
        return valueFactory.createValue(String.valueOf(v));
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Calendar(java.util.Calendar) BigDecimal(java.math.BigDecimal) Date(java.util.Date) ByteArrayInputStream(java.io.ByteArrayInputStream) Binary(javax.jcr.Binary) CheckForNull(javax.annotation.CheckForNull)

Example 45 with Binary

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

the class BundlingNodeTest method addFile.

private void addFile(Node parent, String fileName) throws RepositoryException {
    Node file = getOrAddNode(parent, fileName, NodeType.NT_FILE);
    Node content = getOrAddNode(file, Node.JCR_CONTENT, contentNodeType);
    content.setProperty(Property.JCR_MIMETYPE, "text/plain");
    content.setProperty(Property.JCR_LAST_MODIFIED, Calendar.getInstance());
    Binary binary = parent.getSession().getValueFactory().createBinary(new ByteArrayInputStream("hello".getBytes()));
    content.setProperty(Property.JCR_DATA, binary);
    binary.dispose();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Node(javax.jcr.Node) JcrUtils.getOrAddNode(org.apache.jackrabbit.commons.JcrUtils.getOrAddNode) Binary(javax.jcr.Binary)

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