本文整理汇总了Java中com.google.common.io.Files.asByteSink方法的典型用法代码示例。如果您正苦于以下问题:Java Files.asByteSink方法的具体用法?Java Files.asByteSink怎么用?Java Files.asByteSink使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.common.io.Files
的用法示例。
在下文中一共展示了Files.asByteSink方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: downloadProductImage
import com.google.common.io.Files; //导入方法依赖的package包/类
@Test
public void downloadProductImage() throws Exception {
when(productImageSeeker.seekProductImage(anyLong()))
.thenReturn(Resources.getResource("Large_Scaled_Forest_Lizard.jpg").openStream());
AtomicBoolean completed = new AtomicBoolean(false);
AtomicBoolean error = new AtomicBoolean(false);
File imageFile = File.createTempFile("image", ".jpg");
imageFile.deleteOnExit();
Files.touch(imageFile);
ByteSink byteSink = Files.asByteSink(imageFile, FileWriteMode.APPEND);
StreamObserver<DataChunk> streamObserver = new StreamObserver<DataChunk>() {
@Override
public void onNext(DataChunk dataChunk) {
try {
byteSink.write(dataChunk.getData().toByteArray());
} catch (IOException e) {
log.error("error on write files", e);
onError(e);
}
}
@Override
public void onError(Throwable t) {
error.compareAndSet(false, true);
}
@Override
public void onCompleted() {
log.info("write image to {}", imageFile.getAbsoluteFile());
completed.compareAndSet(false, true);
}
};
stub.downloadProductImage(DownloadProductImageRequest.getDefaultInstance(), streamObserver);
while (!completed.get() && !error.get()) {
Thread.sleep(500);
}
assertThat(completed.get()).isTrue();
assertThat(error.get()).isFalse();
try (InputStream destImageStream = new FileInputStream(imageFile);
InputStream origImageStream = Resources.getResource("Large_Scaled_Forest_Lizard.jpg").openStream()) {
assertThat(DigestUtils.md5Hex(destImageStream)).isEqualTo(
DigestUtils.md5Hex(origImageStream)
);
}
}