org.modeshape.jcr.api.Binary类的使用及代码示例

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

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

Binary介绍

[英]An extension of the standard javax.jcr.Binary interface, with methods to obtain the SHA-1 hash of the binary value.
[中]标准javax的扩展。jcr。二进制接口,用方法获取二进制值的SHA-1哈希。

代码示例

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

  1. @Override
  2. public long setContent( Node parentNode,
  3. String resourceName,
  4. InputStream newContent,
  5. String contentType,
  6. String characterEncoding ) throws RepositoryException, IOException {
  7. Node contentNode;
  8. if (parentNode.hasNode(CONTENT_NODE_NAME)) {
  9. contentNode = parentNode.getNode(CONTENT_NODE_NAME);
  10. } else {
  11. contentNode = parentNode.addNode(CONTENT_NODE_NAME, newContentPrimaryType);
  12. }
  13. // contentNode.setProperty(MIME_TYPE_PROP_NAME, contentType != null ? contentType : "application/octet-stream");
  14. contentNode.setProperty(ENCODING_PROP_NAME, characterEncoding != null ? characterEncoding : "UTF-8");
  15. org.modeshape.jcr.api.Binary binary = (org.modeshape.jcr.api.Binary)parentNode.getSession()
  16. .getValueFactory()
  17. .createBinary(newContent);
  18. contentNode.setProperty(DATA_PROP_NAME, binary);
  19. contentNode.setProperty(MODIFIED_PROP_NAME, Calendar.getInstance());
  20. // Copy the content to the property, THEN re-read the content from the Binary value to avoid discarding the first
  21. // bytes of the stream
  22. if (contentType == null) {
  23. contentType = binary.getMimeType(resourceName);
  24. }
  25. if (contentType != null) {
  26. contentNode.setProperty(MIME_TYPE_PROP_NAME, contentType);
  27. }
  28. return contentNode.getProperty(DATA_PROP_NAME).getLength();
  29. }

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

  1. /**
  2. * Allows subclasses to process the stream of binary value property in "safe" fashion, making sure the stream is closed at the
  3. * end of the operation.
  4. *
  5. * @param binary a {@link org.modeshape.jcr.api.Binary} who is expected to contain a non-null binary value.
  6. * @param operation a {@link org.modeshape.jcr.api.text.TextExtractor.BinaryOperation} which should work with the stream
  7. * @param <T> the return type of the binary operation
  8. * @return whatever type of result the stream operation returns
  9. * @throws Exception if there is an error processing the stream
  10. */
  11. protected final <T> T processStream( Binary binary,
  12. BinaryOperation<T> operation ) throws Exception {
  13. InputStream stream = binary.getStream();
  14. if (stream == null) {
  15. throw new IllegalArgumentException("The binary value is empty");
  16. }
  17. try {
  18. return operation.execute(stream);
  19. } finally {
  20. stream.close();
  21. }
  22. }

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

  1. /**
  2. * Get the hexadecimal form of the SHA-1 hash of the contents.
  3. *
  4. * @return the hexadecimal form of hash, or a null string if the hash could not be computed or is not known
  5. */
  6. private String getHash() {
  7. try {
  8. Node contentNode = getContextNode();
  9. Property data = contentNode.getProperty(Property.JCR_DATA);
  10. Binary bin = (Binary) data.getBinary();
  11. return String.format("{%s}%s", HASH_ALGORITHM, bin.getHexHash());
  12. } catch (RepositoryException e) {
  13. return "";
  14. }
  15. }
  16. }

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

  1. String mimeType = binaryValue.getMimeType(inputFileName);
  2. try (InputStream stream = binaryValue.getStream()) {
  3. sequencePowerpoint(sequencedNode, context.valueFactory(), stream);
  4. return true;
  5. try (InputStream stream = binaryValue.getStream()) {
  6. sequenceWord(sequencedNode, context.valueFactory(), stream);
  7. return true;
  8. try (InputStream stream = binaryValue.getStream()) {
  9. sequenceExcel(sequencedNode, context.valueFactory(), stream);
  10. return true;

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

  1. assertThat(binary, is(instanceOf(StoredBinaryValue.class)));
  2. assertThat(binary.getHexHash(), is(expectedSha1));
  3. assertThat(binary.getSize(), is(numBytes));
  4. InputStream actual = binary.getStream();
  5. byte[] buffer1 = new byte[1024];
  6. byte[] buffer2 = new byte[1024];

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

  1. protected Binary storeAndCheck( int contentIndex,
  2. Class<? extends Binary> valueClass ) throws Exception {
  3. String content = CONTENT[contentIndex];
  4. String sha1 = CONTENT_HASHES[contentIndex];
  5. InputStream stream = new ByteArrayInputStream(content.getBytes());
  6. Stopwatch sw = new Stopwatch();
  7. sw.start();
  8. Binary binary = store.storeValue(stream, false);
  9. sw.stop();
  10. if (print) System.out.println("Time to store 18MB file: " + sw.getTotalDuration());
  11. if (valueClass != null) {
  12. assertThat(binary, is(instanceOf(valueClass)));
  13. }
  14. if (content.length() == 0) {
  15. assertThat(binary, is(instanceOf(EmptyBinaryValue.class)));
  16. } else if (content.length() < MIN_BINARY_SIZE) {
  17. assertThat(binary, is(instanceOf(InMemoryBinaryValue.class)));
  18. } else {
  19. assertThat(binary, is(instanceOf(StoredBinaryValue.class)));
  20. }
  21. assertThat(binary.getHexHash(), is(sha1));
  22. String binaryContent = IoUtil.read(binary.getStream());
  23. assertThat(binaryContent, is(content));
  24. return binary;
  25. }

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

  1. @Test
  2. @FixFor("MODE-1573")
  3. public void shouldPerformRoundTripOnDocumentViewWithBinaryContent() throws Exception {
  4. JcrTools tools = new JcrTools();
  5. File binaryFile = new File("src/test/resources/io/binary.pdf");
  6. assert (binaryFile.exists() && binaryFile.isFile());
  7. File outputFile = File.createTempFile("modeshape_import_export_" + System.currentTimeMillis(), "_test");
  8. outputFile.deleteOnExit();
  9. tools.uploadFile(session, "file", binaryFile);
  10. session.save();
  11. session.exportDocumentView("/file", new FileOutputStream(outputFile), false, false);
  12. assertTrue(outputFile.length() > 0);
  13. session.getRootNode().getNode("file").remove();
  14. session.save();
  15. //sleep so that the binary can be properly cleaned up (this is done via a listener)
  16. Thread.sleep(200);
  17. session.getWorkspace().importXML("/", new FileInputStream(outputFile), ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);
  18. assertNotNull(session.getNode("/file"));
  19. assertNotNull(session.getNode("/file/jcr:content"));
  20. Property data = session.getNode("/file/jcr:content").getProperty("jcr:data");
  21. assertNotNull(data);
  22. Binary binary = (Binary)data.getBinary();
  23. assertNotNull(binary);
  24. assertEquals(binaryFile.length(), binary.getSize());
  25. }

代码示例来源:origin: org.modeshape/modeshape-sequencer-msoffice

  1. String mimeType = binaryValue.getMimeType(inputFileName);
  2. try (InputStream stream = binaryValue.getStream()) {
  3. sequencePowerpoint(sequencedNode, context.valueFactory(), stream);
  4. return true;
  5. try (InputStream stream = binaryValue.getStream()) {
  6. sequenceWord(sequencedNode, context.valueFactory(), stream);
  7. return true;
  8. try (InputStream stream = binaryValue.getStream()) {
  9. sequenceExcel(sequencedNode, context.valueFactory(), stream);
  10. return true;

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

  1. String unused = binaries.iterator().next().getHexHash();
  2. BinaryKey unusedKey = new BinaryKey(unused);
  3. store.markAsUnused(Collections.singleton(unusedKey));
  4. InputStream stream = binary.getStream();
  5. String content = IoUtil.read(stream);
  6. assertThat(content.length() != 0, is(true));

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

  1. /**
  2. * Returns the default mime-type of a given binary property.
  3. *
  4. * @param binaryProperty a non-null {@link Property}
  5. * @return a non-null String which represents the mime-type of the binary property.
  6. * @throws RepositoryException if any JCR related operation involving the binary property fail.
  7. */
  8. public String getDefaultMimeType( Property binaryProperty ) throws RepositoryException {
  9. try {
  10. Binary binary = binaryProperty.getBinary();
  11. return binary instanceof org.modeshape.jcr.api.Binary ? ((org.modeshape.jcr.api.Binary)binary)
  12. .getMimeType() : DEFAULT_MIME_TYPE;
  13. } catch (IOException e) {
  14. logger.warn("Cannot determine default mime-type", e);
  15. return DEFAULT_MIME_TYPE;
  16. }
  17. }

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

  1. Binary binaryValue = (Binary) inputProperty.getBinary();
  2. CheckArg.isNotNull(binaryValue, "binary");
  3. final String mimeType = binaryValue.getMimeType();
  4. boolean isValid = false;
  5. AudioMetadata metadata = null;
  6. try (InputStream stream = binaryValue.getStream()) {
  7. metadata = new AudioMetadata(stream, mimeType);
  8. isValid = metadata.check();

代码示例来源:origin: org.modeshape/modeshape-jcr-api

  1. /**
  2. * Allows subclasses to process the stream of binary value property in "safe" fashion, making sure the stream is closed at the
  3. * end of the operation.
  4. *
  5. * @param binary a {@link org.modeshape.jcr.api.Binary} who is expected to contain a non-null binary value.
  6. * @param operation a {@link org.modeshape.jcr.api.text.TextExtractor.BinaryOperation} which should work with the stream
  7. * @param <T> the return type of the binary operation
  8. * @return whatever type of result the stream operation returns
  9. * @throws Exception if there is an error processing the stream
  10. */
  11. protected final <T> T processStream( Binary binary,
  12. BinaryOperation<T> operation ) throws Exception {
  13. InputStream stream = binary.getStream();
  14. if (stream == null) {
  15. throw new IllegalArgumentException("The binary value is empty");
  16. }
  17. try {
  18. return operation.execute(stream);
  19. } finally {
  20. stream.close();
  21. }
  22. }

代码示例来源:origin: org.fcrepo/fcrepo-kernel-modeshape

  1. /**
  2. * Add necessary information to node
  3. * @param dsNode The target binary node to add information to
  4. * @param descNode The description node associated with the binary node
  5. * @param checksums The checksum information
  6. * @throws RepositoryException on error
  7. */
  8. private static void decorateContentNode(final Node dsNode, final Node descNode, final Collection<URI> checksums)
  9. throws RepositoryException {
  10. if (dsNode == null) {
  11. LOGGER.warn("{} node appears to be null!", JCR_CONTENT);
  12. return;
  13. }
  14. if (dsNode.hasProperty(JCR_DATA)) {
  15. final Property dataProperty = dsNode.getProperty(JCR_DATA);
  16. final Binary binary = (Binary) dataProperty.getBinary();
  17. final String dsChecksum = binary.getHexHash();
  18. checksums.add(ContentDigest.asURI(SHA1.algorithm, dsChecksum));
  19. final String[] checksumArray = new String[checksums.size()];
  20. checksums.stream().map(Object::toString).collect(Collectors.toSet()).toArray(checksumArray);
  21. if (descNode != null) {
  22. descNode.setProperty(CONTENT_DIGEST, checksumArray);
  23. descNode.setProperty(CONTENT_SIZE, dataProperty.getLength());
  24. }
  25. LOGGER.debug("Decorated data property at path: {}", dataProperty.getPath());
  26. }
  27. }

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

  1. private void addFileContent( ZipInputStream zipInputStream,
  2. ZipEntry entry,
  3. Context context,
  4. Node zipFileNode ) throws RepositoryException, IOException {
  5. Node contentNode = zipFileNode.addNode(JcrConstants.JCR_CONTENT, JcrConstants.NT_RESOURCE);
  6. // on session pre-save the appropriate properties should be set automatically
  7. contentNode.addMixin(JcrConstants.MIX_LAST_MODIFIED);
  8. // set the content bytes
  9. byte[] contentBytes = readContent(zipInputStream);
  10. org.modeshape.jcr.api.Binary contentBinary = context.valueFactory().createBinary(contentBytes);
  11. contentNode.setProperty(JcrConstants.JCR_DATA, contentBinary);
  12. // Figure out the mime type ...
  13. String mimeType = contentBinary.getMimeType(entry.getName());
  14. if (mimeType != null) {
  15. contentNode.setProperty(JcrConstants.JCR_MIME_TYPE, mimeType);
  16. }
  17. }

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

  1. protected void assertBinaryContains( Binary binaryValue,
  2. byte[] expectedContent ) throws IOException, RepositoryException {
  3. byte[] actual = IoUtil.readBytes(binaryValue.getStream());
  4. assertThat(actual, is(expectedContent));
  5. }

代码示例来源:origin: org.fcrepo/fcrepo-kernel-modeshape

  1. final String dsSHA1 = ((Binary) dataProperty.getBinary()).getHexHash();
  2. final URI dsSHA1Uri = ContentDigest.asURI(SHA1.algorithm, dsSHA1);

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

  1. mimeType = ((org.modeshape.jcr.api.Binary)binary).getMimeType(parent.getName());

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

  1. String str = null;
  2. if (value instanceof Binary) {
  3. byte[] bytes = IoUtil.readBytes(((Binary)value).getStream());
  4. str = StringUtil.getHexString(bytes);
  5. } else {

代码示例来源:origin: org.fcrepo/modeshape-jcr

  1. sha1 = fieldValue.toString();
  2. } else if (fieldValue instanceof org.modeshape.jcr.api.Binary && !(fieldValue instanceof InMemoryBinaryValue)) {
  3. sha1 = ((org.modeshape.jcr.api.Binary)fieldValue).getHexHash();

代码示例来源:origin: org.fcrepo/modeshape-jcr

  1. mimeType = ((org.modeshape.jcr.api.Binary)binary).getMimeType(parent.getName());

相关文章