javax.jcr.Binary类的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(11.1k)|赞(0)|评价(0)|浏览(168)

本文整理了Java中javax.jcr.Binary类的一些代码示例,展示了Binary类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Binary类的具体详情如下:
包路径:javax.jcr.Binary
类名称:Binary

Binary介绍

[英]A Binary object holds a JCR property value of type BINARY. The Binary interface and the related methods in Property, Value and ValueFactory replace the deprecated Value#getStream and Property#getStreammethods.
[中]Binary对象持有类型为BINARY的JCR属性值。Binary接口和Property、Value和ValueFactory中的相关方法将替换不推荐使用的Value#getStream和Property#getStreammethods。

代码示例

代码示例来源:origin: apache/jackrabbit

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);
  }
}

代码示例来源:origin: apache/jackrabbit-oak

/**
 * 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();
  }
}

代码示例来源:origin: apache/jackrabbit

protected void checkProperty(Property prop) throws Exception {
    for (int i = 0; i < 3; i++) {
      Binary b = prop.getBinary();
      try {
        //System.out.println(b.getClass() + ": " + System.identityHashCode(b));
        b.read(new byte[1], 0);
      } finally {
        b.dispose();
      }
    }
  }
}

代码示例来源:origin: ModeShape/modeshape

@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);
  }
}

代码示例来源:origin: apache/jackrabbit

/**
 * 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();
  }
}

代码示例来源:origin: apache/jackrabbit-oak

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();

代码示例来源:origin: com.cognifide.cq/cqsm-bundle

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();

代码示例来源:origin: apache/jackrabbit

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();

代码示例来源:origin: apache/jackrabbit

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();
}

代码示例来源:origin: apache/jackrabbit

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();

代码示例来源:origin: apache/jackrabbit-oak

@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();
  }
}

代码示例来源:origin: apache/jackrabbit

/**
 * 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);
  }
}

代码示例来源:origin: org.onehippo.cms7/jcrdiff-core

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);
  }
}

代码示例来源:origin: org.apache.jackrabbit.vault/org.apache.jackrabbit.vault

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;
  }
}

代码示例来源:origin: Adobe-Consulting-Services/acs-aem-commons

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) {

代码示例来源:origin: Adobe-Consulting-Services/acs-aem-commons

/**
 * 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();
    }
  }
}

代码示例来源:origin: apache/jackrabbit

/**
 * 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();
  }
}

代码示例来源:origin: apache/jackrabbit-oak

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();
  }
}

代码示例来源:origin: ModeShape/modeshape

@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());
}

代码示例来源:origin: ModeShape/modeshape

@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()));
}

相关文章