summaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorAdrian Velicu <adrianv@google.com>2014-11-11 17:51:29 -0800
committerAdrian Velicu <adrianv@google.com>2014-11-11 18:10:26 -0800
commit9f46834839dadce7fdb8f8c9d6bb43dd8f760dd0 (patch)
tree35a4ca6add9829d97fc2787fccd2d1c7c8ec60a1 /tools
parent1e72f9da12ae7312af0b6ee14114756552cb6576 (diff)
downloadandroid_packages_inputmethods_LatinIME-9f46834839dadce7fdb8f8c9d6bb43dd8f760dd0.tar.gz
android_packages_inputmethods_LatinIME-9f46834839dadce7fdb8f8c9d6bb43dd8f760dd0.tar.bz2
android_packages_inputmethods_LatinIME-9f46834839dadce7fdb8f8c9d6bb43dd8f760dd0.zip
dicttool header to read stream exhaustively
Change-Id: I50a286c115f5bd6e93763bd2f79031676d6fffd8
Diffstat (limited to 'tools')
-rw-r--r--tools/dicttool/src/com/android/inputmethod/latin/dicttool/BinaryDictOffdeviceUtils.java18
1 files changed, 15 insertions, 3 deletions
diff --git a/tools/dicttool/src/com/android/inputmethod/latin/dicttool/BinaryDictOffdeviceUtils.java b/tools/dicttool/src/com/android/inputmethod/latin/dicttool/BinaryDictOffdeviceUtils.java
index 3ec28f313..84c3956f7 100644
--- a/tools/dicttool/src/com/android/inputmethod/latin/dicttool/BinaryDictOffdeviceUtils.java
+++ b/tools/dicttool/src/com/android/inputmethod/latin/dicttool/BinaryDictOffdeviceUtils.java
@@ -186,9 +186,7 @@ public final class BinaryDictOffdeviceUtils {
throw new UnsupportedFormatException("Header too large");
}
final byte[] headerBuffer = new byte[totalHeaderSize - tmpBuffer.length];
- if (headerBuffer.length != input.read(headerBuffer)) {
- throw new UnsupportedFormatException("File shorter than specified in the header");
- }
+ readStreamExhaustively(input, headerBuffer);
final HashMap<String, String> attributes =
BinaryDictDecoderUtils.decodeHeaderAttributes(headerBuffer);
return new DictionaryHeader(totalHeaderSize, new DictionaryOptions(attributes),
@@ -196,6 +194,20 @@ public final class BinaryDictOffdeviceUtils {
}
}
+ private static void readStreamExhaustively(final InputStream inputStream,
+ final byte[] outBuffer) throws IOException, UnsupportedFormatException {
+ int readBytes = 0;
+ int readBytesLastCycle = -1;
+ while (readBytes != outBuffer.length) {
+ readBytesLastCycle = inputStream.read(outBuffer, readBytes,
+ outBuffer.length - readBytes);
+ if (readBytesLastCycle == -1)
+ throw new UnsupportedFormatException("File shorter than specified in the header"
+ + " (expected " + outBuffer.length + ", read " + readBytes + ")");
+ readBytes += readBytesLastCycle;
+ }
+ }
+
public static void copy(final InputStream input, final OutputStream output) throws IOException {
final byte[] buffer = new byte[COPY_BUFFER_SIZE];
for (int readBytes = input.read(buffer); readBytes >= 0; readBytes = input.read(buffer)) {