1917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul/* 2917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Copyright (C) 2007 The Android Open Source Project 3917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 4917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Licensed under the Apache License, Version 2.0 (the "License"); 5917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * you may not use this file except in compliance with the License. 6917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * You may obtain a copy of the License at 7917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 8917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * http://www.apache.org/licenses/LICENSE-2.0 9917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 10917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Unless required by applicable law or agreed to in writing, software 11917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * distributed under the License is distributed on an "AS IS" BASIS, 12917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * See the License for the specific language governing permissions and 14917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * limitations under the License. 15917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 16917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 17917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulpackage com.android.dexgen.util; 18917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 19917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulimport java.io.File; 20917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulimport java.io.FileInputStream; 21917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulimport java.io.IOException; 22917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 23917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul/** 24917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * File I/O utilities. 25917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 26917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulpublic final class FileUtils { 27917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** 28917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * This class is uninstantiable. 29917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 30917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul private FileUtils() { 31917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul // This space intentionally left blank. 32917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 33917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 34917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** 35917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Reads the named file, translating {@link IOException} to a 36917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * {@link RuntimeException} of some sort. 37917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 38917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @param fileName {@code non-null;} name of the file to read 39917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @return {@code non-null;} contents of the file 40917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 41917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul public static byte[] readFile(String fileName) { 42917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul File file = new File(fileName); 43917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul return readFile(file); 44917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 45917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 46917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** 47917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Reads the given file, translating {@link IOException} to a 48917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * {@link RuntimeException} of some sort. 49917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 50917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @param file {@code non-null;} the file to read 51917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @return {@code non-null;} contents of the file 52917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 53917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul public static byte[] readFile(File file) { 54917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul if (!file.exists()) { 55917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul throw new RuntimeException(file + ": file not found"); 56917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 57917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 58917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul if (!file.isFile()) { 59917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul throw new RuntimeException(file + ": not a file"); 60917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 61917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 62917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul if (!file.canRead()) { 63917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul throw new RuntimeException(file + ": file not readable"); 64917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 65917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 66917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul long longLength = file.length(); 67917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul int length = (int) longLength; 68917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul if (length != longLength) { 69917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul throw new RuntimeException(file + ": file too long"); 70917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 71917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 72917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul byte[] result = new byte[length]; 73917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 74917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul try { 75917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul FileInputStream in = new FileInputStream(file); 76917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul int at = 0; 77917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul while (length > 0) { 78917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul int amt = in.read(result, at, length); 79917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul if (amt == -1) { 80917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul throw new RuntimeException(file + ": unexpected EOF"); 81917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 82917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul at += amt; 83917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul length -= amt; 84917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 85917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul in.close(); 86917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } catch (IOException ex) { 87917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul throw new RuntimeException(file + ": trouble reading", ex); 88917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 89917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 90917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul return result; 91917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 92917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul} 93