Search in sources :

Example 36 with CloseShieldInputStream

use of org.apache.commons.io.input.CloseShieldInputStream in project gradle by gradle.

the class TarBuildCacheEntryPacker method unpack.

private UnpackResult unpack(CacheableEntity entity, TarArchiveInputStream tarInput, OriginReader readOriginAction) throws IOException {
    ImmutableMap.Builder<String, CacheableTree> treesBuilder = ImmutableMap.builder();
    entity.visitOutputTrees((name, type, root) -> treesBuilder.put(name, new CacheableTree(type, root)));
    ImmutableMap<String, CacheableTree> treesByName = treesBuilder.build();
    TarArchiveEntry tarEntry;
    OriginMetadata originMetadata = null;
    Map<String, FileSystemLocationSnapshot> snapshots = new HashMap<>();
    tarEntry = tarInput.getNextTarEntry();
    AtomicLong entries = new AtomicLong();
    while (tarEntry != null) {
        entries.incrementAndGet();
        String path = tarEntry.getName();
        if (path.equals(METADATA_PATH)) {
            // handle origin metadata
            originMetadata = readOriginAction.execute(new CloseShieldInputStream(tarInput));
            tarEntry = tarInput.getNextTarEntry();
        } else {
            // handle tree
            Matcher matcher = TREE_PATH.matcher(path);
            if (!matcher.matches()) {
                throw new IllegalStateException("Cached entry format error, invalid contents: " + path);
            }
            String treeName = unescape(matcher.group(2));
            CacheableTree tree = treesByName.get(treeName);
            if (tree == null) {
                throw new IllegalStateException(String.format("No tree '%s' registered", treeName));
            }
            boolean missing = matcher.group(1) != null;
            String childPath = matcher.group(3);
            tarEntry = unpackTree(treeName, tree.getType(), tree.getRoot(), tarInput, tarEntry, childPath, missing, snapshots, entries);
        }
    }
    if (originMetadata == null) {
        throw new IllegalStateException("Cached result format error, no origin metadata was found.");
    }
    return new UnpackResult(originMetadata, entries.get(), snapshots);
}
Also used : HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) FileSystemLocationSnapshot(org.gradle.internal.snapshot.FileSystemLocationSnapshot) OriginMetadata(org.gradle.caching.internal.origin.OriginMetadata) ImmutableMap(com.google.common.collect.ImmutableMap) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) AtomicLong(java.util.concurrent.atomic.AtomicLong) CloseShieldInputStream(org.apache.commons.io.input.CloseShieldInputStream)

Aggregations

CloseShieldInputStream (org.apache.commons.io.input.CloseShieldInputStream)36 XHTMLContentHandler (org.apache.tika.sax.XHTMLContentHandler)13 TikaException (org.apache.tika.exception.TikaException)12 InputStream (java.io.InputStream)8 OfflineContentHandler (org.apache.tika.sax.OfflineContentHandler)8 TikaInputStream (org.apache.tika.io.TikaInputStream)7 AutoDetectReader (org.apache.tika.detect.AutoDetectReader)6 MediaType (org.apache.tika.mime.MediaType)6 EmbeddedContentHandler (org.apache.tika.sax.EmbeddedContentHandler)5 SAXException (org.xml.sax.SAXException)5 BufferedInputStream (java.io.BufferedInputStream)4 Charset (java.nio.charset.Charset)4 ZipArchiveEntry (org.apache.commons.compress.archivers.zip.ZipArchiveEntry)4 TikaConfig (org.apache.tika.config.TikaConfig)4 Matcher (java.util.regex.Matcher)3 ArchiveEntry (org.apache.commons.compress.archivers.ArchiveEntry)3 ZipArchiveInputStream (org.apache.commons.compress.archivers.zip.ZipArchiveInputStream)3 EmbeddedDocumentExtractor (org.apache.tika.extractor.EmbeddedDocumentExtractor)3 Metadata (org.apache.tika.metadata.Metadata)3 TaggedContentHandler (org.apache.tika.sax.TaggedContentHandler)3