am 47015f20: Merge "Consolidate CharEncoding.writeString and BinaryDictIOUtils.writeString."

* commit '47015f205ab9035a6d49a68393f339c5a39d4d71':
  Consolidate CharEncoding.writeString and BinaryDictIOUtils.writeString.
main
Satoshi Kataoka 2013-10-16 00:35:45 -07:00 committed by Android Git Automerger
commit b2fe44490f
2 changed files with 9 additions and 32 deletions

View File

@ -225,20 +225,26 @@ public final class BinaryDictDecoderUtils {
* *
* @param buffer the OutputStream to write to. * @param buffer the OutputStream to write to.
* @param word the string to write. * @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(); final int length = word.length();
int written = 0;
for (int i = 0; i < length; i = word.offsetByCodePoints(i, 1)) { for (int i = 0; i < length; i = word.offsetByCodePoints(i, 1)) {
final int codePoint = word.codePointAt(i); final int codePoint = word.codePointAt(i);
if (1 == getCharSize(codePoint)) { final int charSize = getCharSize(codePoint);
if (1 == charSize) {
buffer.write((byte) codePoint); buffer.write((byte) codePoint);
} else { } else {
buffer.write((byte) (0xFF & (codePoint >> 16))); buffer.write((byte) (0xFF & (codePoint >> 16)));
buffer.write((byte) (0xFF & (codePoint >> 8))); buffer.write((byte) (0xFF & (codePoint >> 8)));
buffer.write((byte) (0xFF & codePoint)); buffer.write((byte) (0xFF & codePoint));
} }
written += charSize;
} }
buffer.write(FormatSpec.PTNODE_CHARACTERS_TERMINATOR); buffer.write(FormatSpec.PTNODE_CHARACTERS_TERMINATOR);
written += FormatSpec.PTNODE_TERMINATOR_SIZE;
return written;
} }
/** /**

View File

@ -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. * Write a PtNode to an output stream from a PtNodeInfo.
* A PtNode is an in-memory representation of a node in the patricia trie. * 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( destination.write((byte)BinaryDictEncoderUtils.makeShortcutFlags(
shortcutIterator.hasNext(), target.mFrequency)); shortcutIterator.hasNext(), target.mFrequency));
size++; size++;
size += writeString(destination, target.mWord); size += CharEncoding.writeString(destination, target.mWord);
} }
} }