From c3f2f7b93b3fd8b2eaff4942f323f60aa4548493 Mon Sep 17 00:00:00 2001 From: Raph Levien Date: Mon, 6 Jul 2015 09:01:36 -0700 Subject: [PATCH] Clean up Hyphenator file reading The old code made invalid assumptions about read() behavior and failed to clean up properly in case of error. This patch changes the file reading to use IoUtils.readFileAsString, which handles that. Bug: 21020457 Change-Id: If9b54955c20a20a4ed5e8381d0c7e9c920d0639a --- core/java/android/text/Hyphenator.java | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/core/java/android/text/Hyphenator.java b/core/java/android/text/Hyphenator.java index 67c36e362eb98..1ee382735d53b 100644 --- a/core/java/android/text/Hyphenator.java +++ b/core/java/android/text/Hyphenator.java @@ -21,9 +21,10 @@ import com.android.internal.annotations.GuardedBy; import android.annotation.Nullable; import android.util.Log; +import libcore.io.IoUtils; + import java.io.File; import java.io.IOException; -import java.io.RandomAccessFile; import java.util.HashMap; import java.util.Locale; @@ -42,9 +43,9 @@ public class Hyphenator { private final static Object sLock = new Object(); @GuardedBy("sLock") - static HashMap sMap = new HashMap(); + final static HashMap sMap = new HashMap(); - private long mNativePtr; + final private long mNativePtr; private Hyphenator(long nativePtr) { mNativePtr = nativePtr; @@ -90,17 +91,13 @@ public class Hyphenator { String patternFilename = "hyph-"+languageTag.toLowerCase(Locale.US)+".pat.txt"; File patternFile = new File(getSystemHyphenatorLocation(), patternFilename); try { - RandomAccessFile rf = new RandomAccessFile(patternFile, "r"); - byte[] buf = new byte[(int)rf.length()]; - rf.read(buf); - rf.close(); - String patternData = new String(buf); + String patternData = IoUtils.readFileAsString(patternFile.getAbsolutePath()); long nativePtr = StaticLayout.nLoadHyphenator(patternData); return new Hyphenator(nativePtr); } catch (IOException e) { Log.e(TAG, "error loading hyphenation " + patternFile, e); + return null; } - return null; } private static File getSystemHyphenatorLocation() {