Debian Codegraph @

android/src/com/reecedunn/espeak/FileUtils.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2012-2013 Reece H. Dunn
3 * Copyright (C) 2009 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package com.reecedunn.espeak;
19
20 import java.io.ByteArrayOutputStream;
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26
27 public class FileUtils {
28 public static String read(File file) throws IOException {
29 return readByteArray(new FileInputStream(file), (int)file.length()).toString();
30 }
31
32 public static String read(InputStream stream) throws IOException {
33 return readByteArray(stream, stream.available()).toString();
34 }
35
36 public static String read(InputStream stream, int length) throws IOException {
37 return readByteArray(stream, length).toString();
38 }
39
40 public static byte[] readBinary(File file) throws IOException {
41 return readByteArray(new FileInputStream(file), (int)file.length()).toByteArray();
42 }
43
44 public static byte[] readBinary(InputStream stream) throws IOException {
45 return readByteArray(stream, stream.available()).toByteArray();
46 }
47
48 public static byte[] readBinary(InputStream stream, int length) throws IOException {
49 return readByteArray(stream, length).toByteArray();
50 }
51
52 private static ByteArrayOutputStream readByteArray(InputStream stream, int length) throws IOException {
53 ByteArrayOutputStream content = new ByteArrayOutputStream(length);
54 int c = stream.read();
55 while (c != -1)
56 {
57 content.write((byte)c);
58 c = stream.read();
59 }
60 return content;
61 }
62
63 public static void write(File outputFile, String contents) throws IOException {
64 write(outputFile, contents.getBytes());
65 }
66
67 public static void write(File outputFile, byte[] contents) throws IOException {
68 FileOutputStream outputStream = new FileOutputStream(outputFile);
69 try {
70 outputStream.write(contents, 0, contents.length);
71 } finally {
72 outputStream.close();
73 }
74 }
75
76 public static void rmdir(File directory) {
77 if (!directory.exists() || !directory.isDirectory()) {
78 return;
79 }
80
81 for (File child : directory.listFiles()) {
82 if (child.isDirectory()) {
83 rmdir(child);
84 }
85
86 child.delete();
87 }
88 }
89 }