Home | History | Annotate | Download | only in makedict
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.inputmethod.latin.makedict;
     18 
     19 import com.android.inputmethod.annotations.UsedForTesting;
     20 import com.android.inputmethod.latin.makedict.FormatSpec.FileHeader;
     21 import com.android.inputmethod.latin.makedict.FormatSpec.FormatOptions;
     22 import com.android.inputmethod.latin.makedict.FusionDictionary.CharGroup;
     23 import com.android.inputmethod.latin.makedict.FusionDictionary.DictionaryOptions;
     24 import com.android.inputmethod.latin.makedict.FusionDictionary.Node;
     25 import com.android.inputmethod.latin.makedict.FusionDictionary.WeightedString;
     26 
     27 import java.io.ByteArrayOutputStream;
     28 import java.io.File;
     29 import java.io.FileInputStream;
     30 import java.io.FileNotFoundException;
     31 import java.io.IOException;
     32 import java.io.OutputStream;
     33 import java.nio.ByteBuffer;
     34 import java.nio.channels.FileChannel;
     35 import java.util.ArrayList;
     36 import java.util.Arrays;
     37 import java.util.HashMap;
     38 import java.util.Iterator;
     39 import java.util.Map;
     40 import java.util.TreeMap;
     41 
     42 /**
     43  * Reads and writes XML files for a FusionDictionary.
     44  *
     45  * All the methods in this class are static.
     46  */
     47 public final class BinaryDictInputOutput {
     48 
     49     private static final boolean DBG = MakedictLog.DBG;
     50 
     51     // Arbitrary limit to how much passes we consider address size compression should
     52     // terminate in. At the time of this writing, our largest dictionary completes
     53     // compression in five passes.
     54     // If the number of passes exceeds this number, makedict bails with an exception on
     55     // suspicion that a bug might be causing an infinite loop.
     56     private static final int MAX_PASSES = 24;
     57     private static final int MAX_JUMPS = 12;
     58 
     59     @UsedForTesting
     60     public interface FusionDictionaryBufferInterface {
     61         public int readUnsignedByte();
     62         public int readUnsignedShort();
     63         public int readUnsignedInt24();
     64         public int readInt();
     65         public int position();
     66         public void position(int newPosition);
     67         public void put(final byte b);
     68         public int limit();
     69         public int capacity();
     70     }
     71 
     72     public static final class ByteBufferWrapper implements FusionDictionaryBufferInterface {
     73         private ByteBuffer mBuffer;
     74 
     75         public ByteBufferWrapper(final ByteBuffer buffer) {
     76             mBuffer = buffer;
     77         }
     78 
     79         @Override
     80         public int readUnsignedByte() {
     81             return mBuffer.get() & 0xFF;
     82         }
     83 
     84         @Override
     85         public int readUnsignedShort() {
     86             return mBuffer.getShort() & 0xFFFF;
     87         }
     88 
     89         @Override
     90         public int readUnsignedInt24() {
     91             final int retval = readUnsignedByte();
     92             return (retval << 16) + readUnsignedShort();
     93         }
     94 
     95         @Override
     96         public int readInt() {
     97             return mBuffer.getInt();
     98         }
     99 
    100         @Override
    101         public int position() {
    102             return mBuffer.position();
    103         }
    104 
    105         @Override
    106         public void position(int newPos) {
    107             mBuffer.position(newPos);
    108         }
    109 
    110         @Override
    111         public void put(final byte b) {
    112             mBuffer.put(b);
    113         }
    114 
    115         @Override
    116         public int limit() {
    117             return mBuffer.limit();
    118         }
    119 
    120         @Override
    121         public int capacity() {
    122             return mBuffer.capacity();
    123         }
    124     }
    125 
    126     /**
    127      * A class grouping utility function for our specific character encoding.
    128      */
    129     static final class CharEncoding {
    130         private static final int MINIMAL_ONE_BYTE_CHARACTER_VALUE = 0x20;
    131         private static final int MAXIMAL_ONE_BYTE_CHARACTER_VALUE = 0xFF;
    132 
    133         /**
    134          * Helper method to find out whether this code fits on one byte
    135          */
    136         private static boolean fitsOnOneByte(final int character) {
    137             return character >= MINIMAL_ONE_BYTE_CHARACTER_VALUE
    138                     && character <= MAXIMAL_ONE_BYTE_CHARACTER_VALUE;
    139         }
    140 
    141         /**
    142          * Compute the size of a character given its character code.
    143          *
    144          * Char format is:
    145          * 1 byte = bbbbbbbb match
    146          * case 000xxxxx: xxxxx << 16 + next byte << 8 + next byte
    147          * else: if 00011111 (= 0x1F) : this is the terminator. This is a relevant choice because
    148          *       unicode code points range from 0 to 0x10FFFF, so any 3-byte value starting with
    149          *       00011111 would be outside unicode.
    150          * else: iso-latin-1 code
    151          * This allows for the whole unicode range to be encoded, including chars outside of
    152          * the BMP. Also everything in the iso-latin-1 charset is only 1 byte, except control
    153          * characters which should never happen anyway (and still work, but take 3 bytes).
    154          *
    155          * @param character the character code.
    156          * @return the size in binary encoded-form, either 1 or 3 bytes.
    157          */
    158         static int getCharSize(final int character) {
    159             // See char encoding in FusionDictionary.java
    160             if (fitsOnOneByte(character)) return 1;
    161             if (FormatSpec.INVALID_CHARACTER == character) return 1;
    162             return 3;
    163         }
    164 
    165         /**
    166          * Compute the byte size of a character array.
    167          */
    168         private static int getCharArraySize(final int[] chars) {
    169             int size = 0;
    170             for (int character : chars) size += getCharSize(character);
    171             return size;
    172         }
    173 
    174         /**
    175          * Writes a char array to a byte buffer.
    176          *
    177          * @param codePoints the code point array to write.
    178          * @param buffer the byte buffer to write to.
    179          * @param index the index in buffer to write the character array to.
    180          * @return the index after the last character.
    181          */
    182         private static int writeCharArray(final int[] codePoints, final byte[] buffer, int index) {
    183             for (int codePoint : codePoints) {
    184                 if (1 == getCharSize(codePoint)) {
    185                     buffer[index++] = (byte)codePoint;
    186                 } else {
    187                     buffer[index++] = (byte)(0xFF & (codePoint >> 16));
    188                     buffer[index++] = (byte)(0xFF & (codePoint >> 8));
    189                     buffer[index++] = (byte)(0xFF & codePoint);
    190                 }
    191             }
    192             return index;
    193         }
    194 
    195         /**
    196          * Writes a string with our character format to a byte buffer.
    197          *
    198          * This will also write the terminator byte.
    199          *
    200          * @param buffer the byte buffer to write to.
    201          * @param origin the offset to write from.
    202          * @param word the string to write.
    203          * @return the size written, in bytes.
    204          */
    205         private static int writeString(final byte[] buffer, final int origin,
    206                 final String word) {
    207             final int length = word.length();
    208             int index = origin;
    209             for (int i = 0; i < length; i = word.offsetByCodePoints(i, 1)) {
    210                 final int codePoint = word.codePointAt(i);
    211                 if (1 == getCharSize(codePoint)) {
    212                     buffer[index++] = (byte)codePoint;
    213                 } else {
    214                     buffer[index++] = (byte)(0xFF & (codePoint >> 16));
    215                     buffer[index++] = (byte)(0xFF & (codePoint >> 8));
    216                     buffer[index++] = (byte)(0xFF & codePoint);
    217                 }
    218             }
    219             buffer[index++] = FormatSpec.GROUP_CHARACTERS_TERMINATOR;
    220             return index - origin;
    221         }
    222 
    223         /**
    224          * Writes a string with our character format to a ByteArrayOutputStream.
    225          *
    226          * This will also write the terminator byte.
    227          *
    228          * @param buffer the ByteArrayOutputStream to write to.
    229          * @param word the string to write.
    230          */
    231         private static void writeString(final ByteArrayOutputStream buffer, final String word) {
    232             final int length = word.length();
    233             for (int i = 0; i < length; i = word.offsetByCodePoints(i, 1)) {
    234                 final int codePoint = word.codePointAt(i);
    235                 if (1 == getCharSize(codePoint)) {
    236                     buffer.write((byte) codePoint);
    237                 } else {
    238                     buffer.write((byte) (0xFF & (codePoint >> 16)));
    239                     buffer.write((byte) (0xFF & (codePoint >> 8)));
    240                     buffer.write((byte) (0xFF & codePoint));
    241                 }
    242             }
    243             buffer.write(FormatSpec.GROUP_CHARACTERS_TERMINATOR);
    244         }
    245 
    246         /**
    247          * Reads a string from a buffer. This is the converse of the above method.
    248          */
    249         private static String readString(final FusionDictionaryBufferInterface buffer) {
    250             final StringBuilder s = new StringBuilder();
    251             int character = readChar(buffer);
    252             while (character != FormatSpec.INVALID_CHARACTER) {
    253                 s.appendCodePoint(character);
    254                 character = readChar(buffer);
    255             }
    256             return s.toString();
    257         }
    258 
    259         /**
    260          * Reads a character from the buffer.
    261          *
    262          * This follows the character format documented earlier in this source file.
    263          *
    264          * @param buffer the buffer, positioned over an encoded character.
    265          * @return the character code.
    266          */
    267         static int readChar(final FusionDictionaryBufferInterface buffer) {
    268             int character = buffer.readUnsignedByte();
    269             if (!fitsOnOneByte(character)) {
    270                 if (FormatSpec.GROUP_CHARACTERS_TERMINATOR == character) {
    271                     return FormatSpec.INVALID_CHARACTER;
    272                 }
    273                 character <<= 16;
    274                 character += buffer.readUnsignedShort();
    275             }
    276             return character;
    277         }
    278     }
    279 
    280     /**
    281      * Compute the binary size of the character array.
    282      *
    283      * If only one character, this is the size of this character. If many, it's the sum of their
    284      * sizes + 1 byte for the terminator.
    285      *
    286      * @param characters the character array
    287      * @return the size of the char array, including the terminator if any
    288      */
    289     static int getGroupCharactersSize(final int[] characters) {
    290         int size = CharEncoding.getCharArraySize(characters);
    291         if (characters.length > 1) size += FormatSpec.GROUP_TERMINATOR_SIZE;
    292         return size;
    293     }
    294 
    295     /**
    296      * Compute the binary size of the character array in a group
    297      *
    298      * If only one character, this is the size of this character. If many, it's the sum of their
    299      * sizes + 1 byte for the terminator.
    300      *
    301      * @param group the group
    302      * @return the size of the char array, including the terminator if any
    303      */
    304     private static int getGroupCharactersSize(final CharGroup group) {
    305         return getGroupCharactersSize(group.mChars);
    306     }
    307 
    308     /**
    309      * Compute the binary size of the group count
    310      * @param count the group count
    311      * @return the size of the group count, either 1 or 2 bytes.
    312      */
    313     public static int getGroupCountSize(final int count) {
    314         if (FormatSpec.MAX_CHARGROUPS_FOR_ONE_BYTE_CHARGROUP_COUNT >= count) {
    315             return 1;
    316         } else if (FormatSpec.MAX_CHARGROUPS_IN_A_NODE >= count) {
    317             return 2;
    318         } else {
    319             throw new RuntimeException("Can't have more than "
    320                     + FormatSpec.MAX_CHARGROUPS_IN_A_NODE + " groups in a node (found " + count
    321                     + ")");
    322         }
    323     }
    324 
    325     /**
    326      * Compute the binary size of the group count for a node
    327      * @param node the node
    328      * @return the size of the group count, either 1 or 2 bytes.
    329      */
    330     private static int getGroupCountSize(final Node node) {
    331         return getGroupCountSize(node.mData.size());
    332     }
    333 
    334     /**
    335      * Compute the size of a shortcut in bytes.
    336      */
    337     private static int getShortcutSize(final WeightedString shortcut) {
    338         int size = FormatSpec.GROUP_ATTRIBUTE_FLAGS_SIZE;
    339         final String word = shortcut.mWord;
    340         final int length = word.length();
    341         for (int i = 0; i < length; i = word.offsetByCodePoints(i, 1)) {
    342             final int codePoint = word.codePointAt(i);
    343             size += CharEncoding.getCharSize(codePoint);
    344         }
    345         size += FormatSpec.GROUP_TERMINATOR_SIZE;
    346         return size;
    347     }
    348 
    349     /**
    350      * Compute the size of a shortcut list in bytes.
    351      *
    352      * This is known in advance and does not change according to position in the file
    353      * like address lists do.
    354      */
    355     static int getShortcutListSize(final ArrayList<WeightedString> shortcutList) {
    356         if (null == shortcutList) return 0;
    357         int size = FormatSpec.GROUP_SHORTCUT_LIST_SIZE_SIZE;
    358         for (final WeightedString shortcut : shortcutList) {
    359             size += getShortcutSize(shortcut);
    360         }
    361         return size;
    362     }
    363 
    364     /**
    365      * Compute the maximum size of a CharGroup, assuming 3-byte addresses for everything.
    366      *
    367      * @param group the CharGroup to compute the size of.
    368      * @param options file format options.
    369      * @return the maximum size of the group.
    370      */
    371     private static int getCharGroupMaximumSize(final CharGroup group, final FormatOptions options) {
    372         int size = getGroupHeaderSize(group, options);
    373         // If terminal, one byte for the frequency
    374         if (group.isTerminal()) size += FormatSpec.GROUP_FREQUENCY_SIZE;
    375         size += FormatSpec.GROUP_MAX_ADDRESS_SIZE; // For children address
    376         size += getShortcutListSize(group.mShortcutTargets);
    377         if (null != group.mBigrams) {
    378             size += (FormatSpec.GROUP_ATTRIBUTE_FLAGS_SIZE
    379                     + FormatSpec.GROUP_ATTRIBUTE_MAX_ADDRESS_SIZE)
    380                     * group.mBigrams.size();
    381         }
    382         return size;
    383     }
    384 
    385     /**
    386      * Compute the maximum size of a node, assuming 3-byte addresses for everything, and caches
    387      * it in the 'actualSize' member of the node.
    388      *
    389      * @param node the node to compute the maximum size of.
    390      * @param options file format options.
    391      */
    392     private static void setNodeMaximumSize(final Node node, final FormatOptions options) {
    393         int size = getGroupCountSize(node);
    394         for (CharGroup g : node.mData) {
    395             final int groupSize = getCharGroupMaximumSize(g, options);
    396             g.mCachedSize = groupSize;
    397             size += groupSize;
    398         }
    399         if (options.mSupportsDynamicUpdate) {
    400             size += FormatSpec.FORWARD_LINK_ADDRESS_SIZE;
    401         }
    402         node.mCachedSize = size;
    403     }
    404 
    405     /**
    406      * Helper method to hide the actual value of the no children address.
    407      */
    408     public static boolean hasChildrenAddress(final int address) {
    409         return FormatSpec.NO_CHILDREN_ADDRESS != address;
    410     }
    411 
    412     /**
    413      * Helper method to check whether the group is moved.
    414      */
    415     public static boolean isMovedGroup(final int flags, final FormatOptions options) {
    416         return options.mSupportsDynamicUpdate
    417                 && ((flags & FormatSpec.MASK_GROUP_ADDRESS_TYPE) == FormatSpec.FLAG_IS_MOVED);
    418     }
    419 
    420     /**
    421      * Helper method to check whether the group is deleted.
    422      */
    423     public static boolean isDeletedGroup(final int flags, final FormatOptions formatOptions) {
    424         return formatOptions.mSupportsDynamicUpdate
    425                 && ((flags & FormatSpec.MASK_GROUP_ADDRESS_TYPE) == FormatSpec.FLAG_IS_DELETED);
    426     }
    427 
    428     /**
    429      * Helper method to check whether the dictionary can be updated dynamically.
    430      */
    431     public static boolean supportsDynamicUpdate(final FormatOptions options) {
    432         return options.mVersion >= FormatSpec.FIRST_VERSION_WITH_DYNAMIC_UPDATE
    433                 && options.mSupportsDynamicUpdate;
    434     }
    435 
    436     /**
    437      * Compute the size of the header (flag + [parent address] + characters size) of a CharGroup.
    438      *
    439      * @param group the group of which to compute the size of the header
    440      * @param options file format options.
    441      */
    442     private static int getGroupHeaderSize(final CharGroup group, final FormatOptions options) {
    443         if (supportsDynamicUpdate(options)) {
    444             return FormatSpec.GROUP_FLAGS_SIZE + FormatSpec.PARENT_ADDRESS_SIZE
    445                     + getGroupCharactersSize(group);
    446         } else {
    447             return FormatSpec.GROUP_FLAGS_SIZE + getGroupCharactersSize(group);
    448         }
    449     }
    450 
    451     private static final int UINT8_MAX = 0xFF;
    452     private static final int UINT16_MAX = 0xFFFF;
    453     private static final int UINT24_MAX = 0xFFFFFF;
    454 
    455     /**
    456      * Compute the size, in bytes, that an address will occupy.
    457      *
    458      * This can be used either for children addresses (which are always positive) or for
    459      * attribute, which may be positive or negative but
    460      * store their sign bit separately.
    461      *
    462      * @param address the address
    463      * @return the byte size.
    464      */
    465     static int getByteSize(final int address) {
    466         assert(address <= UINT24_MAX);
    467         if (!hasChildrenAddress(address)) {
    468             return 0;
    469         } else if (Math.abs(address) <= UINT8_MAX) {
    470             return 1;
    471         } else if (Math.abs(address) <= UINT16_MAX) {
    472             return 2;
    473         } else {
    474             return 3;
    475         }
    476     }
    477 
    478     private static final int SINT24_MAX = 0x7FFFFF;
    479     private static final int MSB8 = 0x80;
    480     private static final int MSB24 = 0x800000;
    481 
    482     // End utility methods.
    483 
    484     // This method is responsible for finding a nice ordering of the nodes that favors run-time
    485     // cache performance and dictionary size.
    486     /* package for tests */ static ArrayList<Node> flattenTree(final Node root) {
    487         final int treeSize = FusionDictionary.countCharGroups(root);
    488         MakedictLog.i("Counted nodes : " + treeSize);
    489         final ArrayList<Node> flatTree = new ArrayList<Node>(treeSize);
    490         return flattenTreeInner(flatTree, root);
    491     }
    492 
    493     private static ArrayList<Node> flattenTreeInner(final ArrayList<Node> list, final Node node) {
    494         // Removing the node is necessary if the tails are merged, because we would then
    495         // add the same node several times when we only want it once. A number of places in
    496         // the code also depends on any node being only once in the list.
    497         // Merging tails can only be done if there are no attributes. Searching for attributes
    498         // in LatinIME code depends on a total breadth-first ordering, which merging tails
    499         // breaks. If there are no attributes, it should be fine (and reduce the file size)
    500         // to merge tails, and removing the node from the list would be necessary. However,
    501         // we don't merge tails because breaking the breadth-first ordering would result in
    502         // extreme overhead at bigram lookup time (it would make the search function O(n) instead
    503         // of the current O(log(n)), where n=number of nodes in the dictionary which is pretty
    504         // high).
    505         // If no nodes are ever merged, we can't have the same node twice in the list, hence
    506         // searching for duplicates in unnecessary. It is also very performance consuming,
    507         // since `list' is an ArrayList so it's an O(n) operation that runs on all nodes, making
    508         // this simple list.remove operation O(n*n) overall. On Android this overhead is very
    509         // high.
    510         // For future reference, the code to remove duplicate is a simple : list.remove(node);
    511         list.add(node);
    512         final ArrayList<CharGroup> branches = node.mData;
    513         final int nodeSize = branches.size();
    514         for (CharGroup group : branches) {
    515             if (null != group.mChildren) flattenTreeInner(list, group.mChildren);
    516         }
    517         return list;
    518     }
    519 
    520     /**
    521      * Finds the absolute address of a word in the dictionary.
    522      *
    523      * @param dict the dictionary in which to search.
    524      * @param word the word we are searching for.
    525      * @return the word address. If it is not found, an exception is thrown.
    526      */
    527     private static int findAddressOfWord(final FusionDictionary dict, final String word) {
    528         return FusionDictionary.findWordInTree(dict.mRoot, word).mCachedAddress;
    529     }
    530 
    531     /**
    532      * Computes the actual node size, based on the cached addresses of the children nodes.
    533      *
    534      * Each node stores its tentative address. During dictionary address computing, these
    535      * are not final, but they can be used to compute the node size (the node size depends
    536      * on the address of the children because the number of bytes necessary to store an
    537      * address depends on its numeric value. The return value indicates whether the node
    538      * contents (as in, any of the addresses stored in the cache fields) have changed with
    539      * respect to their previous value.
    540      *
    541      * @param node the node to compute the size of.
    542      * @param dict the dictionary in which the word/attributes are to be found.
    543      * @param formatOptions file format options.
    544      * @return false if none of the cached addresses inside the node changed, true otherwise.
    545      */
    546     private static boolean computeActualNodeSize(final Node node, final FusionDictionary dict,
    547             final FormatOptions formatOptions) {
    548         boolean changed = false;
    549         int size = getGroupCountSize(node);
    550         for (CharGroup group : node.mData) {
    551             if (group.mCachedAddress != node.mCachedAddress + size) {
    552                 changed = true;
    553                 group.mCachedAddress = node.mCachedAddress + size;
    554             }
    555             int groupSize = getGroupHeaderSize(group, formatOptions);
    556             if (group.isTerminal()) groupSize += FormatSpec.GROUP_FREQUENCY_SIZE;
    557             if (null == group.mChildren && formatOptions.mSupportsDynamicUpdate) {
    558                 groupSize += FormatSpec.SIGNED_CHILDREN_ADDRESS_SIZE;
    559             } else if (null != group.mChildren) {
    560                 final int offsetBasePoint = groupSize + node.mCachedAddress + size;
    561                 final int offset = group.mChildren.mCachedAddress - offsetBasePoint;
    562                 // assign my address to children's parent address
    563                 group.mChildren.mCachedParentAddress = group.mCachedAddress
    564                         - group.mChildren.mCachedAddress;
    565                 if (formatOptions.mSupportsDynamicUpdate) {
    566                     groupSize += FormatSpec.SIGNED_CHILDREN_ADDRESS_SIZE;
    567                 } else {
    568                     groupSize += getByteSize(offset);
    569                 }
    570             }
    571             groupSize += getShortcutListSize(group.mShortcutTargets);
    572             if (null != group.mBigrams) {
    573                 for (WeightedString bigram : group.mBigrams) {
    574                     final int offsetBasePoint = groupSize + node.mCachedAddress + size
    575                             + FormatSpec.GROUP_FLAGS_SIZE;
    576                     final int addressOfBigram = findAddressOfWord(dict, bigram.mWord);
    577                     final int offset = addressOfBigram - offsetBasePoint;
    578                     groupSize += getByteSize(offset) + FormatSpec.GROUP_FLAGS_SIZE;
    579                 }
    580             }
    581             group.mCachedSize = groupSize;
    582             size += groupSize;
    583         }
    584         if (formatOptions.mSupportsDynamicUpdate) {
    585             size += FormatSpec.FORWARD_LINK_ADDRESS_SIZE;
    586         }
    587         if (node.mCachedSize != size) {
    588             node.mCachedSize = size;
    589             changed = true;
    590         }
    591         return changed;
    592     }
    593 
    594     /**
    595      * Computes the byte size of a list of nodes and updates each node cached position.
    596      *
    597      * @param flatNodes the array of nodes.
    598      * @param formatOptions file format options.
    599      * @return the byte size of the entire stack.
    600      */
    601     private static int stackNodes(final ArrayList<Node> flatNodes,
    602             final FormatOptions formatOptions) {
    603         int nodeOffset = 0;
    604         for (Node n : flatNodes) {
    605             n.mCachedAddress = nodeOffset;
    606             int groupCountSize = getGroupCountSize(n);
    607             int groupOffset = 0;
    608             for (CharGroup g : n.mData) {
    609                 g.mCachedAddress = groupCountSize + nodeOffset + groupOffset;
    610                 groupOffset += g.mCachedSize;
    611             }
    612             final int nodeSize = groupCountSize + groupOffset
    613                     + (formatOptions.mSupportsDynamicUpdate
    614                             ? FormatSpec.FORWARD_LINK_ADDRESS_SIZE : 0);
    615             if (nodeSize != n.mCachedSize) {
    616                 throw new RuntimeException("Bug : Stored and computed node size differ");
    617             }
    618             nodeOffset += n.mCachedSize;
    619         }
    620         return nodeOffset;
    621     }
    622 
    623     /**
    624      * Compute the addresses and sizes of an ordered node array.
    625      *
    626      * This method takes a node array and will update its cached address and size values
    627      * so that they can be written into a file. It determines the smallest size each of the
    628      * nodes can be given the addresses of its children and attributes, and store that into
    629      * each node.
    630      * The order of the node is given by the order of the array. This method makes no effort
    631      * to find a good order; it only mechanically computes the size this order results in.
    632      *
    633      * @param dict the dictionary
    634      * @param flatNodes the ordered array of nodes
    635      * @param formatOptions file format options.
    636      * @return the same array it was passed. The nodes have been updated for address and size.
    637      */
    638     private static ArrayList<Node> computeAddresses(final FusionDictionary dict,
    639             final ArrayList<Node> flatNodes, final FormatOptions formatOptions) {
    640         // First get the worst sizes and offsets
    641         for (Node n : flatNodes) setNodeMaximumSize(n, formatOptions);
    642         final int offset = stackNodes(flatNodes, formatOptions);
    643 
    644         MakedictLog.i("Compressing the array addresses. Original size : " + offset);
    645         MakedictLog.i("(Recursively seen size : " + offset + ")");
    646 
    647         int passes = 0;
    648         boolean changesDone = false;
    649         do {
    650             changesDone = false;
    651             for (Node n : flatNodes) {
    652                 final int oldNodeSize = n.mCachedSize;
    653                 final boolean changed = computeActualNodeSize(n, dict, formatOptions);
    654                 final int newNodeSize = n.mCachedSize;
    655                 if (oldNodeSize < newNodeSize) throw new RuntimeException("Increased size ?!");
    656                 changesDone |= changed;
    657             }
    658             stackNodes(flatNodes, formatOptions);
    659             ++passes;
    660             if (passes > MAX_PASSES) throw new RuntimeException("Too many passes - probably a bug");
    661         } while (changesDone);
    662 
    663         final Node lastNode = flatNodes.get(flatNodes.size() - 1);
    664         MakedictLog.i("Compression complete in " + passes + " passes.");
    665         MakedictLog.i("After address compression : "
    666                 + (lastNode.mCachedAddress + lastNode.mCachedSize));
    667 
    668         return flatNodes;
    669     }
    670 
    671     /**
    672      * Sanity-checking method.
    673      *
    674      * This method checks an array of node for juxtaposition, that is, it will do
    675      * nothing if each node's cached address is actually the previous node's address
    676      * plus the previous node's size.
    677      * If this is not the case, it will throw an exception.
    678      *
    679      * @param array the array node to check
    680      */
    681     private static void checkFlatNodeArray(final ArrayList<Node> array) {
    682         int offset = 0;
    683         int index = 0;
    684         for (Node n : array) {
    685             if (n.mCachedAddress != offset) {
    686                 throw new RuntimeException("Wrong address for node " + index
    687                         + " : expected " + offset + ", got " + n.mCachedAddress);
    688             }
    689             ++index;
    690             offset += n.mCachedSize;
    691         }
    692     }
    693 
    694     /**
    695      * Helper method to write a variable-size address to a file.
    696      *
    697      * @param buffer the buffer to write to.
    698      * @param index the index in the buffer to write the address to.
    699      * @param address the address to write.
    700      * @return the size in bytes the address actually took.
    701      */
    702     private static int writeVariableAddress(final byte[] buffer, int index, final int address) {
    703         switch (getByteSize(address)) {
    704         case 1:
    705             buffer[index++] = (byte)address;
    706             return 1;
    707         case 2:
    708             buffer[index++] = (byte)(0xFF & (address >> 8));
    709             buffer[index++] = (byte)(0xFF & address);
    710             return 2;
    711         case 3:
    712             buffer[index++] = (byte)(0xFF & (address >> 16));
    713             buffer[index++] = (byte)(0xFF & (address >> 8));
    714             buffer[index++] = (byte)(0xFF & address);
    715             return 3;
    716         case 0:
    717             return 0;
    718         default:
    719             throw new RuntimeException("Address " + address + " has a strange size");
    720         }
    721     }
    722 
    723     /**
    724      * Helper method to write a variable-size signed address to a file.
    725      *
    726      * @param buffer the buffer to write to.
    727      * @param index the index in the buffer to write the address to.
    728      * @param address the address to write.
    729      * @return the size in bytes the address actually took.
    730      */
    731     private static int writeVariableSignedAddress(final byte[] buffer, int index,
    732             final int address) {
    733         if (!hasChildrenAddress(address)) {
    734             buffer[index] = buffer[index + 1] = buffer[index + 2] = 0;
    735         } else {
    736             final int absAddress = Math.abs(address);
    737             buffer[index++] = (byte)((address < 0 ? MSB8 : 0) | (0xFF & (absAddress >> 16)));
    738             buffer[index++] = (byte)(0xFF & (absAddress >> 8));
    739             buffer[index++] = (byte)(0xFF & absAddress);
    740         }
    741         return 3;
    742     }
    743 
    744     /**
    745      * Makes the flag value for a char group.
    746      *
    747      * @param hasMultipleChars whether the group has multiple chars.
    748      * @param isTerminal whether the group is terminal.
    749      * @param childrenAddressSize the size of a children address.
    750      * @param hasShortcuts whether the group has shortcuts.
    751      * @param hasBigrams whether the group has bigrams.
    752      * @param isNotAWord whether the group is not a word.
    753      * @param isBlackListEntry whether the group is a blacklist entry.
    754      * @param formatOptions file format options.
    755      * @return the flags
    756      */
    757     static int makeCharGroupFlags(final boolean hasMultipleChars, final boolean isTerminal,
    758             final int childrenAddressSize, final boolean hasShortcuts, final boolean hasBigrams,
    759             final boolean isNotAWord, final boolean isBlackListEntry,
    760             final FormatOptions formatOptions) {
    761         byte flags = 0;
    762         if (hasMultipleChars) flags |= FormatSpec.FLAG_HAS_MULTIPLE_CHARS;
    763         if (isTerminal) flags |= FormatSpec.FLAG_IS_TERMINAL;
    764         if (formatOptions.mSupportsDynamicUpdate) {
    765             flags |= FormatSpec.FLAG_IS_NOT_MOVED;
    766         } else if (true) {
    767             switch (childrenAddressSize) {
    768                 case 1:
    769                     flags |= FormatSpec.FLAG_GROUP_ADDRESS_TYPE_ONEBYTE;
    770                     break;
    771                 case 2:
    772                     flags |= FormatSpec.FLAG_GROUP_ADDRESS_TYPE_TWOBYTES;
    773                     break;
    774                 case 3:
    775                     flags |= FormatSpec.FLAG_GROUP_ADDRESS_TYPE_THREEBYTES;
    776                     break;
    777                 case 0:
    778                     flags |= FormatSpec.FLAG_GROUP_ADDRESS_TYPE_NOADDRESS;
    779                     break;
    780                 default:
    781                     throw new RuntimeException("Node with a strange address");
    782             }
    783         }
    784         if (hasShortcuts) flags |= FormatSpec.FLAG_HAS_SHORTCUT_TARGETS;
    785         if (hasBigrams) flags |= FormatSpec.FLAG_HAS_BIGRAMS;
    786         if (isNotAWord) flags |= FormatSpec.FLAG_IS_NOT_A_WORD;
    787         if (isBlackListEntry) flags |= FormatSpec.FLAG_IS_BLACKLISTED;
    788         return flags;
    789     }
    790 
    791     private static byte makeCharGroupFlags(final CharGroup group, final int groupAddress,
    792             final int childrenOffset, final FormatOptions formatOptions) {
    793         return (byte) makeCharGroupFlags(group.mChars.length > 1, group.mFrequency >= 0,
    794                 getByteSize(childrenOffset), group.mShortcutTargets != null, group.mBigrams != null,
    795                 group.mIsNotAWord, group.mIsBlacklistEntry, formatOptions);
    796     }
    797 
    798     /**
    799      * Makes the flag value for a bigram.
    800      *
    801      * @param more whether there are more bigrams after this one.
    802      * @param offset the offset of the bigram.
    803      * @param bigramFrequency the frequency of the bigram, 0..255.
    804      * @param unigramFrequency the unigram frequency of the same word, 0..255.
    805      * @param word the second bigram, for debugging purposes
    806      * @return the flags
    807      */
    808     private static final int makeBigramFlags(final boolean more, final int offset,
    809             int bigramFrequency, final int unigramFrequency, final String word) {
    810         int bigramFlags = (more ? FormatSpec.FLAG_ATTRIBUTE_HAS_NEXT : 0)
    811                 + (offset < 0 ? FormatSpec.FLAG_ATTRIBUTE_OFFSET_NEGATIVE : 0);
    812         switch (getByteSize(offset)) {
    813         case 1:
    814             bigramFlags |= FormatSpec.FLAG_ATTRIBUTE_ADDRESS_TYPE_ONEBYTE;
    815             break;
    816         case 2:
    817             bigramFlags |= FormatSpec.FLAG_ATTRIBUTE_ADDRESS_TYPE_TWOBYTES;
    818             break;
    819         case 3:
    820             bigramFlags |= FormatSpec.FLAG_ATTRIBUTE_ADDRESS_TYPE_THREEBYTES;
    821             break;
    822         default:
    823             throw new RuntimeException("Strange offset size");
    824         }
    825         if (unigramFrequency > bigramFrequency) {
    826             MakedictLog.e("Unigram freq is superior to bigram freq for \"" + word
    827                     + "\". Bigram freq is " + bigramFrequency + ", unigram freq for "
    828                     + word + " is " + unigramFrequency);
    829             bigramFrequency = unigramFrequency;
    830         }
    831         // We compute the difference between 255 (which means probability = 1) and the
    832         // unigram score. We split this into a number of discrete steps.
    833         // Now, the steps are numbered 0~15; 0 represents an increase of 1 step while 15
    834         // represents an increase of 16 steps: a value of 15 will be interpreted as the median
    835         // value of the 16th step. In all justice, if the bigram frequency is low enough to be
    836         // rounded below the first step (which means it is less than half a step higher than the
    837         // unigram frequency) then the unigram frequency itself is the best approximation of the
    838         // bigram freq that we could possibly supply, hence we should *not* include this bigram
    839         // in the file at all.
    840         // until this is done, we'll write 0 and slightly overestimate this case.
    841         // In other words, 0 means "between 0.5 step and 1.5 step", 1 means "between 1.5 step
    842         // and 2.5 steps", and 15 means "between 15.5 steps and 16.5 steps". So we want to
    843         // divide our range [unigramFreq..MAX_TERMINAL_FREQUENCY] in 16.5 steps to get the
    844         // step size. Then we compute the start of the first step (the one where value 0 starts)
    845         // by adding half-a-step to the unigramFrequency. From there, we compute the integer
    846         // number of steps to the bigramFrequency. One last thing: we want our steps to include
    847         // their lower bound and exclude their higher bound so we need to have the first step
    848         // start at exactly 1 unit higher than floor(unigramFreq + half a step).
    849         // Note : to reconstruct the score, the dictionary reader will need to divide
    850         // MAX_TERMINAL_FREQUENCY - unigramFreq by 16.5 likewise to get the value of the step,
    851         // and add (discretizedFrequency + 0.5 + 0.5) times this value to get the best
    852         // approximation. (0.5 to get the first step start, and 0.5 to get the middle of the
    853         // step pointed by the discretized frequency.
    854         final float stepSize =
    855                 (FormatSpec.MAX_TERMINAL_FREQUENCY - unigramFrequency)
    856                 / (1.5f + FormatSpec.MAX_BIGRAM_FREQUENCY);
    857         final float firstStepStart = 1 + unigramFrequency + (stepSize / 2.0f);
    858         final int discretizedFrequency = (int)((bigramFrequency - firstStepStart) / stepSize);
    859         // If the bigram freq is less than half-a-step higher than the unigram freq, we get -1
    860         // here. The best approximation would be the unigram freq itself, so we should not
    861         // include this bigram in the dictionary. For now, register as 0, and live with the
    862         // small over-estimation that we get in this case. TODO: actually remove this bigram
    863         // if discretizedFrequency < 0.
    864         final int finalBigramFrequency = discretizedFrequency > 0 ? discretizedFrequency : 0;
    865         bigramFlags += finalBigramFrequency & FormatSpec.FLAG_ATTRIBUTE_FREQUENCY;
    866         return bigramFlags;
    867     }
    868 
    869     /**
    870      * Makes the 2-byte value for options flags.
    871      */
    872     private static final int makeOptionsValue(final FusionDictionary dictionary,
    873             final FormatOptions formatOptions) {
    874         final DictionaryOptions options = dictionary.mOptions;
    875         final boolean hasBigrams = dictionary.hasBigrams();
    876         return (options.mFrenchLigatureProcessing ? FormatSpec.FRENCH_LIGATURE_PROCESSING_FLAG : 0)
    877                 + (options.mGermanUmlautProcessing ? FormatSpec.GERMAN_UMLAUT_PROCESSING_FLAG : 0)
    878                 + (hasBigrams ? FormatSpec.CONTAINS_BIGRAMS_FLAG : 0)
    879                 + (formatOptions.mSupportsDynamicUpdate ? FormatSpec.SUPPORTS_DYNAMIC_UPDATE : 0);
    880     }
    881 
    882     /**
    883      * Makes the flag value for a shortcut.
    884      *
    885      * @param more whether there are more attributes after this one.
    886      * @param frequency the frequency of the attribute, 0..15
    887      * @return the flags
    888      */
    889     static final int makeShortcutFlags(final boolean more, final int frequency) {
    890         return (more ? FormatSpec.FLAG_ATTRIBUTE_HAS_NEXT : 0)
    891                 + (frequency & FormatSpec.FLAG_ATTRIBUTE_FREQUENCY);
    892     }
    893 
    894     private static final int writeParentAddress(final byte[] buffer, final int index,
    895             final int address, final FormatOptions formatOptions) {
    896         if (supportsDynamicUpdate(formatOptions)) {
    897             if (address == FormatSpec.NO_PARENT_ADDRESS) {
    898                 buffer[index] = buffer[index + 1] = buffer[index + 2] = 0;
    899             } else {
    900                 final int absAddress = Math.abs(address);
    901                 assert(absAddress <= SINT24_MAX);
    902                 buffer[index] = (byte)((address < 0 ? MSB8 : 0)
    903                         | ((absAddress >> 16) & 0xFF));
    904                 buffer[index + 1] = (byte)((absAddress >> 8) & 0xFF);
    905                 buffer[index + 2] = (byte)(absAddress & 0xFF);
    906             }
    907             return index + 3;
    908         } else {
    909             return index;
    910         }
    911     }
    912 
    913     /**
    914      * Write a node to memory. The node is expected to have its final position cached.
    915      *
    916      * This can be an empty map, but the more is inside the faster the lookups will be. It can
    917      * be carried on as long as nodes do not move.
    918      *
    919      * @param dict the dictionary the node is a part of (for relative offsets).
    920      * @param buffer the memory buffer to write to.
    921      * @param node the node to write.
    922      * @param formatOptions file format options.
    923      * @return the address of the END of the node.
    924      */
    925     @SuppressWarnings("unused")
    926     private static int writePlacedNode(final FusionDictionary dict, byte[] buffer,
    927             final Node node, final FormatOptions formatOptions) {
    928         // TODO: Make the code in common with BinaryDictIOUtils#writeCharGroup
    929         int index = node.mCachedAddress;
    930 
    931         final int groupCount = node.mData.size();
    932         final int countSize = getGroupCountSize(node);
    933         final int parentAddress = node.mCachedParentAddress;
    934         if (1 == countSize) {
    935             buffer[index++] = (byte)groupCount;
    936         } else if (2 == countSize) {
    937             // We need to signal 2-byte size by setting the top bit of the MSB to 1, so
    938             // we | 0x80 to do this.
    939             buffer[index++] = (byte)((groupCount >> 8) | 0x80);
    940             buffer[index++] = (byte)(groupCount & 0xFF);
    941         } else {
    942             throw new RuntimeException("Strange size from getGroupCountSize : " + countSize);
    943         }
    944         int groupAddress = index;
    945         for (int i = 0; i < groupCount; ++i) {
    946             CharGroup group = node.mData.get(i);
    947             if (index != group.mCachedAddress) throw new RuntimeException("Bug: write index is not "
    948                     + "the same as the cached address of the group : "
    949                     + index + " <> " + group.mCachedAddress);
    950             groupAddress += getGroupHeaderSize(group, formatOptions);
    951             // Sanity checks.
    952             if (DBG && group.mFrequency > FormatSpec.MAX_TERMINAL_FREQUENCY) {
    953                 throw new RuntimeException("A node has a frequency > "
    954                         + FormatSpec.MAX_TERMINAL_FREQUENCY
    955                         + " : " + group.mFrequency);
    956             }
    957             if (group.mFrequency >= 0) groupAddress += FormatSpec.GROUP_FREQUENCY_SIZE;
    958             final int childrenOffset = null == group.mChildren
    959                     ? FormatSpec.NO_CHILDREN_ADDRESS
    960                             : group.mChildren.mCachedAddress - groupAddress;
    961             byte flags = makeCharGroupFlags(group, groupAddress, childrenOffset, formatOptions);
    962             buffer[index++] = flags;
    963 
    964             if (parentAddress == FormatSpec.NO_PARENT_ADDRESS) {
    965                 index = writeParentAddress(buffer, index, parentAddress, formatOptions);
    966             } else {
    967                 index = writeParentAddress(buffer, index,
    968                         parentAddress + (node.mCachedAddress - group.mCachedAddress),
    969                         formatOptions);
    970             }
    971 
    972             index = CharEncoding.writeCharArray(group.mChars, buffer, index);
    973             if (group.hasSeveralChars()) {
    974                 buffer[index++] = FormatSpec.GROUP_CHARACTERS_TERMINATOR;
    975             }
    976             if (group.mFrequency >= 0) {
    977                 buffer[index++] = (byte) group.mFrequency;
    978             }
    979 
    980             final int shift;
    981             if (formatOptions.mSupportsDynamicUpdate) {
    982                 shift = writeVariableSignedAddress(buffer, index, childrenOffset);
    983             } else {
    984                 shift = writeVariableAddress(buffer, index, childrenOffset);
    985             }
    986             index += shift;
    987             groupAddress += shift;
    988 
    989             // Write shortcuts
    990             if (null != group.mShortcutTargets) {
    991                 final int indexOfShortcutByteSize = index;
    992                 index += FormatSpec.GROUP_SHORTCUT_LIST_SIZE_SIZE;
    993                 groupAddress += FormatSpec.GROUP_SHORTCUT_LIST_SIZE_SIZE;
    994                 final Iterator<WeightedString> shortcutIterator = group.mShortcutTargets.iterator();
    995                 while (shortcutIterator.hasNext()) {
    996                     final WeightedString target = shortcutIterator.next();
    997                     ++groupAddress;
    998                     int shortcutFlags = makeShortcutFlags(shortcutIterator.hasNext(),
    999                             target.mFrequency);
   1000                     buffer[index++] = (byte)shortcutFlags;
   1001                     final int shortcutShift = CharEncoding.writeString(buffer, index, target.mWord);
   1002                     index += shortcutShift;
   1003                     groupAddress += shortcutShift;
   1004                 }
   1005                 final int shortcutByteSize = index - indexOfShortcutByteSize;
   1006                 if (shortcutByteSize > 0xFFFF) {
   1007                     throw new RuntimeException("Shortcut list too large");
   1008                 }
   1009                 buffer[indexOfShortcutByteSize] = (byte)(shortcutByteSize >> 8);
   1010                 buffer[indexOfShortcutByteSize + 1] = (byte)(shortcutByteSize & 0xFF);
   1011             }
   1012             // Write bigrams
   1013             if (null != group.mBigrams) {
   1014                 final Iterator<WeightedString> bigramIterator = group.mBigrams.iterator();
   1015                 while (bigramIterator.hasNext()) {
   1016                     final WeightedString bigram = bigramIterator.next();
   1017                     final CharGroup target =
   1018                             FusionDictionary.findWordInTree(dict.mRoot, bigram.mWord);
   1019                     final int addressOfBigram = target.mCachedAddress;
   1020                     final int unigramFrequencyForThisWord = target.mFrequency;
   1021                     ++groupAddress;
   1022                     final int offset = addressOfBigram - groupAddress;
   1023                     int bigramFlags = makeBigramFlags(bigramIterator.hasNext(), offset,
   1024                             bigram.mFrequency, unigramFrequencyForThisWord, bigram.mWord);
   1025                     buffer[index++] = (byte)bigramFlags;
   1026                     final int bigramShift = writeVariableAddress(buffer, index, Math.abs(offset));
   1027                     index += bigramShift;
   1028                     groupAddress += bigramShift;
   1029                 }
   1030             }
   1031 
   1032         }
   1033         if (formatOptions.mSupportsDynamicUpdate) {
   1034             buffer[index] = buffer[index + 1] = buffer[index + 2]
   1035                     = FormatSpec.NO_FORWARD_LINK_ADDRESS;
   1036             index += FormatSpec.FORWARD_LINK_ADDRESS_SIZE;
   1037         }
   1038         if (index != node.mCachedAddress + node.mCachedSize) throw new RuntimeException(
   1039                 "Not the same size : written "
   1040                 + (index - node.mCachedAddress) + " bytes out of a node that should have "
   1041                 + node.mCachedSize + " bytes");
   1042         return index;
   1043     }
   1044 
   1045     /**
   1046      * Dumps a collection of useful statistics about a node array.
   1047      *
   1048      * This prints purely informative stuff, like the total estimated file size, the
   1049      * number of nodes, of character groups, the repartition of each address size, etc
   1050      *
   1051      * @param nodes the node array.
   1052      */
   1053     private static void showStatistics(ArrayList<Node> nodes) {
   1054         int firstTerminalAddress = Integer.MAX_VALUE;
   1055         int lastTerminalAddress = Integer.MIN_VALUE;
   1056         int size = 0;
   1057         int charGroups = 0;
   1058         int maxGroups = 0;
   1059         int maxRuns = 0;
   1060         for (Node n : nodes) {
   1061             if (maxGroups < n.mData.size()) maxGroups = n.mData.size();
   1062             for (CharGroup cg : n.mData) {
   1063                 ++charGroups;
   1064                 if (cg.mChars.length > maxRuns) maxRuns = cg.mChars.length;
   1065                 if (cg.mFrequency >= 0) {
   1066                     if (n.mCachedAddress < firstTerminalAddress)
   1067                         firstTerminalAddress = n.mCachedAddress;
   1068                     if (n.mCachedAddress > lastTerminalAddress)
   1069                         lastTerminalAddress = n.mCachedAddress;
   1070                 }
   1071             }
   1072             if (n.mCachedAddress + n.mCachedSize > size) size = n.mCachedAddress + n.mCachedSize;
   1073         }
   1074         final int[] groupCounts = new int[maxGroups + 1];
   1075         final int[] runCounts = new int[maxRuns + 1];
   1076         for (Node n : nodes) {
   1077             ++groupCounts[n.mData.size()];
   1078             for (CharGroup cg : n.mData) {
   1079                 ++runCounts[cg.mChars.length];
   1080             }
   1081         }
   1082 
   1083         MakedictLog.i("Statistics:\n"
   1084                 + "  total file size " + size + "\n"
   1085                 + "  " + nodes.size() + " nodes\n"
   1086                 + "  " + charGroups + " groups (" + ((float)charGroups / nodes.size())
   1087                         + " groups per node)\n"
   1088                 + "  first terminal at " + firstTerminalAddress + "\n"
   1089                 + "  last terminal at " + lastTerminalAddress + "\n"
   1090                 + "  Group stats : max = " + maxGroups);
   1091         for (int i = 0; i < groupCounts.length; ++i) {
   1092             MakedictLog.i("    " + i + " : " + groupCounts[i]);
   1093         }
   1094         MakedictLog.i("  Character run stats : max = " + maxRuns);
   1095         for (int i = 0; i < runCounts.length; ++i) {
   1096             MakedictLog.i("    " + i + " : " + runCounts[i]);
   1097         }
   1098     }
   1099 
   1100     /**
   1101      * Dumps a FusionDictionary to a file.
   1102      *
   1103      * This is the public entry point to write a dictionary to a file.
   1104      *
   1105      * @param destination the stream to write the binary data to.
   1106      * @param dict the dictionary to write.
   1107      * @param formatOptions file format options.
   1108      */
   1109     public static void writeDictionaryBinary(final OutputStream destination,
   1110             final FusionDictionary dict, final FormatOptions formatOptions)
   1111             throws IOException, UnsupportedFormatException {
   1112 
   1113         // Addresses are limited to 3 bytes, but since addresses can be relative to each node, the
   1114         // structure itself is not limited to 16MB. However, if it is over 16MB deciding the order
   1115         // of the nodes becomes a quite complicated problem, because though the dictionary itself
   1116         // does not have a size limit, each node must still be within 16MB of all its children and
   1117         // parents. As long as this is ensured, the dictionary file may grow to any size.
   1118 
   1119         final int version = formatOptions.mVersion;
   1120         if (version < FormatSpec.MINIMUM_SUPPORTED_VERSION
   1121                 || version > FormatSpec.MAXIMUM_SUPPORTED_VERSION) {
   1122             throw new UnsupportedFormatException("Requested file format version " + version
   1123                     + ", but this implementation only supports versions "
   1124                     + FormatSpec.MINIMUM_SUPPORTED_VERSION + " through "
   1125                     + FormatSpec.MAXIMUM_SUPPORTED_VERSION);
   1126         }
   1127 
   1128         ByteArrayOutputStream headerBuffer = new ByteArrayOutputStream(256);
   1129 
   1130         // The magic number in big-endian order.
   1131         if (version >= FormatSpec.FIRST_VERSION_WITH_HEADER_SIZE) {
   1132             // Magic number for version 2+.
   1133             headerBuffer.write((byte) (0xFF & (FormatSpec.VERSION_2_MAGIC_NUMBER >> 24)));
   1134             headerBuffer.write((byte) (0xFF & (FormatSpec.VERSION_2_MAGIC_NUMBER >> 16)));
   1135             headerBuffer.write((byte) (0xFF & (FormatSpec.VERSION_2_MAGIC_NUMBER >> 8)));
   1136             headerBuffer.write((byte) (0xFF & FormatSpec.VERSION_2_MAGIC_NUMBER));
   1137             // Dictionary version.
   1138             headerBuffer.write((byte) (0xFF & (version >> 8)));
   1139             headerBuffer.write((byte) (0xFF & version));
   1140         } else {
   1141             // Magic number for version 1.
   1142             headerBuffer.write((byte) (0xFF & (FormatSpec.VERSION_1_MAGIC_NUMBER >> 8)));
   1143             headerBuffer.write((byte) (0xFF & FormatSpec.VERSION_1_MAGIC_NUMBER));
   1144             // Dictionary version.
   1145             headerBuffer.write((byte) (0xFF & version));
   1146         }
   1147         // Options flags
   1148         final int options = makeOptionsValue(dict, formatOptions);
   1149         headerBuffer.write((byte) (0xFF & (options >> 8)));
   1150         headerBuffer.write((byte) (0xFF & options));
   1151         if (version >= FormatSpec.FIRST_VERSION_WITH_HEADER_SIZE) {
   1152             final int headerSizeOffset = headerBuffer.size();
   1153             // Placeholder to be written later with header size.
   1154             for (int i = 0; i < 4; ++i) {
   1155                 headerBuffer.write(0);
   1156             }
   1157             // Write out the options.
   1158             for (final String key : dict.mOptions.mAttributes.keySet()) {
   1159                 final String value = dict.mOptions.mAttributes.get(key);
   1160                 CharEncoding.writeString(headerBuffer, key);
   1161                 CharEncoding.writeString(headerBuffer, value);
   1162             }
   1163             final int size = headerBuffer.size();
   1164             final byte[] bytes = headerBuffer.toByteArray();
   1165             // Write out the header size.
   1166             bytes[headerSizeOffset] = (byte) (0xFF & (size >> 24));
   1167             bytes[headerSizeOffset + 1] = (byte) (0xFF & (size >> 16));
   1168             bytes[headerSizeOffset + 2] = (byte) (0xFF & (size >> 8));
   1169             bytes[headerSizeOffset + 3] = (byte) (0xFF & (size >> 0));
   1170             destination.write(bytes);
   1171         } else {
   1172             headerBuffer.writeTo(destination);
   1173         }
   1174 
   1175         headerBuffer.close();
   1176 
   1177         // Leave the choice of the optimal node order to the flattenTree function.
   1178         MakedictLog.i("Flattening the tree...");
   1179         ArrayList<Node> flatNodes = flattenTree(dict.mRoot);
   1180 
   1181         MakedictLog.i("Computing addresses...");
   1182         computeAddresses(dict, flatNodes, formatOptions);
   1183         MakedictLog.i("Checking array...");
   1184         if (DBG) checkFlatNodeArray(flatNodes);
   1185 
   1186         // Create a buffer that matches the final dictionary size.
   1187         final Node lastNode = flatNodes.get(flatNodes.size() - 1);
   1188         final int bufferSize = lastNode.mCachedAddress + lastNode.mCachedSize;
   1189         final byte[] buffer = new byte[bufferSize];
   1190         int index = 0;
   1191 
   1192         MakedictLog.i("Writing file...");
   1193         int dataEndOffset = 0;
   1194         for (Node n : flatNodes) {
   1195             dataEndOffset = writePlacedNode(dict, buffer, n, formatOptions);
   1196         }
   1197 
   1198         if (DBG) showStatistics(flatNodes);
   1199 
   1200         destination.write(buffer, 0, dataEndOffset);
   1201 
   1202         destination.close();
   1203         MakedictLog.i("Done");
   1204     }
   1205 
   1206 
   1207     // Input methods: Read a binary dictionary to memory.
   1208     // readDictionaryBinary is the public entry point for them.
   1209 
   1210     static int getChildrenAddressSize(final int optionFlags,
   1211             final FormatOptions formatOptions) {
   1212         if (formatOptions.mSupportsDynamicUpdate) return FormatSpec.SIGNED_CHILDREN_ADDRESS_SIZE;
   1213         switch (optionFlags & FormatSpec.MASK_GROUP_ADDRESS_TYPE) {
   1214             case FormatSpec.FLAG_GROUP_ADDRESS_TYPE_ONEBYTE:
   1215                 return 1;
   1216             case FormatSpec.FLAG_GROUP_ADDRESS_TYPE_TWOBYTES:
   1217                 return 2;
   1218             case FormatSpec.FLAG_GROUP_ADDRESS_TYPE_THREEBYTES:
   1219                 return 3;
   1220             case FormatSpec.FLAG_GROUP_ADDRESS_TYPE_NOADDRESS:
   1221             default:
   1222                 return 0;
   1223         }
   1224     }
   1225 
   1226     static int readChildrenAddress(final FusionDictionaryBufferInterface buffer,
   1227             final int optionFlags, final FormatOptions options) {
   1228         if (options.mSupportsDynamicUpdate) {
   1229             final int address = buffer.readUnsignedInt24();
   1230             if (address == 0) return FormatSpec.NO_CHILDREN_ADDRESS;
   1231             if ((address & MSB24) != 0) {
   1232                 return -(address & SINT24_MAX);
   1233             } else {
   1234                 return address;
   1235             }
   1236         }
   1237         int address;
   1238         switch (optionFlags & FormatSpec.MASK_GROUP_ADDRESS_TYPE) {
   1239             case FormatSpec.FLAG_GROUP_ADDRESS_TYPE_ONEBYTE:
   1240                 return buffer.readUnsignedByte();
   1241             case FormatSpec.FLAG_GROUP_ADDRESS_TYPE_TWOBYTES:
   1242                 return buffer.readUnsignedShort();
   1243             case FormatSpec.FLAG_GROUP_ADDRESS_TYPE_THREEBYTES:
   1244                 return buffer.readUnsignedInt24();
   1245             case FormatSpec.FLAG_GROUP_ADDRESS_TYPE_NOADDRESS:
   1246             default:
   1247                 return FormatSpec.NO_CHILDREN_ADDRESS;
   1248         }
   1249     }
   1250 
   1251     static int readParentAddress(final FusionDictionaryBufferInterface buffer,
   1252             final FormatOptions formatOptions) {
   1253         if (supportsDynamicUpdate(formatOptions)) {
   1254             final int parentAddress = buffer.readUnsignedInt24();
   1255             final int sign = ((parentAddress & MSB24) != 0) ? -1 : 1;
   1256             return sign * (parentAddress & SINT24_MAX);
   1257         } else {
   1258             return FormatSpec.NO_PARENT_ADDRESS;
   1259         }
   1260     }
   1261 
   1262     private static final int[] CHARACTER_BUFFER = new int[FormatSpec.MAX_WORD_LENGTH];
   1263     public static CharGroupInfo readCharGroup(final FusionDictionaryBufferInterface buffer,
   1264             final int originalGroupAddress, final FormatOptions options) {
   1265         int addressPointer = originalGroupAddress;
   1266         final int flags = buffer.readUnsignedByte();
   1267         ++addressPointer;
   1268 
   1269         final int parentAddress = readParentAddress(buffer, options);
   1270         if (supportsDynamicUpdate(options)) {
   1271             addressPointer += 3;
   1272         }
   1273 
   1274         final int characters[];
   1275         if (0 != (flags & FormatSpec.FLAG_HAS_MULTIPLE_CHARS)) {
   1276             int index = 0;
   1277             int character = CharEncoding.readChar(buffer);
   1278             addressPointer += CharEncoding.getCharSize(character);
   1279             while (-1 != character) {
   1280                 // FusionDictionary is making sure that the length of the word is smaller than
   1281                 // MAX_WORD_LENGTH.
   1282                 // So we'll never write past the end of CHARACTER_BUFFER.
   1283                 CHARACTER_BUFFER[index++] = character;
   1284                 character = CharEncoding.readChar(buffer);
   1285                 addressPointer += CharEncoding.getCharSize(character);
   1286             }
   1287             characters = Arrays.copyOfRange(CHARACTER_BUFFER, 0, index);
   1288         } else {
   1289             final int character = CharEncoding.readChar(buffer);
   1290             addressPointer += CharEncoding.getCharSize(character);
   1291             characters = new int[] { character };
   1292         }
   1293         final int frequency;
   1294         if (0 != (FormatSpec.FLAG_IS_TERMINAL & flags)) {
   1295             ++addressPointer;
   1296             frequency = buffer.readUnsignedByte();
   1297         } else {
   1298             frequency = CharGroup.NOT_A_TERMINAL;
   1299         }
   1300         int childrenAddress = readChildrenAddress(buffer, flags, options);
   1301         if (childrenAddress != FormatSpec.NO_CHILDREN_ADDRESS) {
   1302             childrenAddress += addressPointer;
   1303         }
   1304         addressPointer += getChildrenAddressSize(flags, options);
   1305         ArrayList<WeightedString> shortcutTargets = null;
   1306         if (0 != (flags & FormatSpec.FLAG_HAS_SHORTCUT_TARGETS)) {
   1307             final int pointerBefore = buffer.position();
   1308             shortcutTargets = new ArrayList<WeightedString>();
   1309             buffer.readUnsignedShort(); // Skip the size
   1310             while (true) {
   1311                 final int targetFlags = buffer.readUnsignedByte();
   1312                 final String word = CharEncoding.readString(buffer);
   1313                 shortcutTargets.add(new WeightedString(word,
   1314                         targetFlags & FormatSpec.FLAG_ATTRIBUTE_FREQUENCY));
   1315                 if (0 == (targetFlags & FormatSpec.FLAG_ATTRIBUTE_HAS_NEXT)) break;
   1316             }
   1317             addressPointer += buffer.position() - pointerBefore;
   1318         }
   1319         ArrayList<PendingAttribute> bigrams = null;
   1320         if (0 != (flags & FormatSpec.FLAG_HAS_BIGRAMS)) {
   1321             bigrams = new ArrayList<PendingAttribute>();
   1322             int bigramCount = 0;
   1323             while (bigramCount++ < FormatSpec.MAX_BIGRAMS_IN_A_GROUP) {
   1324                 final int bigramFlags = buffer.readUnsignedByte();
   1325                 ++addressPointer;
   1326                 final int sign = 0 == (bigramFlags & FormatSpec.FLAG_ATTRIBUTE_OFFSET_NEGATIVE)
   1327                         ? 1 : -1;
   1328                 int bigramAddress = addressPointer;
   1329                 switch (bigramFlags & FormatSpec.MASK_ATTRIBUTE_ADDRESS_TYPE) {
   1330                 case FormatSpec.FLAG_ATTRIBUTE_ADDRESS_TYPE_ONEBYTE:
   1331                     bigramAddress += sign * buffer.readUnsignedByte();
   1332                     addressPointer += 1;
   1333                     break;
   1334                 case FormatSpec.FLAG_ATTRIBUTE_ADDRESS_TYPE_TWOBYTES:
   1335                     bigramAddress += sign * buffer.readUnsignedShort();
   1336                     addressPointer += 2;
   1337                     break;
   1338                 case FormatSpec.FLAG_ATTRIBUTE_ADDRESS_TYPE_THREEBYTES:
   1339                     final int offset = (buffer.readUnsignedByte() << 16)
   1340                             + buffer.readUnsignedShort();
   1341                     bigramAddress += sign * offset;
   1342                     addressPointer += 3;
   1343                     break;
   1344                 default:
   1345                     throw new RuntimeException("Has bigrams with no address");
   1346                 }
   1347                 bigrams.add(new PendingAttribute(bigramFlags & FormatSpec.FLAG_ATTRIBUTE_FREQUENCY,
   1348                         bigramAddress));
   1349                 if (0 == (bigramFlags & FormatSpec.FLAG_ATTRIBUTE_HAS_NEXT)) break;
   1350             }
   1351             if (bigramCount >= FormatSpec.MAX_BIGRAMS_IN_A_GROUP) {
   1352                 MakedictLog.d("too many bigrams in a group.");
   1353             }
   1354         }
   1355         return new CharGroupInfo(originalGroupAddress, addressPointer, flags, characters, frequency,
   1356                 parentAddress, childrenAddress, shortcutTargets, bigrams);
   1357     }
   1358 
   1359     /**
   1360      * Reads and returns the char group count out of a buffer and forwards the pointer.
   1361      */
   1362     public static int readCharGroupCount(final FusionDictionaryBufferInterface buffer) {
   1363         final int msb = buffer.readUnsignedByte();
   1364         if (FormatSpec.MAX_CHARGROUPS_FOR_ONE_BYTE_CHARGROUP_COUNT >= msb) {
   1365             return msb;
   1366         } else {
   1367             return ((FormatSpec.MAX_CHARGROUPS_FOR_ONE_BYTE_CHARGROUP_COUNT & msb) << 8)
   1368                     + buffer.readUnsignedByte();
   1369         }
   1370     }
   1371 
   1372     // The word cache here is a stopgap bandaid to help the catastrophic performance
   1373     // of this method. Since it performs direct, unbuffered random access to the file and
   1374     // may be called hundreds of thousands of times, the resulting performance is not
   1375     // reasonable without some kind of cache. Thus:
   1376     private static TreeMap<Integer, WeightedString> wordCache =
   1377             new TreeMap<Integer, WeightedString>();
   1378     /**
   1379      * Finds, as a string, the word at the address passed as an argument.
   1380      *
   1381      * @param buffer the buffer to read from.
   1382      * @param headerSize the size of the header.
   1383      * @param address the address to seek.
   1384      * @param formatOptions file format options.
   1385      * @return the word with its frequency, as a weighted string.
   1386      */
   1387     /* package for tests */ static WeightedString getWordAtAddress(
   1388             final FusionDictionaryBufferInterface buffer, final int headerSize, final int address,
   1389             final FormatOptions formatOptions) {
   1390         final WeightedString cachedString = wordCache.get(address);
   1391         if (null != cachedString) return cachedString;
   1392 
   1393         final WeightedString result;
   1394         final int originalPointer = buffer.position();
   1395         buffer.position(address);
   1396 
   1397         if (supportsDynamicUpdate(formatOptions)) {
   1398             result = getWordAtAddressWithParentAddress(buffer, headerSize, address, formatOptions);
   1399         } else {
   1400             result = getWordAtAddressWithoutParentAddress(buffer, headerSize, address,
   1401                     formatOptions);
   1402         }
   1403 
   1404         wordCache.put(address, result);
   1405         buffer.position(originalPointer);
   1406         return result;
   1407     }
   1408 
   1409     // TODO: static!? This will behave erratically when used in multi-threaded code.
   1410     // We need to fix this
   1411     private static int[] sGetWordBuffer = new int[FormatSpec.MAX_WORD_LENGTH];
   1412     @SuppressWarnings("unused")
   1413     private static WeightedString getWordAtAddressWithParentAddress(
   1414             final FusionDictionaryBufferInterface buffer, final int headerSize, final int address,
   1415             final FormatOptions options) {
   1416         int currentAddress = address;
   1417         int index = FormatSpec.MAX_WORD_LENGTH - 1;
   1418         int frequency = Integer.MIN_VALUE;
   1419         // the length of the path from the root to the leaf is limited by MAX_WORD_LENGTH
   1420         for (int count = 0; count < FormatSpec.MAX_WORD_LENGTH; ++count) {
   1421             CharGroupInfo currentInfo;
   1422             int loopCounter = 0;
   1423             do {
   1424                 buffer.position(currentAddress + headerSize);
   1425                 currentInfo = readCharGroup(buffer, currentAddress, options);
   1426                 if (isMovedGroup(currentInfo.mFlags, options)) {
   1427                     currentAddress = currentInfo.mParentAddress + currentInfo.mOriginalAddress;
   1428                 }
   1429                 if (DBG && loopCounter++ > MAX_JUMPS) {
   1430                     MakedictLog.d("Too many jumps - probably a bug");
   1431                 }
   1432             } while (isMovedGroup(currentInfo.mFlags, options));
   1433             if (Integer.MIN_VALUE == frequency) frequency = currentInfo.mFrequency;
   1434             for (int i = 0; i < currentInfo.mCharacters.length; ++i) {
   1435                 sGetWordBuffer[index--] =
   1436                         currentInfo.mCharacters[currentInfo.mCharacters.length - i - 1];
   1437             }
   1438             if (currentInfo.mParentAddress == FormatSpec.NO_PARENT_ADDRESS) break;
   1439             currentAddress = currentInfo.mParentAddress + currentInfo.mOriginalAddress;
   1440         }
   1441 
   1442         return new WeightedString(
   1443                 new String(sGetWordBuffer, index + 1, FormatSpec.MAX_WORD_LENGTH - index - 1),
   1444                         frequency);
   1445     }
   1446 
   1447     private static WeightedString getWordAtAddressWithoutParentAddress(
   1448             final FusionDictionaryBufferInterface buffer, final int headerSize, final int address,
   1449             final FormatOptions options) {
   1450         buffer.position(headerSize);
   1451         final int count = readCharGroupCount(buffer);
   1452         int groupOffset = getGroupCountSize(count);
   1453         final StringBuilder builder = new StringBuilder();
   1454         WeightedString result = null;
   1455 
   1456         CharGroupInfo last = null;
   1457         for (int i = count - 1; i >= 0; --i) {
   1458             CharGroupInfo info = readCharGroup(buffer, groupOffset, options);
   1459             groupOffset = info.mEndAddress;
   1460             if (info.mOriginalAddress == address) {
   1461                 builder.append(new String(info.mCharacters, 0, info.mCharacters.length));
   1462                 result = new WeightedString(builder.toString(), info.mFrequency);
   1463                 break; // and return
   1464             }
   1465             if (hasChildrenAddress(info.mChildrenAddress)) {
   1466                 if (info.mChildrenAddress > address) {
   1467                     if (null == last) continue;
   1468                     builder.append(new String(last.mCharacters, 0, last.mCharacters.length));
   1469                     buffer.position(last.mChildrenAddress + headerSize);
   1470                     i = readCharGroupCount(buffer);
   1471                     groupOffset = last.mChildrenAddress + getGroupCountSize(i);
   1472                     last = null;
   1473                     continue;
   1474                 }
   1475                 last = info;
   1476             }
   1477             if (0 == i && hasChildrenAddress(last.mChildrenAddress)) {
   1478                 builder.append(new String(last.mCharacters, 0, last.mCharacters.length));
   1479                 buffer.position(last.mChildrenAddress + headerSize);
   1480                 i = readCharGroupCount(buffer);
   1481                 groupOffset = last.mChildrenAddress + getGroupCountSize(i);
   1482                 last = null;
   1483                 continue;
   1484             }
   1485         }
   1486         return result;
   1487     }
   1488 
   1489     /**
   1490      * Reads a single node from a buffer.
   1491      *
   1492      * This methods reads the file at the current position. A node is fully expected to start at
   1493      * the current position.
   1494      * This will recursively read other nodes into the structure, populating the reverse
   1495      * maps on the fly and using them to keep track of already read nodes.
   1496      *
   1497      * @param buffer the buffer, correctly positioned at the start of a node.
   1498      * @param headerSize the size, in bytes, of the file header.
   1499      * @param reverseNodeMap a mapping from addresses to already read nodes.
   1500      * @param reverseGroupMap a mapping from addresses to already read character groups.
   1501      * @param options file format options.
   1502      * @return the read node with all his children already read.
   1503      */
   1504     private static Node readNode(final FusionDictionaryBufferInterface buffer, final int headerSize,
   1505             final Map<Integer, Node> reverseNodeMap, final Map<Integer, CharGroup> reverseGroupMap,
   1506             final FormatOptions options)
   1507             throws IOException {
   1508         final ArrayList<CharGroup> nodeContents = new ArrayList<CharGroup>();
   1509         final int nodeOrigin = buffer.position() - headerSize;
   1510 
   1511         do { // Scan the linked-list node.
   1512             final int nodeHeadPosition = buffer.position() - headerSize;
   1513             final int count = readCharGroupCount(buffer);
   1514             int groupOffset = nodeHeadPosition + getGroupCountSize(count);
   1515             for (int i = count; i > 0; --i) { // Scan the array of CharGroup.
   1516                 CharGroupInfo info = readCharGroup(buffer, groupOffset, options);
   1517                 if (isMovedGroup(info.mFlags, options)) continue;
   1518                 ArrayList<WeightedString> shortcutTargets = info.mShortcutTargets;
   1519                 ArrayList<WeightedString> bigrams = null;
   1520                 if (null != info.mBigrams) {
   1521                     bigrams = new ArrayList<WeightedString>();
   1522                     for (PendingAttribute bigram : info.mBigrams) {
   1523                         final WeightedString word = getWordAtAddress(
   1524                                 buffer, headerSize, bigram.mAddress, options);
   1525                         final int reconstructedFrequency =
   1526                                 reconstructBigramFrequency(word.mFrequency, bigram.mFrequency);
   1527                         bigrams.add(new WeightedString(word.mWord, reconstructedFrequency));
   1528                     }
   1529                 }
   1530                 if (hasChildrenAddress(info.mChildrenAddress)) {
   1531                     Node children = reverseNodeMap.get(info.mChildrenAddress);
   1532                     if (null == children) {
   1533                         final int currentPosition = buffer.position();
   1534                         buffer.position(info.mChildrenAddress + headerSize);
   1535                         children = readNode(
   1536                                 buffer, headerSize, reverseNodeMap, reverseGroupMap, options);
   1537                         buffer.position(currentPosition);
   1538                     }
   1539                     nodeContents.add(
   1540                             new CharGroup(info.mCharacters, shortcutTargets, bigrams,
   1541                                     info.mFrequency,
   1542                                     0 != (info.mFlags & FormatSpec.FLAG_IS_NOT_A_WORD),
   1543                                     0 != (info.mFlags & FormatSpec.FLAG_IS_BLACKLISTED), children));
   1544                 } else {
   1545                     nodeContents.add(
   1546                             new CharGroup(info.mCharacters, shortcutTargets, bigrams,
   1547                                     info.mFrequency,
   1548                                     0 != (info.mFlags & FormatSpec.FLAG_IS_NOT_A_WORD),
   1549                                     0 != (info.mFlags & FormatSpec.FLAG_IS_BLACKLISTED)));
   1550                 }
   1551                 groupOffset = info.mEndAddress;
   1552             }
   1553 
   1554             // reach the end of the array.
   1555             if (options.mSupportsDynamicUpdate) {
   1556                 final int nextAddress = buffer.readUnsignedInt24();
   1557                 if (nextAddress >= 0 && nextAddress < buffer.limit()) {
   1558                     buffer.position(nextAddress);
   1559                 } else {
   1560                     break;
   1561                 }
   1562             }
   1563         } while (options.mSupportsDynamicUpdate &&
   1564                 buffer.position() != FormatSpec.NO_FORWARD_LINK_ADDRESS);
   1565 
   1566         final Node node = new Node(nodeContents);
   1567         node.mCachedAddress = nodeOrigin;
   1568         reverseNodeMap.put(node.mCachedAddress, node);
   1569         return node;
   1570     }
   1571 
   1572     /**
   1573      * Helper function to get the binary format version from the header.
   1574      * @throws IOException
   1575      */
   1576     private static int getFormatVersion(final FusionDictionaryBufferInterface buffer)
   1577             throws IOException {
   1578         final int magic_v1 = buffer.readUnsignedShort();
   1579         if (FormatSpec.VERSION_1_MAGIC_NUMBER == magic_v1) return buffer.readUnsignedByte();
   1580         final int magic_v2 = (magic_v1 << 16) + buffer.readUnsignedShort();
   1581         if (FormatSpec.VERSION_2_MAGIC_NUMBER == magic_v2) return buffer.readUnsignedShort();
   1582         return FormatSpec.NOT_A_VERSION_NUMBER;
   1583     }
   1584 
   1585     /**
   1586      * Helper function to get and validate the binary format version.
   1587      * @throws UnsupportedFormatException
   1588      * @throws IOException
   1589      */
   1590     private static int checkFormatVersion(final FusionDictionaryBufferInterface buffer)
   1591             throws IOException, UnsupportedFormatException {
   1592         final int version = getFormatVersion(buffer);
   1593         if (version < FormatSpec.MINIMUM_SUPPORTED_VERSION
   1594                 || version > FormatSpec.MAXIMUM_SUPPORTED_VERSION) {
   1595             throw new UnsupportedFormatException("This file has version " + version
   1596                     + ", but this implementation does not support versions above "
   1597                     + FormatSpec.MAXIMUM_SUPPORTED_VERSION);
   1598         }
   1599         return version;
   1600     }
   1601 
   1602     /**
   1603      * Reads a header from a buffer.
   1604      * @param buffer the buffer to read.
   1605      * @throws IOException
   1606      * @throws UnsupportedFormatException
   1607      */
   1608     public static FileHeader readHeader(final FusionDictionaryBufferInterface buffer)
   1609             throws IOException, UnsupportedFormatException {
   1610         final int version = checkFormatVersion(buffer);
   1611         final int optionsFlags = buffer.readUnsignedShort();
   1612 
   1613         final HashMap<String, String> attributes = new HashMap<String, String>();
   1614         final int headerSize;
   1615         if (version < FormatSpec.FIRST_VERSION_WITH_HEADER_SIZE) {
   1616             headerSize = buffer.position();
   1617         } else {
   1618             headerSize = buffer.readInt();
   1619             populateOptions(buffer, headerSize, attributes);
   1620             buffer.position(headerSize);
   1621         }
   1622 
   1623         if (headerSize < 0) {
   1624             throw new UnsupportedFormatException("header size can't be negative.");
   1625         }
   1626 
   1627         final FileHeader header = new FileHeader(headerSize,
   1628                 new FusionDictionary.DictionaryOptions(attributes,
   1629                         0 != (optionsFlags & FormatSpec.GERMAN_UMLAUT_PROCESSING_FLAG),
   1630                         0 != (optionsFlags & FormatSpec.FRENCH_LIGATURE_PROCESSING_FLAG)),
   1631                 new FormatOptions(version,
   1632                         0 != (optionsFlags & FormatSpec.SUPPORTS_DYNAMIC_UPDATE)));
   1633         return header;
   1634     }
   1635 
   1636     /**
   1637      * Reads options from a buffer and populate a map with their contents.
   1638      *
   1639      * The buffer is read at the current position, so the caller must take care the pointer
   1640      * is in the right place before calling this.
   1641      */
   1642     public static void populateOptions(final FusionDictionaryBufferInterface buffer,
   1643             final int headerSize, final HashMap<String, String> options) {
   1644         while (buffer.position() < headerSize) {
   1645             final String key = CharEncoding.readString(buffer);
   1646             final String value = CharEncoding.readString(buffer);
   1647             options.put(key, value);
   1648         }
   1649     }
   1650 
   1651     /**
   1652      * Reads a buffer and returns the memory representation of the dictionary.
   1653      *
   1654      * This high-level method takes a buffer and reads its contents, populating a
   1655      * FusionDictionary structure. The optional dict argument is an existing dictionary to
   1656      * which words from the buffer should be added. If it is null, a new dictionary is created.
   1657      *
   1658      * @param buffer the buffer to read.
   1659      * @param dict an optional dictionary to add words to, or null.
   1660      * @return the created (or merged) dictionary.
   1661      */
   1662     @UsedForTesting
   1663     public static FusionDictionary readDictionaryBinary(
   1664             final FusionDictionaryBufferInterface buffer, final FusionDictionary dict)
   1665                     throws IOException, UnsupportedFormatException {
   1666         // clear cache
   1667         wordCache.clear();
   1668 
   1669         // Read header
   1670         final FileHeader header = readHeader(buffer);
   1671 
   1672         Map<Integer, Node> reverseNodeMapping = new TreeMap<Integer, Node>();
   1673         Map<Integer, CharGroup> reverseGroupMapping = new TreeMap<Integer, CharGroup>();
   1674         final Node root = readNode(buffer, header.mHeaderSize, reverseNodeMapping,
   1675                 reverseGroupMapping, header.mFormatOptions);
   1676 
   1677         FusionDictionary newDict = new FusionDictionary(root, header.mDictionaryOptions);
   1678         if (null != dict) {
   1679             for (final Word w : dict) {
   1680                 if (w.mIsBlacklistEntry) {
   1681                     newDict.addBlacklistEntry(w.mWord, w.mShortcutTargets, w.mIsNotAWord);
   1682                 } else {
   1683                     newDict.add(w.mWord, w.mFrequency, w.mShortcutTargets, w.mIsNotAWord);
   1684                 }
   1685             }
   1686             for (final Word w : dict) {
   1687                 // By construction a binary dictionary may not have bigrams pointing to
   1688                 // words that are not also registered as unigrams so we don't have to avoid
   1689                 // them explicitly here.
   1690                 for (final WeightedString bigram : w.mBigrams) {
   1691                     newDict.setBigram(w.mWord, bigram.mWord, bigram.mFrequency);
   1692                 }
   1693             }
   1694         }
   1695 
   1696         return newDict;
   1697     }
   1698 
   1699     /**
   1700      * Helper method to pass a file name instead of a File object to isBinaryDictionary.
   1701      */
   1702     public static boolean isBinaryDictionary(final String filename) {
   1703         final File file = new File(filename);
   1704         return isBinaryDictionary(file);
   1705     }
   1706 
   1707     /**
   1708      * Basic test to find out whether the file is a binary dictionary or not.
   1709      *
   1710      * Concretely this only tests the magic number.
   1711      *
   1712      * @param file The file to test.
   1713      * @return true if it's a binary dictionary, false otherwise
   1714      */
   1715     public static boolean isBinaryDictionary(final File file) {
   1716         FileInputStream inStream = null;
   1717         try {
   1718             inStream = new FileInputStream(file);
   1719             final ByteBuffer buffer = inStream.getChannel().map(
   1720                     FileChannel.MapMode.READ_ONLY, 0, file.length());
   1721             final int version = getFormatVersion(new ByteBufferWrapper(buffer));
   1722             return (version >= FormatSpec.MINIMUM_SUPPORTED_VERSION
   1723                     && version <= FormatSpec.MAXIMUM_SUPPORTED_VERSION);
   1724         } catch (FileNotFoundException e) {
   1725             return false;
   1726         } catch (IOException e) {
   1727             return false;
   1728         } finally {
   1729             if (inStream != null) {
   1730                 try {
   1731                     inStream.close();
   1732                 } catch (IOException e) {
   1733                     // do nothing
   1734                 }
   1735             }
   1736         }
   1737     }
   1738 
   1739     /**
   1740      * Calculate bigram frequency from compressed value
   1741      *
   1742      * @see #makeBigramFlags
   1743      *
   1744      * @param unigramFrequency
   1745      * @param bigramFrequency compressed frequency
   1746      * @return approximate bigram frequency
   1747      */
   1748     public static int reconstructBigramFrequency(final int unigramFrequency,
   1749             final int bigramFrequency) {
   1750         final float stepSize = (FormatSpec.MAX_TERMINAL_FREQUENCY - unigramFrequency)
   1751                 / (1.5f + FormatSpec.MAX_BIGRAM_FREQUENCY);
   1752         final float resultFreqFloat = unigramFrequency + stepSize * (bigramFrequency + 1.0f);
   1753         return (int)resultFreqFloat;
   1754     }
   1755 }
   1756