Consolidate CharEncoding.writeString and BinaryDictIOUtils.writeString.

Change-Id: I6f990fd84e7f08fd1149198c33d8bbf1cac8e078
This commit is contained in:
Yuichiro Hanada 2013-10-16 00:08:02 +09:00
parent b067be0e5f
commit 1557de7aa4
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 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;
}
/**

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.
* 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);
}
}