refactor: make javabin use ByteUtils for encoding/decoding

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1236860 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yonik Seeley 2012-01-27 19:45:26 +00:00
parent 02d4bd91df
commit 7784c42dd1
1 changed files with 6 additions and 28 deletions

View File

@ -16,6 +16,7 @@
*/
package org.apache.solr.common.util;
import org.apache.noggit.CharArr;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
@ -470,39 +471,16 @@ public class JavaBinCodec {
}
byte[] bytes;
char[] chars;
CharArr arr = new CharArr();
public String readStr(FastInputStream dis) throws IOException {
int sz = readSize(dis);
if (chars == null || chars.length < sz) chars = new char[sz];
if (bytes == null || bytes.length < sz) bytes = new byte[sz];
dis.readFully(bytes, 0, sz);
int outUpto=0;
for (int i = 0; i < sz;) {
final int b = bytes[i++]&0xff;
final int ch;
if (b < 0xc0) {
assert b < 0x80;
ch = b;
} else if (b < 0xe0) {
ch = ((b&0x1f)<<6) + (bytes[i++]&0x3f);
} else if (b < 0xf0) {
ch = ((b&0xf)<<12) + ((bytes[i++]&0x3f)<<6) + (bytes[i++]&0x3f);
} else {
assert b < 0xf8;
ch = ((b&0x7)<<18) + ((bytes[i++]&0x3f)<<12) + ((bytes[i++]&0x3f)<<6) + (bytes[i++]&0x3f);
}
if (ch <= 0xFFFF) {
// target is a character <= 0xFFFF
chars[outUpto++] = (char) ch;
} else {
// target is a character in range 0xFFFF - 0x10FFFF
final int chHalf = ch - 0x10000;
chars[outUpto++] = (char) ((chHalf >> 0xA) + 0xD800);
chars[outUpto++] = (char) ((chHalf & 0x3FF) + 0xDC00);
}
}
return new String(chars, 0, outUpto);
arr.reset();
ByteUtils.UTF8toUTF16(bytes, 0, sz, arr);
return arr.toString();
}
public void writeInt(int val) throws IOException {