• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2017, the R8 project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4 package com.android.tools.r8.utils;
5 
6 import com.android.tools.r8.CompilationException;
7 import java.io.BufferedReader;
8 import java.io.IOException;
9 import java.nio.charset.StandardCharsets;
10 import java.nio.file.Files;
11 import java.nio.file.Path;
12 import java.util.ArrayList;
13 import java.util.Arrays;
14 import java.util.List;
15 
16 public class FileUtils {
17 
18   public static final String APK_EXTENSION = ".apk";
19   public static final String CLASS_EXTENSION = ".class";
20   public static final String DEX_EXTENSION = ".dex";
21   public static final String JAR_EXTENSION = ".jar";
22   public static final String ZIP_EXTENSION = ".zip";
23   public static final String DEFAULT_DEX_FILENAME = "classes.dex";
24 
isDexFile(Path path)25   public static boolean isDexFile(Path path) {
26     String name = path.getFileName().toString().toLowerCase();
27     return name.endsWith(DEX_EXTENSION);
28   }
29 
isClassFile(Path path)30   public static boolean isClassFile(Path path) {
31     String name = path.getFileName().toString().toLowerCase();
32     return name.endsWith(CLASS_EXTENSION);
33   }
34 
isJarFile(Path path)35   public static boolean isJarFile(Path path) {
36     String name = path.getFileName().toString().toLowerCase();
37     return name.endsWith(JAR_EXTENSION);
38   }
39 
isZipFile(Path path)40   public static boolean isZipFile(Path path) {
41     String name = path.getFileName().toString().toLowerCase();
42     return name.endsWith(ZIP_EXTENSION);
43   }
44 
isApkFile(Path path)45   public static boolean isApkFile(Path path) {
46     String name = path.getFileName().toString().toLowerCase();
47     return name.endsWith(APK_EXTENSION);
48   }
49 
isArchive(Path path)50   public static boolean isArchive(Path path) {
51     String name = path.getFileName().toString().toLowerCase();
52     return name.endsWith(APK_EXTENSION)
53         || name.endsWith(JAR_EXTENSION)
54         || name.endsWith(ZIP_EXTENSION);
55   }
56 
readTextFile(Path file)57   public static List<String> readTextFile(Path file) throws IOException {
58     try (BufferedReader reader = Files.newBufferedReader(file, StandardCharsets.UTF_8)) {
59       List<String> result = new ArrayList<>();
60       String line;
61       while ((line = reader.readLine()) != null) {
62         result.add(line);
63       }
64       return result;
65     }
66   }
67 
writeTextFile(Path file, List<String> lines)68   public static void writeTextFile(Path file, List<String> lines) throws IOException {
69     Files.write(file, lines);
70   }
71 
writeTextFile(Path file, String... lines)72   public static void writeTextFile(Path file, String... lines) throws IOException {
73     Files.write(file, Arrays.asList(lines));
74   }
75 
validateOutputFile(Path path)76   public static Path validateOutputFile(Path path) throws CompilationException {
77     if (path != null) {
78       boolean isJarOrZip = isZipFile(path) || isJarFile(path);
79       if (!isJarOrZip  && !(Files.exists(path) && Files.isDirectory(path))) {
80         throw new CompilationException(
81             "Invalid output: "
82                 + path +
83                 "\nOutput must be a .zip or .jar archive or an existing directory");
84       }
85     }
86     return path;
87   }
88 }
89