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/*
18 * As per the Apache license requirements, this file has been modified
19 * from its original state.
20 *
21 * Such modifications are Copyright (C) 2010 Ben Gruver, and are released
22 * under the original license
23 */
24
25package org.jf.dexlib.Util;
26
27import java.io.File;
28import java.io.FileInputStream;
29import java.io.IOException;
30import java.io.InputStream;
31
32/**
33 * File I/O utilities.
34 */
35public final class FileUtils {
36    /**
37     * This class is uninstantiable.
38     */
39    private FileUtils() {
40        // This space intentionally left blank.
41    }
42
43    /**
44     * Reads the named file, translating {@link IOException} to a
45     * {@link RuntimeException} of some sort.
46     *
47     * @param fileName non-null; name of the file to read
48     * @return non-null; contents of the file
49     */
50    public static byte[] readFile(String fileName)
51            throws IOException {
52        File file = new File(fileName);
53        return readFile(file);
54    }
55
56    /**
57     * Reads the given file, translating {@link IOException} to a
58     * {@link RuntimeException} of some sort.
59     *
60     * @param file non-null; the file to read
61     * @return non-null; contents of the file
62     */
63    public static byte[] readFile(File file)
64            throws IOException {
65        return readFile(file, 0, -1);
66    }
67
68    /**
69     * Reads the specified block from the given file, translating
70     * {@link IOException} to a {@link RuntimeException} of some sort.
71     *
72     * @param file non-null; the file to read
73     * @param offset the offset to begin reading
74     * @param length the number of bytes to read, or -1 to read to the
75     * end of the file
76     * @return non-null; contents of the file
77     */
78    public static byte[] readFile(File file, int offset, int length)
79            throws IOException {
80        if (!file.exists()) {
81            throw new RuntimeException(file + ": file not found");
82        }
83
84        if (!file.isFile()) {
85            throw new RuntimeException(file + ": not a file");
86        }
87
88        if (!file.canRead()) {
89            throw new RuntimeException(file + ": file not readable");
90        }
91
92        long longLength = file.length();
93        int fileLength = (int) longLength;
94        if (fileLength != longLength) {
95            throw new RuntimeException(file + ": file too long");
96        }
97
98        if (length == -1) {
99            length = fileLength - offset;
100        }
101
102        if (offset + length > fileLength) {
103            throw new RuntimeException(file + ": file too short");
104        }
105
106        FileInputStream in = new FileInputStream(file);
107
108        int at = offset;
109        while(at > 0) {
110            long amt = in.skip(at);
111            if (amt == -1) {
112                throw new RuntimeException(file + ": unexpected EOF");
113            }
114            at -= amt;
115        }
116
117        byte[] result = readStream(in, length);
118
119        in.close();
120
121        return result;
122    }
123
124    public static byte[] readStream(InputStream in, int length)
125            throws IOException {
126        byte[] result = new byte[length];
127        int at=0;
128
129        while (length > 0) {
130            int amt = in.read(result, at, length);
131            if (amt == -1) {
132                throw new RuntimeException("unexpected EOF");
133            }
134            at += amt;
135            length -= amt;
136        }
137
138        return result;
139    }
140}
141