| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 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 |
} |