Add the ability to import an eSpeak dictionary file from the SD card.

This commit is contained in:
Reece H. Dunn
2013-09-17 13:41:01 +01:00
parent dea3ee743f
commit 9c00360347
6 changed files with 239 additions and 4 deletions
@@ -26,14 +26,30 @@ import java.io.InputStream;
public class FileUtils {
public static String read(File file) throws IOException {
return read(new FileInputStream(file), (int)file.length());
return readByteArray(new FileInputStream(file), (int)file.length()).toString();
}
public static String read(InputStream stream) throws IOException {
return read(stream, stream.available());
return readByteArray(stream, stream.available()).toString();
}
public static String read(InputStream stream, int length) throws IOException {
return readByteArray(stream, length).toString();
}
public static byte[] readBinary(File file) throws IOException {
return readByteArray(new FileInputStream(file), (int)file.length()).toByteArray();
}
public static byte[] readBinary(InputStream stream) throws IOException {
return readByteArray(stream, stream.available()).toByteArray();
}
public static byte[] readBinary(InputStream stream, int length) throws IOException {
return readByteArray(stream, length).toByteArray();
}
private static ByteArrayOutputStream readByteArray(InputStream stream, int length) throws IOException {
ByteArrayOutputStream content = new ByteArrayOutputStream(length);
int c = stream.read();
while (c != -1)
@@ -41,13 +57,17 @@ public class FileUtils {
content.write((byte)c);
c = stream.read();
}
return content.toString();
return content;
}
public static void write(File outputFile, String contents) throws IOException {
write(outputFile, contents.getBytes());
}
public static void write(File outputFile, byte[] contents) throws IOException {
FileOutputStream outputStream = new FileOutputStream(outputFile);
try {
outputStream.write(contents.getBytes(), 0, contents.length());
outputStream.write(contents, 0, contents.length);
} finally {
outputStream.close();
}