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

Commit 1d15fe7e authored by Jean Chalard's avatar Jean Chalard
Browse files

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

Bug: 7702011
Change-Id: Ib88f6dc222892831ae6932635b65fd2595b16b43
parent babc71ee
Loading
Loading
Loading
Loading
+27 −0
Original line number Diff line number Diff line
@@ -25,8 +25,12 @@ import com.android.inputmethod.latin.makedict.FormatSpec.FormatOptions;
import com.android.inputmethod.latin.makedict.FusionDictionary.CharGroup;
import com.android.inputmethod.latin.makedict.FusionDictionary.WeightedString;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
@@ -977,4 +981,27 @@ public final class BinaryDictIOUtils {
        }
        return null;
    }

    /**
     * Convenience method to read the header of a binary file.
     *
     * This is quite resource intensive - don't call when performance is critical.
     *
     * @param file The file to read.
     */
    private static final int HEADER_READING_BUFFER_SIZE = 16384;
    public static FileHeader getDictionaryFileHeader(final File file)
        throws FileNotFoundException, IOException, UnsupportedFormatException {
        final byte[] buffer = new byte[HEADER_READING_BUFFER_SIZE];
        final FileInputStream inStream = new FileInputStream(file);
        try {
            inStream.read(buffer);
            final BinaryDictInputOutput.ByteBufferWrapper wrapper =
                    new BinaryDictInputOutput.ByteBufferWrapper(inStream.getChannel().map(
                            FileChannel.MapMode.READ_ONLY, 0, file.length()));
            return BinaryDictInputOutput.readHeader(wrapper);
        } finally {
            inStream.close();
        }
    }
}