blob: e54cfe03e3ec9bd8b2baa883aaea26d4fa9ac2f0 [file] [log] [blame]
// Copyright 2020 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.android.r8;
import com.google.common.base.Ascii;
import java.nio.file.Path;
class FileUtils {
public static final String AAR_EXTENSION = ".aar";
public static final String APK_EXTENSION = ".apk";
public static final String CLASS_EXTENSION = ".class";
public static final String DEX_EXTENSION = ".dex";
public static final String JAR_EXTENSION = ".jar";
public static final String ZIP_EXTENSION = ".zip";
public static final String MODULE_INFO_CLASS = "module-info.class";
public static final String META_INF = "meta-inf";
private static boolean hasExtension(String name, String extension) {
return Ascii.toLowerCase(name).endsWith(extension);
}
private static boolean hasExtension(Path path, String extension) {
return hasExtension(path.getFileName().toString(), extension);
}
static boolean isDexFile(Path path) {
return hasExtension(path, DEX_EXTENSION);
}
static boolean isClassFile(String name) {
name = Ascii.toLowerCase(name);
// Android does not support Java 9 module, thus skip module-info.
if (name.equals(MODULE_INFO_CLASS)) {
return false;
}
if (name.startsWith(META_INF) || name.startsWith("/" + META_INF)) {
return false;
}
return name.endsWith(CLASS_EXTENSION);
}
static boolean isClassFile(Path path) {
return isClassFile(path.getFileName().toString());
}
static boolean isJarFile(String name) {
return hasExtension(name, JAR_EXTENSION);
}
static boolean isJarFile(Path path) {
return hasExtension(path, JAR_EXTENSION);
}
static boolean isZipFile(String name) {
return hasExtension(name, ZIP_EXTENSION);
}
static boolean isZipFile(Path path) {
return hasExtension(path, ZIP_EXTENSION);
}
static boolean isApkFile(String name) {
return hasExtension(name, APK_EXTENSION);
}
static boolean isApkFile(Path path) {
return hasExtension(path, APK_EXTENSION);
}
static boolean isAarFile(String name) {
return hasExtension(name, AAR_EXTENSION);
}
static boolean isAarFile(Path path) {
return hasExtension(path, AAR_EXTENSION);
}
static boolean isArchive(Path path) {
return isApkFile(path) || isJarFile(path) || isZipFile(path) || isAarFile(path);
}
private FileUtils() {}
}