本文整理匯總了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)
);
}
}