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()); }
@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()); } }
private static Map<String, Object> getProperties(Entity entity) throws IOException, ClassNotFoundException { if (entity == null) { return null; } ImmutableMap.Builder<String, Object> builder = new ImmutableMap.Builder<>(); for (String property : entity.getNames()) { // builder.put(property, entity.getValue(property)); if (entity.getValue(property) instanceof StringValue) { builder.put(property, (String) entity.getString(property)); } else if (entity.getValue(property) instanceof LongValue) { // This conversion is safe because of integer to long conversion above builder.put(property, new Long(entity.getLong(property)).intValue()); } else if (entity.getValue(property) instanceof DoubleValue) { builder.put(property, (Double) entity.getDouble(property)); } else if (entity.getValue(property) instanceof BooleanValue) { builder.put(property, (Boolean) entity.getBoolean(property)); } else if (entity.getValue(property) instanceof TimestampValue) { builder.put(property, (Timestamp) entity.getTimestamp(property)); } else { Blob blob = entity.getBlob(property); Object obj = null; try (ObjectInputStream in = new ObjectInputStream(blob.asInputStream())) { obj = in.readObject(); } builder.put(property, obj); // BlobValue } } return builder.build(); }
@Test public void testToByteArray() throws Exception { assertArrayEquals(bytes1, blob1.toByteArray()); assertArrayEquals(bytes2, blob2.toByteArray()); }
/** * Copies bytes into a buffer. * * @throws IndexOutOfBoundsException if an offset or size is negative or too large */ public void copyTo(byte[] target) { byteString.copyTo(target, 0, 0, getLength()); }
/** Returns an {@link InputStream} for this blob content. */ public InputStream asInputStream() { final ByteBuffer byteBuffer = asReadOnlyByteBuffer(); return new InputStream() { @Override public int read() { return !byteBuffer.hasRemaining() ? -1 : byteBuffer.get() & 0xFF; } }; }
@Test public void testGetBlob() throws Exception { assertArrayEquals(STRING_INDEX_VALUE.get().getBytes(), ENTITY2.getBlob("a").toByteArray()); assertEquals(BLOB_VALUE.get(), ENTITY2.getBlob("b")); }
@Test public void testAsReadOnlyByteBuffer() throws Exception { ByteBuffer buffer = blob1.asReadOnlyByteBuffer(); byte[] bytes = new byte[bytes1.length]; buffer.get(bytes); assertFalse(buffer.hasRemaining()); assertArrayEquals(bytes1, bytes); }
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(); }
@Override protected byte[] toPojo(final Value<Blob> value) { return value.get().toByteArray(); }
@Test public void testAsInputStream() throws Exception { byte[] bytes = new byte[bytes1.length]; InputStream in = blob1.asInputStream(); assertEquals(bytes1.length, in.read(bytes)); assertEquals(-1, in.read()); assertArrayEquals(bytes1, bytes); }
/** * Copies bytes into a buffer. * * @throws IndexOutOfBoundsException if an offset or size is negative or too large */ public void copyTo(byte[] target) { byteString.copyTo(target, 0, 0, getLength()); }
/** Returns an {@link InputStream} for this blob content. */ public InputStream asInputStream() { final ByteBuffer byteBuffer = asReadOnlyByteBuffer(); return new InputStream() { @Override public int read() { return !byteBuffer.hasRemaining() ? -1 : byteBuffer.get() & 0xFF; } }; }
@Override protected Object loadValue(final Value<Blob> value, final LoadContext ctx, final Path path) throws SkipException { Exception initialException = null; // Need to be careful here because we don't really know if the data was serialized or not. Start // with whatever the annotation says, and if that doesn't work, try the other option. try { final ByteArrayInputStream bais = new ByteArrayInputStream(value.get().toByteArray()); // Start with the annotation's zip setting final boolean unzip = serializeAnno.zip(); try { return readObject(bais, unzip); } catch (IOException ex) { // will be one of ZipException or StreamCorruptedException initialException = ex; if (log.isInfoEnabled()) log.info("Error trying to deserialize object using unzip=" + unzip + ", retrying with " + !unzip, ex); return readObject(bais, !unzip); // this will pass the exception up } } catch (Exception ex) { if (initialException == null) { initialException = ex; } path.throwIllegalState("Unable to deserialize " + value, initialException); return null; // never gets here } }
session.setLastSaved(lastSaved); session.setExpiry(expiry); try (ClassLoadingObjectInputStream ois = new ClassLoadingObjectInputStream(blob.asInputStream()))
@Test public void testEquals() throws Exception { assertEquals(blob1, blob1); assertEquals(blob1, Blob.copyFrom(bytes1)); assertNotEquals(blob1, blob2); }
@Override public byte[] convert(Blob source) { return source.toByteArray(); } };
@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")); }