From dea3ee743fdba31640ee30106a4b3658752f3d57 Mon Sep 17 00:00:00 2001 From: "Reece H. Dunn" Date: Tue, 17 Sep 2013 13:28:59 +0100 Subject: [PATCH] FileUtils: Set the initial size of the ByteArrayOutputStream buffer. --- android/src/com/reecedunn/espeak/CheckVoiceData.java | 2 +- android/src/com/reecedunn/espeak/FileUtils.java | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/android/src/com/reecedunn/espeak/CheckVoiceData.java b/android/src/com/reecedunn/espeak/CheckVoiceData.java index 95d4ed87..08f48119 100755 --- a/android/src/com/reecedunn/espeak/CheckVoiceData.java +++ b/android/src/com/reecedunn/espeak/CheckVoiceData.java @@ -76,7 +76,7 @@ public class CheckVoiceData extends Activity { public static boolean canUpgradeResources(Context context) { try { final String version = FileUtils.read(context.getResources().openRawResource(R.raw.espeakdata_version)); - final String installedVersion = FileUtils.read(new FileInputStream(new File(getDataPath(context), "version"))); + final String installedVersion = FileUtils.read(new File(getDataPath(context), "version")); return !version.equals(installedVersion); } catch (Exception e) { return false; diff --git a/android/src/com/reecedunn/espeak/FileUtils.java b/android/src/com/reecedunn/espeak/FileUtils.java index 278f2b7e..31fb6fed 100644 --- a/android/src/com/reecedunn/espeak/FileUtils.java +++ b/android/src/com/reecedunn/espeak/FileUtils.java @@ -19,13 +19,22 @@ package com.reecedunn.espeak; import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; public class FileUtils { + public static String read(File file) throws IOException { + return read(new FileInputStream(file), (int)file.length()); + } + public static String read(InputStream stream) throws IOException { - ByteArrayOutputStream content = new ByteArrayOutputStream(); + return read(stream, stream.available()); + } + + public static String read(InputStream stream, int length) throws IOException { + ByteArrayOutputStream content = new ByteArrayOutputStream(length); int c = stream.read(); while (c != -1) {