Package javax.jcr

Examples of javax.jcr.Binary


    public void testLengthBinaryLiteral() throws RepositoryException {
        node.setProperty(propertyName1, "abc");
        superuser.save();

        String length = String.valueOf(node.getProperty(propertyName1).getLength());
        Binary b = vf.createBinary(new ByteArrayInputStream(length.getBytes()));
        try {
            executeQuery(propertyName1, QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO,
                    vf.createValue(b));
        } finally {
            b.dispose();
        }
    }
View Full Code Here


    /**
     * Test the persistence of a property modified with an input stream
     * parameter and saved from the parent Node
     */
    public void testBinaryParentJcr2() throws RepositoryException, IOException {
        Binary bin = value.getBinary();
        property1.setValue(bin);
        node.save();
        bin = property1.getValue().getBinary();
        InputStream in = bin.getStream();
        try {
            compareStream(data, in);
        } finally {
            in.close();
        }
View Full Code Here

     * Tests that when Binary.getStream() is called a second time a new stream
     * object is returned.
     */
    public void testSameStreamJcr2() throws RepositoryException, IOException {
        Value val = PropertyUtil.getValue(prop);
        Binary bin = val.getBinary();
        InputStream in = bin.getStream();
        InputStream in2 = bin.getStream();
        try {
            assertNotSame("Value.getStream() called on a new value " +
                    "object should return a different Stream object.", in, in2);
            //check if both streams can be read independently but contain the same bytes
            int n,n2;
View Full Code Here

    /**
     * Tests the Binary.read() method.
     */
    public void testRandomAccess() throws RepositoryException, IOException {
        Value val = PropertyUtil.getValue(prop);
        Binary bin = val.getBinary();
        byte[] buf = new byte[0x1000];

        //verify that reading behind EOF returns -1
        assertEquals("reading behind EOF must return -1", -1, bin.read(buf, bin.getSize()));

        //read content using Binary.read()
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        for (int cnt, pos = 0; (cnt = bin.read(buf, pos)) > 0; pos += cnt) {
            out.write(buf, 0, cnt);
        }
        byte[] content = out.toByteArray();
        assertEquals("unexpected content length", bin.getSize(), content.length);

        //verify against stream
        InputStream in = val.getStream();
        try {
            int k = 0;
            for (int b; (b = in.read()) != -1; k++) {
                assertEquals("Value.getStream().read() and Value.getBinary().read() " +
                        "return different values.", (byte) b, content[k]);
            }
            assertEquals("unexpected content length", k, content.length);
        } finally {
            try {
                in.close();
            } catch (IOException ignore) {}
        }

        //verify random access
        buf = new byte[1];
        assertTrue("unexpected result of Value.getBinary.read()", -1 != bin.read(buf, 0));
        assertEquals("unexpected result of Value.getBinary.read()", content[0], buf[0]);
        if (content.length > 0) {
            assertTrue("unexpected result of Value.getBinary.read()", -1 != bin.read(buf, content.length - 1));
            assertEquals("unexpected result of Value.getBinary.read()", content[content.length - 1], buf[0]);
            assertTrue("unexpected result of Value.getBinary.read()", -1 != bin.read(buf, 0));
            assertEquals("unexpected result of Value.getBinary.read()", content[0], buf[0]);
        }
    }
View Full Code Here

     *                             or updated
     */
    public static Node putFile(
            Node parent, String name, String mime, Calendar date,
            InputStream data) throws RepositoryException {
        Binary binary =
            parent.getSession().getValueFactory().createBinary(data);
        try {
            Node file = getOrAddNode(parent, name, NodeType.NT_FILE);
            Node content =
                getOrAddNode(file, Node.JCR_CONTENT, NodeType.NT_RESOURCE);

            content.setProperty(Property.JCR_MIMETYPE, mime);
            String[] parameters = mime.split(";");
            for (int i = 1; i < parameters.length; i++) {
                int equals = parameters[i].indexOf('=');
                if (equals != -1) {
                    String parameter = parameters[i].substring(0, equals);
                    if ("charset".equalsIgnoreCase(parameter.trim())) {
                        content.setProperty(
                                Property.JCR_ENCODING,
                                parameters[i].substring(equals + 1).trim());
                    }
                }
            }

            content.setProperty(Property.JCR_LAST_MODIFIED, date);
            content.setProperty(Property.JCR_DATA, binary);
            return file;
        } finally {
            binary.dispose();
        }
    }
View Full Code Here

     * {{@link #toString(Item)} method.
     */
    private static void append(StringBuilder builder, Value value)
            throws RepositoryException {
        if (value.getType() == PropertyType.BINARY) {
            Binary binary = value.getBinary();
            try {
                builder.append("<");
                builder.append(binary.getSize());
                builder.append(" bytes>");
            } finally {
                binary.dispose();
            }
        } else {
            String string = value.getString();
            if (string.length() > 40) {
                builder.append(string.substring(0, 37));
View Full Code Here

            // compile the properties
            JcrFolder.setProperties(contentNode, getTypeDefinition(), properties);

            // write content, if available
            Binary binary = contentStream == null || contentStream.getStream() == null
                    ? JcrBinary.EMPTY
                    : new JcrBinary(new BufferedInputStream(contentStream.getStream()));
            try {
                contentNode.setProperty(Property.JCR_DATA, binary);
                if (contentStream != null && contentStream.getMimeType() != null) {
                    contentNode.setProperty(Property.JCR_MIMETYPE, contentStream.getMimeType());
                }
            }
            finally {
                binary.dispose();
            }

            fileNode.getSession().save();
            JcrNode jcrFileNode = getJcrNode(fileNode);
            if (versioningState == VersioningState.NONE) {
View Full Code Here

            if (autoCheckout) {
                jcrVersion.checkout();
            }

            // write content, if available
            Binary binary = contentStream == null || contentStream.getStream() == null
                    ? JcrBinary.EMPTY
                    : new JcrBinary(new BufferedInputStream(contentStream.getStream()));
            try {
                contentNode.setProperty(Property.JCR_DATA, binary);
                if (contentStream != null && contentStream.getMimeType() != null) {
                    contentNode.setProperty(Property.JCR_MIMETYPE, contentStream.getMimeType());
                }
            }
            finally {
                binary.dispose();
            }

            contentNode.getSession().save();

            if (autoCheckout) {
View Full Code Here

    @Test
    public void addBinaryProperty() throws RepositoryException, IOException {
        Node parentNode = getNode(TEST_PATH);
        InputStream is = new ByteArrayInputStream("foo\"".getBytes());
        Binary bin = getSession().getValueFactory().createBinary(is);
        addProperty(parentNode, "binary", getSession().getValueFactory().createValue(bin));
    }
View Full Code Here

    @Test
    public void addSmallBinaryProperty() throws RepositoryException, IOException {
        Node parentNode = getNode(TEST_PATH);
        InputStream is = new NumberStream(1234);
        Binary bin = getSession().getValueFactory().createBinary(is);
        addProperty(parentNode, "bigBinary", getSession().getValueFactory().createValue(bin));
    }
View Full Code Here

TOP

Related Classes of javax.jcr.Binary

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.