| // Copyright (c) 2019, the R8 project authors. Please see the AUTHORS file | |
| // for details. All rights reserved. Use of this source code is governed by a | |
| // BSD-style license that can be found in the LICENSE file. | |
| package com.android.tools.r8.utils; | |
| import static com.android.tools.r8.utils.internal.FileUtils.isArchive; | |
| import com.android.tools.r8.DataResourceProvider; | |
| import com.android.tools.r8.ProgramResource; | |
| import com.android.tools.r8.ProgramResource.Kind; | |
| import com.android.tools.r8.ProgramResourceProvider; | |
| import com.android.tools.r8.ResourceException; | |
| import com.android.tools.r8.errors.CompilationError; | |
| import com.android.tools.r8.origin.ArchiveEntryOrigin; | |
| import com.android.tools.r8.origin.Origin; | |
| import com.android.tools.r8.origin.PathOrigin; | |
| import com.android.tools.r8.utils.internal.FileUtils; | |
| import com.google.common.io.ByteStreams; | |
| import java.io.IOException; | |
| import java.io.InputStream; | |
| import java.nio.charset.StandardCharsets; | |
| import java.nio.file.Path; | |
| import java.util.ArrayList; | |
| import java.util.Collection; | |
| import java.util.Collections; | |
| import java.util.Enumeration; | |
| import java.util.List; | |
| import java.util.function.Consumer; | |
| import java.util.zip.ZipEntry; | |
| import java.util.zip.ZipException; | |
| import java.util.zip.ZipFile; | |
| import java.util.zip.ZipInputStream; | |
| public class AarArchiveResourceProvider implements ProgramResourceProvider, DataResourceProvider { | |
| private final Origin classesOrigin; | |
| private final Origin origin; | |
| private final Path archive; | |
| public static AarArchiveResourceProvider fromArchive(Path archive) { | |
| return fromArchive(archive, new PathOrigin(archive)); | |
| } | |
| public static AarArchiveResourceProvider fromArchive(Path archive, Origin origin) { | |
| return new AarArchiveResourceProvider(archive, origin); | |
| } | |
| AarArchiveResourceProvider(Path archive, Origin origin) { | |
| assert isArchive(archive); | |
| this.classesOrigin = new ArchiveEntryOrigin("classes.jar", origin); | |
| this.origin = origin; | |
| this.archive = archive; | |
| } | |
| private void readClassesJar(ZipInputStream stream, Consumer<ProgramResource> consumer) | |
| throws IOException { | |
| ZipEntry entry; | |
| while (null != (entry = stream.getNextEntry())) { | |
| String name = entry.getName(); | |
| if (ZipUtils.isClassFile(name)) { | |
| String descriptor = DescriptorUtils.guessTypeDescriptor(name); | |
| ProgramResource resource = | |
| OneShotByteResource.create( | |
| classesOrigin, | |
| Kind.CF, | |
| ByteStreams.toByteArray(stream), | |
| Collections.singleton(descriptor)); | |
| consumer.accept(resource); | |
| } | |
| } | |
| } | |
| private void readArchive(Consumer<ProgramResource> consumer) throws IOException { | |
| try (ZipFile zipFile = FileUtils.createZipFile(archive.toFile(), StandardCharsets.UTF_8)) { | |
| final Enumeration<? extends ZipEntry> entries = zipFile.entries(); | |
| while (entries.hasMoreElements()) { | |
| ZipEntry entry = entries.nextElement(); | |
| try (InputStream stream = zipFile.getInputStream(entry)) { | |
| String name = entry.getName(); | |
| if (name.equals("classes.jar")) { | |
| try (ZipInputStream classesStream = new ZipInputStream(stream)) { | |
| readClassesJar(classesStream, consumer); | |
| } | |
| break; | |
| } | |
| } | |
| } | |
| } catch (ZipException e) { | |
| throw new CompilationError("Zip error while reading '" + archive + "': " + e.getMessage(), e); | |
| } | |
| } | |
| @Deprecated | |
| @Override | |
| public Collection<ProgramResource> getProgramResources() throws ResourceException { | |
| try { | |
| List<ProgramResource> classResources = new ArrayList<>(); | |
| readArchive(classResources::add); | |
| return classResources; | |
| } catch (IOException e) { | |
| throw new ResourceException(classesOrigin, e); | |
| } | |
| } | |
| @Override | |
| public void getProgramResources(Consumer<ProgramResource> consumer) throws ResourceException { | |
| try { | |
| readArchive(consumer); | |
| } catch (IOException e) { | |
| throw new ResourceException(classesOrigin, e); | |
| } | |
| } | |
| @Override | |
| public DataResourceProvider getDataResourceProvider() { | |
| return this; | |
| } | |
| @Override | |
| public void accept(DataResourceProvider.Visitor visitor) throws ResourceException { | |
| ArchiveResourceProviderUtils.readDataResources( | |
| archive, origin, entry -> entry.getName().equals("proguard.txt"), visitor); | |
| } | |
| } |