Refine search
private void spoolSingleValued(OutputStream out) throws IOException { try { Binary binary = ((Property) item).getBinary(); try { InputStream in = binary.getStream(); try { IOUtil.spool(in, out); } finally { in.close(); } } finally { binary.dispose(); } } catch (RepositoryException e) { log.error("Cannot obtain stream from " + item, e); } }
/** * Return the length of the specified JCR value object. * * @param value The value. * @return The length of the given value. * @throws RepositoryException If an error occurs. */ private static long getLength(Value value) throws RepositoryException { if (value.getType() == PropertyType.BINARY) { return value.getBinary().getSize(); } else { return value.getString().length(); } }
@Override public byte[] getBytes( long pos, int length ) throws SQLException { try { byte[] data = new byte[length]; int numRead = 0; try { numRead = binary.read(data, pos); } finally { binary.dispose(); } // We may have read less than the desired length ... if (numRead < length) { // create a shortened array ... byte[] shortData = new byte[numRead]; System.arraycopy(data, 0, shortData, 0, numRead); data = shortData; } return data; } catch (Exception e) { throw new SQLException(e); } }
/** * Test the persistence of a property modified with an BinaryValue parameter * and saved from the Session */ public void testBinarySessionJcr2() throws RepositoryException, IOException { property1.setValue(value); superuser.save(); Binary bin = property1.getValue().getBinary(); try { InputStream in = bin.getStream(); try { compareStream(data, in); } finally { try { in.close(); } catch (IOException ignore) {} } } finally { bin.dispose(); } }
Session session = createAdminSession(); try { assertTrue(session.nodeExists("/properties")); Node properties = session.getNode("/properties"); assertEquals( PropertyType.BOOLEAN, properties.getProperty("boolean").getType()); assertEquals( true, properties.getProperty("boolean").getBoolean()); assertEquals( PropertyType.BINARY, properties.getProperty("binary").getType()); Binary binary = properties.getProperty("binary").getBinary(); try { InputStream stream = binary.getStream(); try { for (byte aBINARY : BINARY) { assertEquals(aBINARY, (byte) stream.read()); assertEquals(-1, stream.read()); } finally { stream.close(); binary.dispose();
Node jcrContent = session.getNode(path + "/jcr:content"); InputStream input = jcrContent.getProperty("jcr:data").getBinary().getStream(); session.save(); int read; byte[] bytes = new byte[BYTES_DOWNLOAD]; OutputStream os = response.getOutputStream(); while ((read = input.read(bytes)) != -1) { os.write(bytes, 0, read); input.close(); os.flush(); os.close();
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(); bin.dispose();
public void test() throws Exception { Node root = superuser.getRootNode(); ValueFactory vf = superuser.getValueFactory(); // store a binary in the data store root.setProperty("p1", vf.createBinary(new RandomInputStream(1, STREAM_LENGTH))); superuser.save(); // read from the binary, but don't close the file Value v1 = root.getProperty("p1").getValue(); InputStream in = v1.getBinary().getStream(); in.read(); // store the same content at a different place - // this will change the last modified date of the file root.setProperty("p2", vf.createBinary(new RandomInputStream(1, STREAM_LENGTH))); superuser.save(); in.close(); }
Binary bin = val.getBinary(); try { byte[] buf = new byte[0x1000]; assertEquals("reading behind EOF must return -1", -1, bin.read(buf, bin.getSize())); for (int cnt, pos = 0; (cnt = bin.read(buf, pos)) > 0; pos += cnt) { out.write(buf, 0, cnt); assertEquals("unexpected content length", bin.getSize(), content.length); } finally { try { in.close(); } catch (IOException ignore) {} 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]); bin.dispose();
@Test public void getStream() throws RepositoryException, IOException { byte[] bytes = "just a test".getBytes(); Binary binary = new TestBinary(bytes); Blob blob = new BinaryBasedBlob(binary); assertEquals(bytes.length, blob.length()); InputStream expected = binary.getStream(); InputStream actual = blob.getNewStream(); try { for (int e = expected.read(); e != -1; e = expected.read()) { assertEquals(e, actual.read()); } assertEquals(-1, actual.read()); } finally { expected.close(); actual.close(); } }
/** * Tests reading concurrently from two different streams coming from the * same property. */ public void testTwoStreamsFromSamePropertyConcurrently() throws Exception { Node root = superuser.getRootNode(); ValueFactory vf = superuser.getValueFactory(); root.setProperty("p1", vf.createBinary(new RandomInputStream(1, STREAM_LENGTH))); superuser.save(); InputStream i1 = root.getProperty("p1").getBinary().getStream(); InputStream i2 = root.getProperty("p1").getBinary().getStream(); assertEquals("Streams are different", i1, i2); try { i1.close(); } catch (IOException e) { log.info("Could not close first input stream: ", e); } try { i2.close(); } catch (IOException e) { log.info("Could not close second input stream: ", e); } }
private Value toDiffValue(javax.jcr.Value value) throws JcrDiffException { try { if (value.getType() == PropertyType.BINARY) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); final InputStream stream = value.getBinary().getStream(); IOUtils.copy(stream, baos); stream.close(); return new Value(Base64.encodeBase64String(baos.toByteArray())); } else { return new Value(value.getString()); } } catch (IOException | RepositoryException e) { throw new JcrDiffException(e); } }
public void writeFile(InputStream in, String relPath) throws IOException { try { Node content; Node local = getOrCreateItem(relPath, false); if (local.hasNode(JcrConstants.JCR_CONTENT)) { content = local.getNode(JcrConstants.JCR_CONTENT); } else { content = local.addNode(JcrConstants.JCR_CONTENT, JcrConstants.NT_RESOURCE); } Binary b = content.getSession().getValueFactory().createBinary(in); content.setProperty(JcrConstants.JCR_DATA, b); content.setProperty(JcrConstants.JCR_LASTMODIFIED, Calendar.getInstance()); if (!content.hasProperty(JcrConstants.JCR_MIMETYPE)){ content.setProperty(JcrConstants.JCR_MIMETYPE, "application/octet-stream"); } b.dispose(); in.close(); } catch (RepositoryException e) { IOException io = new IOException("Error while writing file " + relPath); io.initCause(e); throw io; } }
private static void outputPropertyValue(Property property, Value value, JsonWriter out) throws RepositoryException, IOException { if (value.getType() == PropertyType.STRING) { out.value(value.getString()); } else if (value.getType() == PropertyType.BINARY) { try { java.io.InputStream stream = value.getBinary().getStream(); String ckSum = DigestUtils.sha1Hex(stream); stream.close(); out.value(ckSum); } catch (IOException e) { out.value("ERROR: calculating hash for binary of " + property.getPath() + " : " + e.getMessage()); } else if (value.getType() == PropertyType.BOOLEAN) {
/** * Gets the checksum for a Binary value. * @param value the Value * @return the checksum * @throws RepositoryException * @throws IOException */ protected String getBinaryChecksum(final Value value) throws RepositoryException, IOException { InputStream stream = null; try { stream = value.getBinary().getStream(); return DigestUtils.sha1Hex(stream); } finally { if (stream != null) { stream.close(); } } }
/** * 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(); try { property1.setValue(bin); node.save(); bin = property1.getValue().getBinary(); InputStream in = bin.getStream(); try { compareStream(data, in); } finally { try { in.close(); } catch (IOException ignore) {} } } finally { bin.dispose(); } }
public static void verifyBlob(Session session) throws IOException, RepositoryException { Property p = session.getProperty("/sling-logo.png/jcr:content/jcr:data"); InputStream is = p.getValue().getBinary().getStream(); String expectedMD5 = "35504d8c59455ab12a31f3d06f139a05"; try { assertEquals(expectedMD5, DigestUtils.md5Hex(is)); } finally { is.close(); } }
@Test public void shouldAccessBinaryContent() throws Exception { Node file = getSession().getNode("/cmis/My_Folder-0-0/My_Document-1-0"); Node cnt = file.getNode("jcr:content"); Property value = cnt.getProperty("jcr:data"); Binary bv = value.getValue().getBinary(); InputStream is = bv.getStream(); ByteArrayOutputStream bout = new ByteArrayOutputStream(); int b = 0; while (b != -1) { b = is.read(); if (b != -1) { bout.write(b); } } byte[] content = bout.toByteArray(); String s = new String(content, 0, content.length); assertFalse("Content shouldn't be empty.", s.trim().isEmpty()); }
@FixFor("MODE-1308") @Test public void shouldAllowAnyBinaryImplementation() throws Exception { Node node = binaryProp.getParent(); final String stringValue = "This is the string stringValue"; Binary binaryValue = new InMemoryTestBinary(stringValue.getBytes()); node.setProperty("binProp", binaryValue); // Get the actual binary value ... Binary nodeValue = node.getProperty("binProp").getBinary(); assertThat(nodeValue, is(not(sameInstance(binaryValue)))); assertThat(nodeValue, is(notNullValue())); assertThat(stringValue.getBytes().length, is((int)nodeValue.getSize())); // Check the contents ... byte[] buffer = new byte[100]; int available; InputStream inputStream = nodeValue.getStream(); ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); while ((available = inputStream.read(buffer)) != -1) { byteOut.write(buffer, 0, available); } assertThat(stringValue.getBytes(), is(byteOut.toByteArray())); }