[AD2] Add a helper method to read an arbitrary dict header

Bug: 7702011
Change-Id: Ib88f6dc222892831ae6932635b65fd2595b16b43
This commit is contained in:
Jean Chalard 2013-01-18 16:42:04 +09:00
parent babc71ee30
commit 1d15fe7e51

View File

@ -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();
}
}
}