aboutsummaryrefslogtreecommitdiffstats
path: root/tests/src
diff options
context:
space:
mode:
authorJean Chalard <jchalard@google.com>2014-11-06 15:20:24 +0000
committerAndroid Git Automerger <android-git-automerger@android.com>2014-11-06 15:20:24 +0000
commit830a600358c3332d09d20edc8510b6ccd4f63fff (patch)
treeb11db6021b0e39b344f395a5eed47b7a156e4db6 /tests/src
parentce34cb9b8381bd6de9648fc233e7f3824ade2872 (diff)
parentf6b0e32df38da4e2130bdbfc8875ea2d19054caf (diff)
downloadlatinime-830a600358c3332d09d20edc8510b6ccd4f63fff.tar.gz
latinime-830a600358c3332d09d20edc8510b6ccd4f63fff.tar.xz
latinime-830a600358c3332d09d20edc8510b6ccd4f63fff.zip
am f6b0e32d: Add a *FAST* dictionary header reader.
* commit 'f6b0e32df38da4e2130bdbfc8875ea2d19054caf': Add a *FAST* dictionary header reader.
Diffstat (limited to 'tests/src')
-rw-r--r--tests/src/com/android/inputmethod/latin/makedict/BinaryDictDecoderUtils.java42
1 files changed, 42 insertions, 0 deletions
diff --git a/tests/src/com/android/inputmethod/latin/makedict/BinaryDictDecoderUtils.java b/tests/src/com/android/inputmethod/latin/makedict/BinaryDictDecoderUtils.java
index 120b96bc6..be75565bb 100644
--- a/tests/src/com/android/inputmethod/latin/makedict/BinaryDictDecoderUtils.java
+++ b/tests/src/com/android/inputmethod/latin/makedict/BinaryDictDecoderUtils.java
@@ -17,11 +17,16 @@
package com.android.inputmethod.latin.makedict;
import com.android.inputmethod.annotations.UsedForTesting;
+import com.android.inputmethod.latin.makedict.UnsupportedFormatException;
+
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.util.HashMap;
+import java.util.LinkedList;
+
+import javax.annotation.Nonnull;
/**
* Decodes binary files for a FusionDictionary.
@@ -361,6 +366,43 @@ public final class BinaryDictDecoderUtils {
}
/**
+ * Helper method that brutally decodes a header from a byte array.
+ *
+ * @param headerBuffer a buffer containing the bytes of the header.
+ * @return a hashmap of the attributes stored in the header
+ */
+ @Nonnull
+ public static HashMap<String, String> decodeHeaderAttributes(@Nonnull final byte[] headerBuffer)
+ throws UnsupportedFormatException {
+ final StringBuilder sb = new StringBuilder();
+ final LinkedList<String> keyValues = new LinkedList<>();
+ int index = 0;
+ while (index < headerBuffer.length) {
+ if (headerBuffer[index] == FormatSpec.PTNODE_CHARACTERS_TERMINATOR) {
+ keyValues.add(sb.toString());
+ sb.setLength(0);
+ } else if (CharEncoding.fitsOnOneByte(headerBuffer[index] & 0xFF,
+ null /* codePointTable */)) {
+ sb.appendCodePoint(headerBuffer[index] & 0xFF);
+ } else {
+ sb.appendCodePoint(((headerBuffer[index] & 0xFF) << 16)
+ + ((headerBuffer[index + 1] & 0xFF) << 8)
+ + (headerBuffer[index + 2] & 0xFF));
+ index += 2;
+ }
+ index += 1;
+ }
+ if ((keyValues.size() & 1) != 0) {
+ throw new UnsupportedFormatException("Odd number of attributes");
+ }
+ final HashMap<String, String> attributes = new HashMap<>();
+ for (int i = 0; i < keyValues.size(); i += 2) {
+ attributes.put(keyValues.get(i), keyValues.get(i + 1));
+ }
+ return attributes;
+ }
+
+ /**
* Helper method to pass a file name instead of a File object to isBinaryDictionary.
*/
public static boolean isBinaryDictionary(final String filename) {