/** * {@inheritDoc} * * @see org.modeshape.graph.property.ValueFactory#create(org.modeshape.graph.property.Binary) */ public String create( Binary value ) throws ValueFormatException, IoException { if (value == null) return null; try { value.acquire(); InputStream stream = value.getStream(); try { return create(stream, value.getSize()); } finally { try { stream.close(); } catch (IOException e) { Logger.getLogger(getClass()).debug(e, "Error closing the stream while converting from Binary to String"); } } } finally { value.release(); } }
/** * {@inheritDoc} * * @see org.modeshape.graph.property.ValueTypeSystem.Factory#asReadableString(java.lang.Object) */ @Override public String asReadableString( Object value ) { Binary binary = this.valueFactory.create(value); // Just print out the SHA-1 hash in Base64, plus length return "(Binary,length=" + binary.getSize() + ",SHA1=" + Base64.encodeBytes(binary.getHash()) + ")"; }
@Test public void shouldCompareBinaryAndStringValues() { String strValue = "Jack and Jill went up the hill"; Binary binaryValue = factories.getBinaryFactory().create(strValue); Binary binarySpy = spy(binaryValue); // The values should compare to each other ... assertThat(ValueComparators.OBJECT_COMPARATOR.compare(strValue, binarySpy), is(0)); assertThat(ValueComparators.OBJECT_COMPARATOR.compare(binarySpy, strValue), is(0)); // Verify that the binary value was not obtained, but that we just used the hash verify(binarySpy, times(0)).getBytes(); verify(binarySpy, times(0)).getStream(); verify(binarySpy, atLeastOnce()).getHash(); }
@FixFor( "MODE-966" ) @Test public void shouldStoreLargeBinaryValue() throws Exception { File file = new File("src/test/resources/testdata/test1.xmi"); assertThat(file.exists(), is(true)); BinaryFactory binaryFactory = context.getValueFactories().getBinaryFactory(); Binary binaryValue = binaryFactory.create(file); graph.batch() .create("/someFile.xmi") .with("jcr:primaryType", "nt:file") .and() .create("/someFile.xmi/jcr:content") .with("jcr:priamryType", "nt:resource") .and("jcr:data", binaryValue) .and() .execute(); // Now read the content back out ... Property data = graph.getProperty("jcr:data").on("/someFile.xmi/jcr:content"); Binary readValue = binaryFactory.create(data.getFirstValue()); // and verify the content matches ... assertThat(binaryValue.getHash(), is(readValue.getHash())); assertThat(binaryValue.getBytes(), is(readValue.getBytes())); }
public LargeValue get( Binary obj ) { return get(obj.getHash()); }
/** * {@inheritDoc} * * @see org.modeshape.graph.property.ValueTypeSystem.Factory#length(java.lang.Object) */ @Override public long length( Object value ) { Binary binary = this.valueFactory.create(value); return binary != null ? binary.getSize() : 0; } };
@Test public void shouldUseSizeWhenComparing() { byte[] shorterContent = new byte[validByteArrayContent.length - 2]; for (int i = 0; i != shorterContent.length; ++i) { shorterContent[i] = validByteArrayContent[i]; } Binary another = new InMemoryBinary(shorterContent); assertThat(binary.equals(another), is(false)); assertThat(binary.compareTo(another), is(1)); assertThat(another.compareTo(binary), is(-1)); assertThat(another, hasContent(shorterContent)); }
public boolean isSkipped( Binary binary ) { String key = StringUtil.getHexString(binary.getHash()); return isSkipped(key); }
/** * {@inheritDoc} */ @Override public boolean equals( Object obj ) { if (obj == this) return true; if (obj instanceof Binary) { Binary that = (Binary)obj; if (this.getSize() != that.getSize()) return false; return ValueComparators.BINARY_COMPARATOR.compare(this, that) == 0; } return false; }
if (o2 == null) return 1; try { o1.acquire(); try { o2.acquire(); final long len1 = o1.getSize(); final long len2 = o2.getSize(); if (len1 < len2) return -1; if (len1 > len2) return 1; byte[] hash1 = o1.getHash(); byte[] hash2 = o2.getHash(); if (hash1.length != 0 || hash2.length != 0) { assert hash1.length == hash2.length; InputStream stream2 = null; try { stream1 = o1.getStream(); stream2 = o2.getStream(); byte[] buffer1 = new byte[1024]; byte[] buffer2 = new byte[1024]; o2.release(); o1.release();
@Override public boolean matchesSafely( Binary content ) { try { content.acquire(); byte[] actualContents = content.getBytes(); if (actualContents.length != expectedContent.length) return false; for (int i = 0, len = actualContents.length; i != len; ++i) { if (actualContents[i] != expectedContent[i]) return false; } return true; } finally { content.release(); } }
value2 = stringFactory.create(o2); hash2 = SecureHash.getHash(SecureHash.Algorithm.SHA_1, value2.getBytes()); hash1 = ((Binary)o1).getHash(); } else { assert type2 == PropertyType.BINARY; value1 = stringFactory.create(o1); hash1 = SecureHash.getHash(SecureHash.Algorithm.SHA_1, value1.getBytes()); hash2 = ((Binary)o2).getHash();