Search in sources :

Example 71 with ByteSource

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);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) JAXBException(javax.xml.bind.JAXBException) ByteSource(com.google.common.io.ByteSource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) WriteFileStep(com.facebook.buck.step.fs.WriteFileStep)

Example 72 with ByteSource

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()));
}
Also used : ClasspathTraversal(com.facebook.buck.jvm.java.classes.ClasspathTraversal) HashCode(com.google.common.hash.HashCode) HashMap(java.util.HashMap) DefaultClasspathTraverser(com.facebook.buck.jvm.java.classes.DefaultClasspathTraverser) ByteSource(com.google.common.io.ByteSource) IOException(java.io.IOException) FileLike(com.facebook.buck.jvm.java.classes.FileLike)

Example 73 with ByteSource

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));
}
Also used : RuleKey(com.facebook.buck.rules.RuleKey) DataInputStream(java.io.DataInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteSource(com.google.common.io.ByteSource) Test(org.junit.Test)

Example 74 with ByteSource

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)));
}
Also used : RuleKey(com.facebook.buck.rules.RuleKey) DataInputStream(java.io.DataInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteSource(com.google.common.io.ByteSource) Test(org.junit.Test)

Example 75 with ByteSource

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));
}
Also used : Path(java.nio.file.Path) ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) XZInputStream(org.tukaani.xz.XZInputStream) PathByteSource(com.facebook.buck.io.PathByteSource) ByteSource(com.google.common.io.ByteSource) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Aggregations

ByteSource (com.google.common.io.ByteSource)139 IOException (java.io.IOException)59 Test (org.junit.Test)58 InputStream (java.io.InputStream)42 ByteArrayInputStream (java.io.ByteArrayInputStream)33 File (java.io.File)33 ContentItemImpl (ddf.catalog.content.data.impl.ContentItemImpl)18 Metacard (ddf.catalog.data.Metacard)17 ContentItem (ddf.catalog.content.data.ContentItem)16 StringWriter (java.io.StringWriter)14 FileInputStream (java.io.FileInputStream)13 Test (org.junit.jupiter.api.Test)12 URI (java.net.URI)11 URL (java.net.URL)11 Path (java.nio.file.Path)11 ArrayList (java.util.ArrayList)11 CreateStorageRequestImpl (ddf.catalog.content.operation.impl.CreateStorageRequestImpl)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 TemporaryFileBackedOutputStream (org.codice.ddf.platform.util.TemporaryFileBackedOutputStream)9 FilterInputStream (java.io.FilterInputStream)8