Search in sources :

Example 1 with HashingInputStream

use of com.google.common.hash.HashingInputStream in project buck by facebook.

the class HashingDeterministicJarWriter method writeEntry.

public HashingDeterministicJarWriter writeEntry(String name, InputStream contents) throws IOException {
    try (HashingInputStream hashingContents = new HashingInputStream(Hashing.murmur3_128(), contents)) {
        writeToJar(name, hashingContents);
        manifestWriter.setEntryAttribute(name, DIGEST_ATTRIBUTE_NAME, hashingContents.hash().toString());
    }
    return this;
}
Also used : HashingInputStream(com.google.common.hash.HashingInputStream)

Example 2 with HashingInputStream

use of com.google.common.hash.HashingInputStream in project openscoring by openscoring.

the class ModelRegistry method load.

@SuppressWarnings(value = { "resource" })
public Model load(InputStream is) throws Exception {
    CountingInputStream countingIs = new CountingInputStream(is);
    HashingInputStream hashingIs = new HashingInputStream(Hashing.md5(), countingIs);
    PMML pmml = unmarshal(hashingIs, this.validate);
    this.visitorBattery.applyTo(pmml);
    ModelEvaluatorFactory modelEvaluatorFactory = this.modelEvaluatorFactory;
    Evaluator evaluator = modelEvaluatorFactory.newModelEvaluator(pmml);
    evaluator.verify();
    Model model = new Model(evaluator);
    model.putProperty(Model.PROPERTY_FILE_SIZE, countingIs.getCount());
    model.putProperty(Model.PROPERTY_FILE_MD5SUM, (hashingIs.hash()).toString());
    return model;
}
Also used : CountingInputStream(com.google.common.io.CountingInputStream) PMML(org.dmg.pmml.PMML) HasPMML(org.jpmml.evaluator.HasPMML) ModelEvaluatorFactory(org.jpmml.evaluator.ModelEvaluatorFactory) Evaluator(org.jpmml.evaluator.Evaluator) HashingInputStream(com.google.common.hash.HashingInputStream)

Example 3 with HashingInputStream

use of com.google.common.hash.HashingInputStream in project buck by facebook.

the class HashingDeterministicJarWriterTest method manifestContainsEntryHashesOfHashedEntries.

@Test
public void manifestContainsEntryHashesOfHashedEntries() throws IOException {
    String entryName = "A";
    InputStream contents = new ByteArrayInputStream("contents".getBytes(StandardCharsets.UTF_8));
    try (HashingInputStream hashingContents = new HashingInputStream(Hashing.murmur3_128(), contents)) {
        writer.writeEntry(entryName, hashingContents);
        writer.close();
        try (JarInputStream jar = new JarInputStream(new ByteArrayInputStream(out.toByteArray()))) {
            jar.getNextJarEntry();
            JarEntry manifestEntry = jar.getNextJarEntry();
            assertEquals(JarFile.MANIFEST_NAME, manifestEntry.getName());
            Manifest manifest = new Manifest();
            manifest.read(jar);
            String expectedHash = hashingContents.hash().toString();
            assertEquals(expectedHash, manifest.getEntries().get(entryName).getValue("Murmur3-128-Digest"));
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) JarInputStream(java.util.jar.JarInputStream) HashingInputStream(com.google.common.hash.HashingInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) JarEntry(java.util.jar.JarEntry) Manifest(java.util.jar.Manifest) HashingInputStream(com.google.common.hash.HashingInputStream) Test(org.junit.Test)

Example 4 with HashingInputStream

use of com.google.common.hash.HashingInputStream in project atlas by alibaba.

the class AtlasDesugarTransform method initDesugarJar.

/**
 * Set this location of extracted desugar jar that is used for processing.
 */
private static void initDesugarJar(@Nullable FileCache cache) throws IOException {
    if (isDesugarJarInitialized()) {
        return;
    }
    URL url = DesugarProcessBuilder.class.getClassLoader().getResource(DESUGAR_JAR);
    Preconditions.checkNotNull(url);
    Path extractedDesugar = null;
    if (cache != null) {
        try {
            String fileHash;
            try (HashingInputStream stream = new HashingInputStream(Hashing.sha256(), url.openStream())) {
                fileHash = stream.hash().toString();
            }
            FileCache.Inputs inputs = new FileCache.Inputs.Builder(FileCache.Command.EXTRACT_DESUGAR_JAR).putString("pluginVersion", Version.ANDROID_GRADLE_PLUGIN_VERSION).putString("jarUrl", url.toString()).putString("fileHash", fileHash).build();
            File cachedFile = cache.createFileInCacheIfAbsent(inputs, file -> copyDesugarJar(url, file.toPath())).getCachedFile();
            Preconditions.checkNotNull(cachedFile);
            extractedDesugar = cachedFile.toPath();
        } catch (IOException | ExecutionException e) {
            logger.error(e, "Unable to cache Desugar jar. Extracting to temp dir.");
        }
    }
    synchronized (desugarJar) {
        if (isDesugarJarInitialized()) {
            return;
        }
        if (extractedDesugar == null) {
            extractedDesugar = PathUtils.createTmpToRemoveOnShutdown(DESUGAR_JAR);
            copyDesugarJar(url, extractedDesugar);
        }
        desugarJar.set(extractedDesugar);
    }
}
Also used : Path(java.nio.file.Path) java.util(java.util) SdkConstants(com.android.SdkConstants) DesugarProcessBuilder(com.android.builder.core.DesugarProcessBuilder) URL(java.net.URL) Version(com.android.builder.Version) Hashing(com.google.common.hash.Hashing) HashingInputStream(com.google.common.hash.HashingInputStream) Callable(java.util.concurrent.Callable) PathUtils(com.android.utils.PathUtils) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) StandardCopyOption(java.nio.file.StandardCopyOption) AtlasBuildContext(com.taobao.android.builder.AtlasBuildContext) com.google.common.collect(com.google.common.collect) Path(java.nio.file.Path) LoggerWrapper(com.android.build.gradle.internal.LoggerWrapper) Files(java.nio.file.Files) Nullable(com.android.annotations.Nullable) WaitableExecutor(com.android.ide.common.internal.WaitableExecutor) LoggedProcessOutputHandler(com.android.ide.common.process.LoggedProcessOutputHandler) FileCache(com.android.builder.utils.FileCache) Throwables(com.google.common.base.Throwables) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) TransformManager(com.android.build.gradle.internal.pipeline.TransformManager) ExecutionException(java.util.concurrent.ExecutionException) TransformInputUtil(com.android.build.gradle.internal.transforms.TransformInputUtil) JavaProcessExecutor(com.android.ide.common.process.JavaProcessExecutor) WorkerExecutor(org.gradle.workers.WorkerExecutor) AppVariantOutputContext(com.android.build.gradle.internal.api.AppVariantOutputContext) NonNull(com.android.annotations.NonNull) com.android.build.api.transform(com.android.build.api.transform) DesugarTransform(com.android.build.gradle.internal.transforms.DesugarTransform) Preconditions(com.google.common.base.Preconditions) ProcessException(com.android.ide.common.process.ProcessException) DesugarWorkerItem(com.android.build.gradle.internal.transforms.DesugarWorkerItem) InputStream(java.io.InputStream) DesugarProcessBuilder(com.android.builder.core.DesugarProcessBuilder) IOException(java.io.IOException) URL(java.net.URL) HashingInputStream(com.google.common.hash.HashingInputStream) FileCache(com.android.builder.utils.FileCache) ExecutionException(java.util.concurrent.ExecutionException) File(java.io.File) DesugarProcessBuilder(com.android.builder.core.DesugarProcessBuilder)

Aggregations

HashingInputStream (com.google.common.hash.HashingInputStream)4 InputStream (java.io.InputStream)2 SdkConstants (com.android.SdkConstants)1 NonNull (com.android.annotations.NonNull)1 Nullable (com.android.annotations.Nullable)1 com.android.build.api.transform (com.android.build.api.transform)1 LoggerWrapper (com.android.build.gradle.internal.LoggerWrapper)1 AppVariantOutputContext (com.android.build.gradle.internal.api.AppVariantOutputContext)1 TransformManager (com.android.build.gradle.internal.pipeline.TransformManager)1 DesugarTransform (com.android.build.gradle.internal.transforms.DesugarTransform)1 DesugarWorkerItem (com.android.build.gradle.internal.transforms.DesugarWorkerItem)1 TransformInputUtil (com.android.build.gradle.internal.transforms.TransformInputUtil)1 Version (com.android.builder.Version)1 DesugarProcessBuilder (com.android.builder.core.DesugarProcessBuilder)1 FileCache (com.android.builder.utils.FileCache)1 WaitableExecutor (com.android.ide.common.internal.WaitableExecutor)1 JavaProcessExecutor (com.android.ide.common.process.JavaProcessExecutor)1 LoggedProcessOutputHandler (com.android.ide.common.process.LoggedProcessOutputHandler)1 ProcessException (com.android.ide.common.process.ProcessException)1 PathUtils (com.android.utils.PathUtils)1