1 // Copyright 2017 The Bazel Authors. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 package com.google.devtools.build.android.desugar.io; 15 16 import com.google.errorprone.annotations.MustBeClosed; 17 import java.io.Closeable; 18 import java.io.IOException; 19 import java.io.InputStream; 20 import java.nio.file.Files; 21 import java.nio.file.Path; 22 import java.util.zip.ZipEntry; 23 24 /** Input file provider allows to iterate on relative path filename of a directory or a jar file. */ 25 public interface InputFileProvider extends Closeable, Iterable<String> { 26 27 /** 28 * Return a ZipEntry for {@code filename}. If the provider is a {@link ZipInputFileProvider}, the 29 * method returns the existing ZipEntry in order to keep its metadata, otherwise a new one is 30 * created. 31 */ getZipEntry(String filename)32 ZipEntry getZipEntry(String filename); 33 34 /** 35 * This method returns an input stream allowing to read the file {@code filename}, it is the 36 * responsibility of the caller to close this stream. 37 */ getInputStream(String filename)38 InputStream getInputStream(String filename) throws IOException; 39 40 /** Transform a Path to an InputFileProvider that needs to be closed by the caller. */ 41 @MustBeClosed open(Path path)42 public static InputFileProvider open(Path path) throws IOException { 43 if (Files.isDirectory(path)) { 44 return new DirectoryInputFileProvider(path); 45 } else { 46 return new ZipInputFileProvider(path); 47 } 48 } 49 } 50