Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a6dc3056 authored by Ken Wakasa's avatar Ken Wakasa Committed by Android (Google) Code Review
Browse files

Merge "Add getDictDecoder."

parents 28af9ed1 0e40cd0c
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -21,9 +21,10 @@ import android.content.SharedPreferences;
import android.content.res.AssetFileDescriptor;
import android.util.Log;

import com.android.inputmethod.latin.makedict.DictDecoder;
import com.android.inputmethod.latin.makedict.FormatSpec;
import com.android.inputmethod.latin.makedict.FormatSpec.FileHeader;
import com.android.inputmethod.latin.makedict.UnsupportedFormatException;
import com.android.inputmethod.latin.makedict.Ver3DictDecoder;
import com.android.inputmethod.latin.utils.CollectionUtils;
import com.android.inputmethod.latin.utils.DictionaryInfoUtils;
import com.android.inputmethod.latin.utils.LocaleUtils;
@@ -228,7 +229,7 @@ final public class BinaryDictionaryGetter {
    private static boolean hackCanUseDictionaryFile(final Locale locale, final File f) {
        try {
            // Read the version of the file
            final Ver3DictDecoder dictDecoder = new Ver3DictDecoder(f);
            final DictDecoder dictDecoder = FormatSpec.getDictDecoder(f);
            final FileHeader header = dictDecoder.readHeader();

            final String version = header.mDictionaryOptions.mAttributes.get(VERSION_KEY);
+3 −3
Original line number Diff line number Diff line
@@ -148,7 +148,7 @@ public final class BinaryDictIOUtils {
     * @throws IOException if the file can't be read.
     * @throws UnsupportedFormatException if the format of the file is not recognized.
     */
    /* package */ static void readUnigramsAndBigramsBinary(final Ver3DictDecoder dictDecoder,
    /* package */ static void readUnigramsAndBigramsBinary(final DictDecoder dictDecoder,
            final Map<Integer, String> words, final Map<Integer, Integer> frequencies,
            final Map<Integer, ArrayList<PendingAttribute>> bigrams) throws IOException,
            UnsupportedFormatException {
@@ -169,7 +169,7 @@ public final class BinaryDictIOUtils {
     * @throws UnsupportedFormatException if the format of the file is not recognized.
     */
    @UsedForTesting
    /* package */ static int getTerminalPosition(final Ver3DictDecoder dictDecoder,
    /* package */ static int getTerminalPosition(final DictDecoder dictDecoder,
            final String word) throws IOException, UnsupportedFormatException {
        if (word == null) return FormatSpec.NOT_VALID_WORD;
        dictDecoder.setPosition(0);
@@ -519,7 +519,7 @@ public final class BinaryDictIOUtils {
            final File file, final long offset, final long length)
            throws FileNotFoundException, IOException, UnsupportedFormatException {
        final byte[] buffer = new byte[HEADER_READING_BUFFER_SIZE];
        final Ver3DictDecoder dictDecoder = new Ver3DictDecoder(file,
        final DictDecoder dictDecoder = FormatSpec.getDictDecoder(file,
                new DictDecoder.DictionaryBufferFactory() {
                    @Override
                    public DictBuffer getDictionaryBuffer(File file)
+8 −0
Original line number Diff line number Diff line
@@ -118,6 +118,14 @@ public interface DictDecoder {
    public boolean readForwardLinkAndAdvancePosition();
    public boolean hasNextPtNodeArray();

    /**
     * Opens the dictionary file and makes DictBuffer.
     */
    @UsedForTesting
    public void openDictBuffer() throws FileNotFoundException, IOException;
    @UsedForTesting
    public boolean isOpenedDictBuffer();

    // Flags for DictionaryBufferFactory.
    public static final int USE_READONLY_BYTEBUFFER = 0x01000000;
    public static final int USE_BYTEARRAY = 0x02000000;
+25 −0
Original line number Diff line number Diff line
@@ -18,8 +18,11 @@ package com.android.inputmethod.latin.makedict;

import com.android.inputmethod.annotations.UsedForTesting;
import com.android.inputmethod.latin.Constants;
import com.android.inputmethod.latin.makedict.DictDecoder.DictionaryBufferFactory;
import com.android.inputmethod.latin.makedict.FusionDictionary.DictionaryOptions;

import java.io.File;

/**
 * Dictionary File Format Specification.
 */
@@ -341,6 +344,28 @@ public final class FormatSpec {
        }
    }

    /**
     * Returns new dictionary decoder.
     *
     * @param dictFile the dictionary file.
     * @param bufferType the flag indicating buffer type which is used by the dictionary decoder.
     * @return new dictionary decoder if the dictionary file exists, otherwise null.
     */
    public static DictDecoder getDictDecoder(final File dictFile, final int bufferType) {
        if (!dictFile.isFile()) return null;
        return new Ver3DictDecoder(dictFile, bufferType);
    }

    public static DictDecoder getDictDecoder(final File dictFile,
            final DictionaryBufferFactory factory) {
        if (!dictFile.isFile()) return null;
        return new Ver3DictDecoder(dictFile, factory);
    }

    public static DictDecoder getDictDecoder(final File dictFile) {
        return getDictDecoder(dictFile, DictDecoder.USE_READONLY_BYTEBUFFER);
    }

    private FormatSpec() {
        // This utility class is not publicly instantiable.
    }
+8 −6
Original line number Diff line number Diff line
@@ -173,11 +173,7 @@ public class Ver3DictDecoder implements DictDecoder {
    private final DictionaryBufferFactory mBufferFactory;
    private DictBuffer mDictBuffer;

    public Ver3DictDecoder(final File file) {
        this(file, USE_READONLY_BYTEBUFFER);
    }

    public Ver3DictDecoder(final File file, final int factoryFlag) {
    /* package */ Ver3DictDecoder(final File file, final int factoryFlag) {
        mDictionaryBinaryFile = file;
        mDictBuffer = null;

@@ -192,15 +188,21 @@ public class Ver3DictDecoder implements DictDecoder {
        }
    }

    public Ver3DictDecoder(final File file, final DictionaryBufferFactory factory) {
    /* package */ Ver3DictDecoder(final File file, final DictionaryBufferFactory factory) {
        mDictionaryBinaryFile = file;
        mBufferFactory = factory;
    }

    @Override
    public void openDictBuffer() throws FileNotFoundException, IOException {
        mDictBuffer = mBufferFactory.getDictionaryBuffer(mDictionaryBinaryFile);
    }

    @Override
    public boolean isOpenedDictBuffer() {
        return mDictBuffer != null;
    }

    /* package */ DictBuffer getDictBuffer() {
        return mDictBuffer;
    }
Loading