public static Blob copyFrom(InputStream input) throws IOException { BufferedInputStream bufferedInput = new BufferedInputStream(input); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); int value; while ((value = bufferedInput.read()) != -1) { bytes.write(value); } return copyFrom(bytes.toByteArray()); }
private Entity createEntity(Key key, Map<String, Object> data) throws IOException { Entity.Builder builder = Entity.newBuilder(key).set(CREATED_FIELD, Timestamp.now()); for (Entry<String, Object> entry : data.entrySet()) { if (entry.getValue() instanceof String) { builder.set(entry.getKey(), (String) entry.getValue()); // StringValue } else if (entry.getValue() instanceof Integer) { builder.set(entry.getKey(), (Integer) entry.getValue()); // LongValue } else if (entry.getValue() instanceof Double) { builder.set(entry.getKey(), (Double) entry.getValue()); // DoubleValue } else if (entry.getValue() instanceof Boolean) { builder.set(entry.getKey(), (Boolean) entry.getValue()); // BooleanValue } else if (entry.getValue() instanceof Timestamp) { builder.set(entry.getKey(), (Timestamp) entry.getValue()); // TimestampValue } else { ByteArrayOutputStream bos = new ByteArrayOutputStream(); try (ObjectOutputStream out = new ObjectOutputStream(bos)) { out.writeObject(entry.getValue()); } builder.set(entry.getKey(), Blob.copyFrom(bos.toByteArray())); // BlobValue } } return builder.build(); }
@Test public void testCopyFrom() throws Exception { Blob blob = Blob.copyFrom(ByteBuffer.wrap(bytes1)); assertEquals(blob1, blob); assertArrayEquals(bytes1, blob.toByteArray()); blob = Blob.copyFrom(new ByteArrayInputStream(bytes2)); assertEquals(blob2, blob); assertArrayEquals(bytes2, blob.toByteArray()); } }
@Test public void testEquals() throws Exception { assertEquals(blob1, blob1); assertEquals(blob1, Blob.copyFrom(bytes1)); assertNotEquals(blob1, blob2); }
@Test public void testGetBlob() throws Exception { BaseEntity<Key> entity = builder.build(); assertEquals(BLOB, entity.getBlob("blob")); Blob blob = Blob.copyFrom(new byte[] {}); entity = builder.set("blob", BlobValue.of(blob)).build(); assertEquals(blob, entity.getBlob("blob")); }
@Override protected Value<Blob> toDatastore(final byte[] value) { return BlobValue.of(Blob.copyFrom(value)); } }
@Override protected Value<Blob> saveValue(final Object value, final SaveContext ctx, final Path path) throws SkipException { try { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); OutputStream out = baos; if (serializeAnno.zip()) { final Deflater deflater = new Deflater(serializeAnno.compressionLevel()); out = new DeflaterOutputStream(out, deflater); } final ObjectOutputStream oos = new ObjectOutputStream(out); oos.writeObject(value); oos.close(); return BlobValue.of(Blob.copyFrom(baos.toByteArray())); } catch (IOException ex) { path.throwIllegalState("Unable to serialize " + value, ex); return null; // never gets here } }
@Before public void setUp() { builder = new Builder(); builder.set("blob", BLOB).set("boolean", true).set("timestamp", TIMESTAMP); builder.set("double", 1.25).set("key", KEY).set("string", "hello world"); builder.set("long", 125).setNull("null").set("entity", ENTITY).set("latLng", LAT_LNG); builder.set("partialEntity", PARTIAL_ENTITY).set("stringValue", StringValue.of("bla")); builder.set("list1", NullValue.of(), StringValue.of("foo"), LatLngValue.of(LAT_LNG)); builder.set("list2", ImmutableList.of(LongValue.of(10), DoubleValue.of(2))); builder.set("list3", Collections.singletonList(BooleanValue.of(true))); builder.set( "blobList", BLOB, Blob.copyFrom(new byte[] {3, 4}), Blob.copyFrom(new byte[] {5, 6})); builder.set("booleanList", true, false, true); builder.set("timestampList", Timestamp.now(), Timestamp.now(), Timestamp.now()); builder.set("doubleList", 12.3, 4.56, .789); builder.set( "keyList", KEY, Key.newBuilder("ds2", "k2", "n2").build(), Key.newBuilder("ds3", "k3", "n3").build()); builder.set("entityList", ENTITY, PARTIAL_ENTITY); builder.set("stringList", "s1", "s2", "s3"); builder.set("longList", 1, 23, 456); builder.set("latLngList", LAT_LNG, LAT_LNG); }
@Override public Blob convert(byte[] source) { return Blob.copyFrom(source); } };
@Override public Blob convert(byte[] source) { return Blob.copyFrom(source); } };
@Override final protected P loadSafe(final Value<D> value, final LoadContext ctx, final Path path) throws SkipException { if (!isTypeExpected(value.getType())) { // Normally we would just throw an error here but there are some edge cases caused by projection queries. // For example, timestamps come back as LongValue and blobs come back as StringValue. We'll special-case them. // The downside is that a user who changes a field from 'long' to 'Date' will not trigger an error. // The exact logic here comes from com.google.cloud.datastore.ProjectionEntity if (value.getType() == ValueType.LONG && isTypeExpected(ValueType.TIMESTAMP)) { @SuppressWarnings("unchecked") final Value<D> timestampValue = (Value<D>)TimestampValue.of(Timestamp.ofTimeMicroseconds((Long)value.get())); return loadValue(timestampValue, ctx, path); } else if (value.getType() == ValueType.STRING && isTypeExpected(ValueType.BLOB)) { @SuppressWarnings("unchecked") final Value<D> blobValue = (Value<D>)BlobValue.of(Blob.copyFrom(((String)value.get()).getBytes(StandardCharsets.UTF_8))); return loadValue(blobValue, ctx, path); } else { path.throwIllegalState("Expected value of type " + Arrays.toString(expectedValueTypes) + ", got " + value.getType() + ": " + value); } } return loadValue(value, ctx, path); }
public static Blob copyFrom(InputStream input) throws IOException { BufferedInputStream bufferedInput = new BufferedInputStream(input); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); int value; while ((value = bufferedInput.read()) != -1) { bytes.write(value); } return copyFrom(bytes.toByteArray()); }
@Override public ValueBuilder<?, ?, ?> toDatastore(Object input) { if (input == null) { return NullValue.newBuilder(); } return BlobValue.newBuilder(Blob.copyFrom((byte[]) input)); }
queryBuilder.setBinding(bindingName, toTimestamp((ZonedDateTime) bindingValue)); } else if (bindingValue instanceof byte[]) { queryBuilder.setBinding(bindingName, Blob.copyFrom((byte[]) bindingValue)); } else if (bindingValue instanceof DatastoreKey) { queryBuilder.setBinding(bindingName, ((DatastoreKey) bindingValue).nativeKey());
queryBuilder.addBinding(toTimestamp((ZonedDateTime) binding)); } else if (binding instanceof byte[]) { queryBuilder.addBinding(Blob.copyFrom((byte[]) binding)); } else if (binding instanceof DatastoreKey) { queryBuilder.addBinding(((DatastoreKey) binding).nativeKey());
.set(_model.getMaxInactive(), session.getMaxInactiveMs()) .set(_model.getLastSaved(), session.getLastSaved()) .set(_model.getAttributes(), BlobValue.newBuilder(Blob.copyFrom(baos.toByteArray())).setExcludeFromIndexes(true).build()).build(); return entity;