use of org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.ByteString in project bazel by bazelbuild.
the class GrpcActionCache method createFileFromStream.
private ContentDigest createFileFromStream(Map<ContentDigest, Pair<Path, FileMetadata>> metadataMap, Iterator<CasDownloadReply> replies) throws IOException, CacheNotFoundException {
Preconditions.checkArgument(replies.hasNext());
CasDownloadReply reply = replies.next();
if (reply.hasStatus()) {
handleDownloadStatus(reply.getStatus());
}
BlobChunk chunk = reply.getData();
ContentDigest digest = chunk.getDigest();
Preconditions.checkArgument(metadataMap.containsKey(digest));
Pair<Path, FileMetadata> metadata = metadataMap.get(digest);
Path path = metadata.first;
FileSystemUtils.createDirectoryAndParents(path.getParentDirectory());
try (OutputStream stream = path.getOutputStream()) {
ByteString data = chunk.getData();
data.writeTo(stream);
long bytesLeft = digest.getSizeBytes() - data.size();
while (bytesLeft > 0) {
Preconditions.checkArgument(replies.hasNext());
reply = replies.next();
if (reply.hasStatus()) {
handleDownloadStatus(reply.getStatus());
}
chunk = reply.getData();
data = chunk.getData();
Preconditions.checkArgument(!chunk.hasDigest());
Preconditions.checkArgument(chunk.getOffset() == digest.getSizeBytes() - bytesLeft);
data.writeTo(stream);
bytesLeft -= data.size();
}
path.setExecutable(metadata.second.getExecutable());
}
return digest;
}
use of org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.ByteString in project bazel by bazelbuild.
the class Digest method fromVirtualActionInput.
/**
* Gets the digest and size of a given VirtualActionInput.
*
* @param input the VirtualActionInput.
* @return the digest and size.
*/
public static Pair<ByteString, Long> fromVirtualActionInput(VirtualActionInput input) throws IOException {
CountingMD5OutputStream md5Stream = new CountingMD5OutputStream();
input.writeTo(md5Stream);
ByteString digest = toByteString(md5Stream.getDigest());
return Pair.of(digest, md5Stream.getSize());
}
use of org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.ByteString in project grpc-java by grpc.
the class TestServiceImpl method createBufferFromFile.
/**
* Creates a buffer with data read from a file.
*/
// Not concerned about suppression; expected to be exceedingly rare
@SuppressWarnings("Finally")
private ByteString createBufferFromFile(String fileClassPath) {
ByteString buffer = ByteString.EMPTY;
InputStream inputStream = getClass().getResourceAsStream(fileClassPath);
if (inputStream == null) {
throw new IllegalArgumentException("Unable to locate file on classpath: " + fileClassPath);
}
try {
buffer = ByteString.readFrom(inputStream);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
inputStream.close();
} catch (IOException ignorable) {
// ignore
}
}
return buffer;
}
use of org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.ByteString in project bazel by bazelbuild.
the class DigestTest method testFromVirtualInput.
@Test
public void testFromVirtualInput() throws Exception {
Pair<ByteString, Long> result = Digest.fromVirtualActionInput(new VirtualActionInput() {
@Override
public void writeTo(OutputStream out) throws IOException {
out.write(UGLY.getBytes(UTF_8));
}
@Override
public String getExecPathString() {
throw new UnsupportedOperationException();
}
@Override
public PathFragment getExecPath() {
throw new UnsupportedOperationException();
}
});
assertEquals(UGLY_DIGEST, result.first.toStringUtf8());
assertEquals(UGLY.length(), result.second.longValue());
}
use of org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.ByteString in project bazel by bazelbuild.
the class SingleBuildFileCacheTest method testUnreadableFileWhenFileSystemSupportsDigest.
@Test
public void testUnreadableFileWhenFileSystemSupportsDigest() throws Exception {
byte[] expectedDigestRaw = MessageDigest.getInstance("md5").digest("randomtext".getBytes(StandardCharsets.UTF_8));
ByteString expectedDigest = ByteString.copyFrom(expectedDigestRaw);
md5Overrides.put("/unreadable", expectedDigestRaw);
ActionInput input = ActionInputHelper.fromPath("/unreadable");
Path file = fs.getPath("/unreadable");
file.getOutputStream().close();
file.chmod(0);
ByteString actualDigest = ByteString.copyFrom(underTest.getDigest(input));
assertThat(expectedDigest).isEqualTo(actualDigest);
}
Aggregations