From 1d15fe7e51075e77b7cb477cf8de6569b8eefa2f Mon Sep 17 00:00:00 2001 From: Jean Chalard Date: Fri, 18 Jan 2013 16:42:04 +0900 Subject: [PATCH] [AD2] Add a helper method to read an arbitrary dict header Bug: 7702011 Change-Id: Ib88f6dc222892831ae6932635b65fd2595b16b43 --- .../latin/makedict/BinaryDictIOUtils.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/java/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtils.java b/java/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtils.java index ee0e9cd7e..fa5451bad 100644 --- a/java/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtils.java +++ b/java/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtils.java @@ -25,8 +25,12 @@ import com.android.inputmethod.latin.makedict.FormatSpec.FormatOptions; import com.android.inputmethod.latin.makedict.FusionDictionary.CharGroup; import com.android.inputmethod.latin.makedict.FusionDictionary.WeightedString; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.OutputStream; +import java.nio.channels.FileChannel; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; @@ -977,4 +981,27 @@ public final class BinaryDictIOUtils { } return null; } + + /** + * Convenience method to read the header of a binary file. + * + * This is quite resource intensive - don't call when performance is critical. + * + * @param file The file to read. + */ + private static final int HEADER_READING_BUFFER_SIZE = 16384; + public static FileHeader getDictionaryFileHeader(final File file) + throws FileNotFoundException, IOException, UnsupportedFormatException { + final byte[] buffer = new byte[HEADER_READING_BUFFER_SIZE]; + final FileInputStream inStream = new FileInputStream(file); + try { + inStream.read(buffer); + final BinaryDictInputOutput.ByteBufferWrapper wrapper = + new BinaryDictInputOutput.ByteBufferWrapper(inStream.getChannel().map( + FileChannel.MapMode.READ_ONLY, 0, file.length())); + return BinaryDictInputOutput.readHeader(wrapper); + } finally { + inStream.close(); + } + } }