1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.dex.util;
18 
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.IOException;
22 
23 /**
24  * File I/O utilities.
25  */
26 public final class FileUtils {
FileUtils()27     private FileUtils() {
28     }
29 
30     /**
31      * Reads the named file, translating {@link IOException} to a
32      * {@link RuntimeException} of some sort.
33      *
34      * @param fileName {@code non-null;} name of the file to read
35      * @return {@code non-null;} contents of the file
36      */
readFile(String fileName)37     public static byte[] readFile(String fileName) {
38         File file = new File(fileName);
39         return readFile(file);
40     }
41 
42     /**
43      * Reads the given file, translating {@link IOException} to a
44      * {@link RuntimeException} of some sort.
45      *
46      * @param file {@code non-null;} the file to read
47      * @return {@code non-null;} contents of the file
48      */
readFile(File file)49     public static byte[] readFile(File file) {
50         if (!file.exists()) {
51             throw new RuntimeException(file + ": file not found");
52         }
53 
54         if (!file.isFile()) {
55             throw new RuntimeException(file + ": not a file");
56         }
57 
58         if (!file.canRead()) {
59             throw new RuntimeException(file + ": file not readable");
60         }
61 
62         long longLength = file.length();
63         int length = (int) longLength;
64         if (length != longLength) {
65             throw new RuntimeException(file + ": file too long");
66         }
67 
68         byte[] result = new byte[length];
69 
70         try {
71             FileInputStream in = new FileInputStream(file);
72             int at = 0;
73             while (length > 0) {
74                 int amt = in.read(result, at, length);
75                 if (amt == -1) {
76                     throw new RuntimeException(file + ": unexpected EOF");
77                 }
78                 at += amt;
79                 length -= amt;
80             }
81             in.close();
82         } catch (IOException ex) {
83             throw new RuntimeException(file + ": trouble reading", ex);
84         }
85 
86         return result;
87     }
88 
89     /**
90      * Returns true if {@code fileName} names a .zip, .jar, or .apk.
91      */
hasArchiveSuffix(String fileName)92     public static boolean hasArchiveSuffix(String fileName) {
93         return fileName.endsWith(".zip")
94                 || fileName.endsWith(".jar")
95                 || fileName.endsWith(".apk");
96     }
97 }
98