From 1557de7aa47bdac707c06daa0a6cce3afda0de0f Mon Sep 17 00:00:00 2001
From: Yuichiro Hanada <yhanada@google.com>
Date: Wed, 16 Oct 2013 00:08:02 +0900
Subject: [PATCH] Consolidate CharEncoding.writeString and
 BinaryDictIOUtils.writeString.

Change-Id: I6f990fd84e7f08fd1149198c33d8bbf1cac8e078
---
 .../makedict/BinaryDictDecoderUtils.java      | 10 ++++--
 .../latin/makedict/BinaryDictIOUtils.java     | 31 +------------------
 2 files changed, 9 insertions(+), 32 deletions(-)

diff --git a/java/src/com/android/inputmethod/latin/makedict/BinaryDictDecoderUtils.java b/java/src/com/android/inputmethod/latin/makedict/BinaryDictDecoderUtils.java
index 216492b4de..8109321b61 100644
--- a/java/src/com/android/inputmethod/latin/makedict/BinaryDictDecoderUtils.java
+++ b/java/src/com/android/inputmethod/latin/makedict/BinaryDictDecoderUtils.java
@@ -225,20 +225,26 @@ public final class BinaryDictDecoderUtils {
          *
          * @param buffer the OutputStream to write to.
          * @param word the string to write.
+         * @return the size written, in bytes.
          */
-        static void writeString(final OutputStream buffer, final String word) throws IOException {
+        static int writeString(final OutputStream buffer, final String word) throws IOException {
             final int length = word.length();
+            int written = 0;
             for (int i = 0; i < length; i = word.offsetByCodePoints(i, 1)) {
                 final int codePoint = word.codePointAt(i);
-                if (1 == getCharSize(codePoint)) {
+                final int charSize = getCharSize(codePoint);
+                if (1 == charSize) {
                     buffer.write((byte) codePoint);
                 } else {
                     buffer.write((byte) (0xFF & (codePoint >> 16)));
                     buffer.write((byte) (0xFF & (codePoint >> 8)));
                     buffer.write((byte) (0xFF & codePoint));
                 }
+                written += charSize;
             }
             buffer.write(FormatSpec.PTNODE_CHARACTERS_TERMINATOR);
+            written += FormatSpec.PTNODE_TERMINATOR_SIZE;
+            return written;
         }
 
         /**
diff --git a/java/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtils.java b/java/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtils.java
index 0f7d2f6c98..9a28629b1b 100644
--- a/java/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtils.java
+++ b/java/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtils.java
@@ -300,35 +300,6 @@ public final class BinaryDictIOUtils {
         }
     }
 
-    /**
-     * Write a string to a stream.
-     *
-     * @param destination the stream to write.
-     * @param word the string to be written.
-     * @return the size written, in bytes.
-     * @throws IOException
-     */
-    private static int writeString(final OutputStream destination, final String word)
-            throws IOException {
-        int size = 0;
-        final int length = word.length();
-        for (int i = 0; i < length; i = word.offsetByCodePoints(i, 1)) {
-            final int codePoint = word.codePointAt(i);
-            if (CharEncoding.getCharSize(codePoint) == 1) {
-                destination.write((byte)codePoint);
-                size++;
-            } else {
-                destination.write((byte)(0xFF & (codePoint >> 16)));
-                destination.write((byte)(0xFF & (codePoint >> 8)));
-                destination.write((byte)(0xFF & codePoint));
-                size += 3;
-            }
-        }
-        destination.write((byte)FormatSpec.PTNODE_CHARACTERS_TERMINATOR);
-        size += FormatSpec.PTNODE_TERMINATOR_SIZE;
-        return size;
-    }
-
     /**
      * Write a PtNode to an output stream from a PtNodeInfo.
      * A PtNode is an in-memory representation of a node in the patricia trie.
@@ -387,7 +358,7 @@ public final class BinaryDictIOUtils {
                 destination.write((byte)BinaryDictEncoderUtils.makeShortcutFlags(
                         shortcutIterator.hasNext(), target.mFrequency));
                 size++;
-                size += writeString(destination, target.mWord);
+                size += CharEncoding.writeString(destination, target.mWord);
             }
         }
 
-- 
GitLab