@Override public boolean equals( Object obj ) { if (obj == this) return true; if (obj instanceof Binary) { Binary that = (Binary)obj; if (this.getType() != that.getType()) return false; if (this.length() != that.length()) return false; return Arrays.equals(this.getBytes(), that.getBytes()); } return false; }
@Override public Binary createBinary(byte type, byte[] data) { return new Binary(type, data); }
protected void write( Binary value, Writer writer ) throws IOException { int type = value.getType() - 0; String base64 = value.getBytesInBase64(); write(new BasicDocument(BINARY_TYPE, type, BASE_64, base64), writer); }
/** * Creates content stream using JCR node. * * @param document JCR node representation * @return CMIS content stream object */ private ContentStream jcrBinaryContent( Document document ) { // pickup node properties Document props = document.getDocument("properties").getDocument(JcrLexicon.Namespace.URI); // extract binary value and content Binary value = props.getBinary("data"); if (value == null) { return null; } byte[] content = value.getBytes(); String fileName = props.getString("fileName"); String mimeType = props.getString("mimeType"); // wrap with input stream ByteArrayInputStream bin = new ByteArrayInputStream(content); bin.reset(); // create content stream return new ContentStreamImpl(fileName, BigInteger.valueOf(content.length), mimeType, bin); }
@Override public int hashCode() { return length(); }
return binaries.create(binary.getBytes());
@Override public String toString() { return "Binary (" + (int)type + ':' + length() + ')'; }
@Override public Binary createBinary(byte type, byte[] data) { return new Binary(type, data); }
return binaries.create(binary.getBytes());
@Override public boolean equals( Object obj ) { if (obj == this) return true; if (obj instanceof Binary) { Binary that = (Binary)obj; if (this.getType() != that.getType()) return false; if (this.length() != that.length()) return false; return Arrays.equals(this.getBytes(), that.getBytes()); } return false; }
protected void write( Binary value, Writer writer ) throws IOException { int type = value.getType() - 0; String base64 = value.getBytesInBase64(); write(new BasicDocument(BINARY_TYPE, type, BASE_64, base64), writer); }
@Override public int hashCode() { return length(); }
@Test public void shouldRoundTripSimpleBsonObjectWithBinary1() { byte[] data = new byte[] {0x16, 0x00, 0x00, 0x00, 0x02, 0x68, 0x65, 0x6c}; input = new BasicDocument("foo", new Binary(data)); assertRoundtrip(input); }
} else if (value instanceof Binary) { Binary binary = (Binary)value; value = new org.bson.types.Binary(binary.getBytes()); } else if (value instanceof List) { List<?> values = (List<?>)value;
@Override public String toString() { return "Binary (" + (int)type + ':' + length() + ')'; }
@Test //Fix-For MODE-1575 public void shouldRoundTripSimpleBsonObjectWithBinary2() throws Exception { ByteArrayOutputStream bos = new ByteArrayOutputStream(); InputStream is = getClass().getClassLoader().getResourceAsStream("binary"); assertNotNull(is); try { byte[] buff = new byte[1024]; int read; while ((read = is.read(buff)) != -1) { bos.write(buff, 0, read); } } finally { bos.close(); is.close(); } input = new BasicDocument("foo", new Binary(bos.toByteArray())); assertRoundtrip(input); }
@Test public void shouldCorrectlyWriteBinaryValue() { byte[] data = new byte[] {0x00, 0x01, 0x02, 0x03, 0x04, 0x05}; @SuppressWarnings( "deprecation" ) Binary binary = new Binary(Bson.BinaryType.BINARY, data); String expected = "{ \"$type\" : 2 , \"$base64\" : \"AAECAwQF\" }"; String actual = writer.write(binary); // print =true; assertSame(expected, actual); }
@Test public void shouldParseDocumentWithBinary() throws Exception { byte[] bytes = new byte[] {0x13, 0x22, 0x53, 0x00}; Binary obj = new Binary(Bson.BinaryType.MD5, bytes); value = parser(writer.write(obj)).parseValue(); assertEquals(obj, value); }