use of com.google.common.io.ByteSource in project buck by facebook.
the class JarFattener method writeFatJarInfo.
/**
* @return a {@link Step} that generates the fat jar info resource.
*/
private Step writeFatJarInfo(Path destination, final ImmutableMap<String, String> nativeLibraries) {
ByteSource source = new ByteSource() {
@Override
public InputStream openStream() throws IOException {
FatJar fatJar = new FatJar(FAT_JAR_INNER_JAR, nativeLibraries);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
try {
fatJar.store(bytes);
} catch (JAXBException e) {
throw new RuntimeException(e);
}
return new ByteArrayInputStream(bytes.toByteArray());
}
};
return new WriteFileStep(getProjectFilesystem(), source, destination, /* executable */
false);
}
use of com.google.common.io.ByteSource in project buck by facebook.
the class AccumulateClassNamesStep method calculateClassHashes.
/**
* @return an Optional that will be absent if there was an error.
*/
public static Optional<ImmutableSortedMap<String, HashCode>> calculateClassHashes(ExecutionContext context, ProjectFilesystem filesystem, Path path) {
final Map<String, HashCode> classNames = new HashMap<>();
ClasspathTraversal traversal = new ClasspathTraversal(Collections.singleton(path), filesystem) {
@Override
public void visit(final FileLike fileLike) throws IOException {
// end in .class, which should be ignored.
if (!FileLikes.isClassFile(fileLike)) {
return;
}
String key = FileLikes.getFileNameWithoutClassSuffix(fileLike);
ByteSource input = new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return fileLike.getInput();
}
};
HashCode value = input.hash(Hashing.sha1());
HashCode existing = classNames.putIfAbsent(key, value);
if (existing != null && !existing.equals(value)) {
throw new IllegalArgumentException(String.format("Multiple entries with same key but differing values: %1$s=%2$s and %1$s=%3$s", key, value, existing));
}
}
};
try {
new DefaultClasspathTraverser().traverse(traversal);
} catch (IOException e) {
context.logError(e, "Error accumulating class names for %s.", path);
return Optional.empty();
}
return Optional.of(ImmutableSortedMap.copyOf(classNames, Ordering.natural()));
}
use of com.google.common.io.ByteSource in project buck by facebook.
the class HttpArtifactCacheBinaryProtocolTest method testWriteStoreRequest.
@Test
public void testWriteStoreRequest() throws IOException {
final String base64EncodedData = "AAAAAgAgMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAAIDkwMDA" + "wMDAwMDAwMDAwMDAwMDAwMDA4MDAwMDAwMDA1AAAAXgAAAAIAIDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDA" + "wMDAwACA5MDAwMDAwMDAwMDAwMDAwMDAwMDAwODAwMDAwMDAwNQAAAAEAA2tleQAAAAV2YWx1ZRf0zcZkYXRhZGF" + "0YQ==";
final RuleKey ruleKey = new RuleKey("00000000000000000000000000000000");
final RuleKey ruleKey2 = new RuleKey("90000000000000000000008000000005");
HttpArtifactCacheBinaryProtocol.StoreRequest storeRequest = new HttpArtifactCacheBinaryProtocol.StoreRequest(ArtifactInfo.builder().addRuleKeys(ruleKey, ruleKey2).setMetadata(ImmutableMap.of("key", "value")).build(), new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return new ByteArrayInputStream("datadata".getBytes(Charsets.UTF_8));
}
});
assertThat(storeRequest.getContentLength(), Matchers.is(178L));
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
StoreWriteResult writeResult = storeRequest.write(byteArrayOutputStream);
assertThat(writeResult.getArtifactContentHashCode(), Matchers.equalTo(HashCode.fromString("2c0b14a4")));
assertThat(writeResult.getArtifactSizeBytes(), Matchers.is(8L));
byte[] expectedBytes = BaseEncoding.base64().decode(base64EncodedData);
assertThat(byteArrayOutputStream.toByteArray(), Matchers.equalTo(expectedBytes));
}
use of com.google.common.io.ByteSource in project buck by facebook.
the class HttpArtifactCacheBinaryProtocolTest method testFetchResponse.
@Test
public void testFetchResponse() throws IOException {
RuleKey ruleKey = new RuleKey("00000000000000000000000000000000");
RuleKey ruleKey2 = new RuleKey("90000000000000000000008000000005");
final String data = "data";
ImmutableMap<String, String> metadata = ImmutableMap.of("metaKey", "metaValue");
HttpArtifactCacheBinaryProtocol.FetchResponse fetchResponse = new HttpArtifactCacheBinaryProtocol.FetchResponse(ImmutableSet.of(ruleKey, ruleKey2), metadata, new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return new ByteArrayInputStream(data.getBytes(Charsets.UTF_8));
}
});
assertThat(fetchResponse.getContentLength(), Matchers.is(110L));
ByteArrayOutputStream fetchResponseOutputStream = new ByteArrayOutputStream();
fetchResponse.write(fetchResponseOutputStream);
ByteArrayInputStream fetchResponseInputStream = new ByteArrayInputStream(fetchResponseOutputStream.toByteArray());
ByteArrayOutputStream fetchResponsePayload = new ByteArrayOutputStream();
FetchResponseReadResult responseReadResult = HttpArtifactCacheBinaryProtocol.readFetchResponse(new DataInputStream(fetchResponseInputStream), fetchResponsePayload);
assertThat(responseReadResult.getRuleKeys(), Matchers.containsInAnyOrder(ruleKey, ruleKey2));
assertThat(responseReadResult.getMetadata(), Matchers.equalTo(metadata));
assertThat(fetchResponsePayload.toByteArray(), Matchers.equalTo(data.getBytes(Charsets.UTF_8)));
}
use of com.google.common.io.ByteSource in project buck by facebook.
the class XzStepTest method testXzStep.
@Test
public void testXzStep() throws IOException {
final Path sourceFile = TestDataHelper.getTestDataScenario(this, "xz_with_rm_and_check").resolve("xzstep.data");
final File destinationFile = tmp.newFile("xzstep.data.xz");
XzStep step = new XzStep(new ProjectFilesystem(tmp.getRoot().toPath()), sourceFile, destinationFile.toPath(), /* compressionLevel -- for faster testing */
1, /* keep */
true, XZ.CHECK_CRC32);
ExecutionContext context = TestExecutionContext.newInstance();
assertEquals(0, step.execute(context).getExitCode());
ByteSource original = PathByteSource.asByteSource(sourceFile);
ByteSource decompressed = new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return new XZInputStream(new FileInputStream(destinationFile));
}
};
assertTrue("Decompressed file must be identical to original.", original.contentEquals(decompressed));
}
Aggregations