Update the magic number in makedict

Update makedict to write the magic number to the format
expected by LatinIME starting with I3aafdd7e

Bug: 5686638
Change-Id: I78c6befc3347f689a8520b450d589eabefc413d6
main
Jean Chalard 2012-03-06 16:15:42 +09:00
parent 7fc7a76b04
commit 89dfb2bae1
1 changed files with 36 additions and 16 deletions

View File

@ -111,7 +111,8 @@ public class BinaryDictInputOutput {
* *
*/ */
private static final int MAGIC_NUMBER = 0x78B1; private static final int VERSION_1_MAGIC_NUMBER = 0x78B1;
private static final int VERSION_2_MAGIC_NUMBER = 0x9BC13AFE;
private static final int MINIMUM_SUPPORTED_VERSION = 1; private static final int MINIMUM_SUPPORTED_VERSION = 1;
private static final int MAXIMUM_SUPPORTED_VERSION = 2; private static final int MAXIMUM_SUPPORTED_VERSION = 2;
private static final int FIRST_VERSION_WITH_HEADER_SIZE = 2; private static final int FIRST_VERSION_WITH_HEADER_SIZE = 2;
@ -801,8 +802,9 @@ public class BinaryDictInputOutput {
* @param dict the dictionary to write. * @param dict the dictionary to write.
* @param version the version of the format to write, currently either 1 or 2. * @param version the version of the format to write, currently either 1 or 2.
*/ */
public static void writeDictionaryBinary(OutputStream destination, FusionDictionary dict, public static void writeDictionaryBinary(final OutputStream destination,
final int version) throws IOException, UnsupportedFormatException { final FusionDictionary dict, final int version)
throws IOException, UnsupportedFormatException {
// Addresses are limited to 3 bytes, so we'll just make a 16MB buffer. Since addresses // Addresses are limited to 3 bytes, so we'll just make a 16MB buffer. Since addresses
// can be relative to each node, the structure itself is not limited to 16MB at all, but // can be relative to each node, the structure itself is not limited to 16MB at all, but
@ -820,9 +822,18 @@ public class BinaryDictInputOutput {
+ MINIMUM_SUPPORTED_VERSION + " through " + MAXIMUM_SUPPORTED_VERSION); + MINIMUM_SUPPORTED_VERSION + " through " + MAXIMUM_SUPPORTED_VERSION);
} }
// Magic number in big-endian order. // The magic number in big-endian order.
buffer[index++] = (byte) (0xFF & (MAGIC_NUMBER >> 8)); if (version >= FIRST_VERSION_WITH_HEADER_SIZE) {
buffer[index++] = (byte) (0xFF & MAGIC_NUMBER); // Magic number for version 2+.
buffer[index++] = (byte) (0xFF & (VERSION_2_MAGIC_NUMBER >> 24));
buffer[index++] = (byte) (0xFF & (VERSION_2_MAGIC_NUMBER >> 16));
buffer[index++] = (byte) (0xFF & (VERSION_2_MAGIC_NUMBER >> 8));
buffer[index++] = (byte) (0xFF & VERSION_2_MAGIC_NUMBER);
} else {
// Magic number for version 1.
buffer[index++] = (byte) (0xFF & (VERSION_1_MAGIC_NUMBER >> 8));
buffer[index++] = (byte) (0xFF & VERSION_1_MAGIC_NUMBER);
}
// Dictionary version. // Dictionary version.
buffer[index++] = (byte) (0xFF & version); buffer[index++] = (byte) (0xFF & version);
// Options flags // Options flags
@ -831,9 +842,8 @@ public class BinaryDictInputOutput {
if (version >= FIRST_VERSION_WITH_HEADER_SIZE) { if (version >= FIRST_VERSION_WITH_HEADER_SIZE) {
final int headerSizeOffset = index; final int headerSizeOffset = index;
index += 3; // Size of the header size index += 3; // Size of the header size
// TODO: Write out the header contents here.
// Write out the header contents here. // Write out the header size.
buffer[headerSizeOffset] = (byte) (0xFF & (index >> 16)); buffer[headerSizeOffset] = (byte) (0xFF & (index >> 16));
buffer[headerSizeOffset + 1] = (byte) (0xFF & (index >> 8)); buffer[headerSizeOffset + 1] = (byte) (0xFF & (index >> 8));
buffer[headerSizeOffset + 2] = (byte) (0xFF & (index >> 0)); buffer[headerSizeOffset + 2] = (byte) (0xFF & (index >> 0));
@ -1111,6 +1121,17 @@ public class BinaryDictInputOutput {
return node; return node;
} }
/**
* Helper function to test the magic number of the file.
*/
private static boolean isExpectedMagicNumber(final RandomAccessFile source) throws IOException {
final int magic_v1 = source.readUnsignedShort();
if (VERSION_1_MAGIC_NUMBER == magic_v1) return true;
final int magic_v2 = (magic_v1 << 16) + source.readUnsignedShort();
if (VERSION_2_MAGIC_NUMBER == magic_v2) return true;
return false;
}
/** /**
* Reads a random access file and returns the memory representation of the dictionary. * Reads a random access file and returns the memory representation of the dictionary.
* *
@ -1122,11 +1143,10 @@ public class BinaryDictInputOutput {
* @param dict an optional dictionary to add words to, or null. * @param dict an optional dictionary to add words to, or null.
* @return the created (or merged) dictionary. * @return the created (or merged) dictionary.
*/ */
public static FusionDictionary readDictionaryBinary(RandomAccessFile source, public static FusionDictionary readDictionaryBinary(final RandomAccessFile source,
FusionDictionary dict) throws IOException, UnsupportedFormatException { final FusionDictionary dict) throws IOException, UnsupportedFormatException {
// Check magic number // Check magic number
final int magic = source.readUnsignedShort(); if (!isExpectedMagicNumber(source)) {
if (MAGIC_NUMBER != magic) {
throw new UnsupportedFormatException("The magic number in this file does not match " throw new UnsupportedFormatException("The magic number in this file does not match "
+ "the expected value"); + "the expected value");
} }
@ -1146,7 +1166,7 @@ public class BinaryDictInputOutput {
if (version < FIRST_VERSION_WITH_HEADER_SIZE) { if (version < FIRST_VERSION_WITH_HEADER_SIZE) {
headerSize = source.getFilePointer(); headerSize = source.getFilePointer();
} else { } else {
headerSize = source.readUnsignedByte() << 16 + source.readUnsignedByte() << 8 headerSize = (source.readUnsignedByte() << 16) + (source.readUnsignedByte() << 8)
+ source.readUnsignedByte(); + source.readUnsignedByte();
// read the header body // read the header body
source.seek(headerSize); source.seek(headerSize);
@ -1175,10 +1195,10 @@ public class BinaryDictInputOutput {
* @param filename The name of the file to test. * @param filename The name of the file to test.
* @return true if it's a binary dictionary, false otherwise * @return true if it's a binary dictionary, false otherwise
*/ */
public static boolean isBinaryDictionary(String filename) { public static boolean isBinaryDictionary(final String filename) {
try { try {
RandomAccessFile f = new RandomAccessFile(filename, "r"); RandomAccessFile f = new RandomAccessFile(filename, "r");
return MAGIC_NUMBER == f.readUnsignedShort(); return isExpectedMagicNumber(f);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
return false; return false;
} catch (IOException e) { } catch (IOException e) {