use of com.facebook.buck.util.hash.HasherOutputStream in project buck by facebook.
the class HttpArtifactCacheBinaryProtocol method createMetadataHeader.
@VisibleForTesting
static byte[] createMetadataHeader(ImmutableSet<RuleKey> ruleKeys, ImmutableMap<String, String> metadata, ByteSource data) throws IOException {
ByteArrayOutputStream rawOut = new ByteArrayOutputStream();
Hasher hasher = HASH_FUNCTION.newHasher();
try (DataOutputStream out = new DataOutputStream(new HasherOutputStream(hasher, rawOut))) {
// Write the rule keys to the raw metadata, including them in the end-to-end checksum.
out.writeInt(ruleKeys.size());
for (RuleKey ruleKey : ruleKeys) {
out.writeUTF(ruleKey.toString());
}
// Write out the metadata map to the raw metadata, including it in the end-to-end checksum.
out.writeInt(metadata.size());
for (Map.Entry<String, String> ent : metadata.entrySet()) {
out.writeUTF(ent.getKey());
byte[] val = ent.getValue().getBytes(Charsets.UTF_8);
out.writeInt(val.length);
out.write(val);
if (out.size() > MAX_METADATA_HEADER_SIZE) {
throw new IOException("Metadata header too big.");
}
}
}
// Add the file data contents to the end-to-end checksum.
data.copyTo(new HasherOutputStream(hasher, ByteStreams.nullOutputStream()));
// Finish the checksum, adding it to the raw metadata
rawOut.write(hasher.hash().asBytes());
// Finally, base64 encode the raw bytes to make usable in a HTTP header.
byte[] bytes = rawOut.toByteArray();
if (bytes.length > MAX_METADATA_HEADER_SIZE) {
throw new IOException("Metadata header too big.");
}
return bytes;
}
Aggregations