private byte[] readBlob(final Long blobId) { try (BlobReader readBlob = blobstore.readBlob(blobId)) { int blobSize = (int) readBlob.getSize(); byte[] result = new byte[blobSize]; readBlob.read(result, 0, blobSize); return result; } }
private Long createBlob(final InputStream inputStream) { Objects.requireNonNull(inputStream, "inputStream cannot be null"); try (BlobAccessor blobAccessor = blobstore.createBlob()) { int nRead; byte[] data = new byte[BUFFER_SIZE]; while ((nRead = inputStream.read(data, 0, data.length)) != -1) { blobAccessor.write(data, 0, nRead); } return blobAccessor.getBlobId(); } catch (IOException e) { throw new UncheckedIOException(e); } }
private Attachment createAttachment( final Long blobId, final String contentType, final String name) { InputStreamSupplier inputStreamSupplier = null; if (blobId != null) { inputStreamSupplier = () -> { BlobReader readBlob = blobstore.readBlob(blobId); return new BlobInputStream(readBlob); }; } Attachment attachment = new Attachment() .withContentType(contentType) .withName(name) .withInputStreamSupplier(inputStreamSupplier); return attachment; }
private void deleteBlob(final Long blobId) { blobstore.deleteBlob(blobId); }