2 * content builder for UTF-8 (UCS2 only)
\r
4 * License : The MIT License
\r
5 * Copyright(c) 2010 olyutorskii
\r
8 package jp.sourceforge.jindolf.parser;
\r
11 * "UTF-8"エンコーディング用デコードハンドラ。
\r
12 * {@link StreamDecoder}からの通知に従い、
\r
13 * {@link DecodedContent}へとデコードする。
\r
14 * UCS-4はUTF-16エラー扱い。
\r
16 public class ContentBuilderUCS2 extends ContentBuilder{
\r
18 private static final int DEF_BUF_SZ = 128;
\r
23 * 長さ0で空の{@link DecodedContent}がセットされる。
\r
25 public ContentBuilderUCS2(){
\r
32 * 長さ0で空の{@link DecodedContent}がセットされる。
\r
33 * @param capacity 初期容量
\r
34 * @throws NegativeArraySizeException 容量指定が負。
\r
36 public ContentBuilderUCS2(int capacity)
\r
37 throws NegativeArraySizeException{
\r
45 * サロゲートペア文字(上位,下位)をUTF-16BEバイト列に変換する。
\r
49 public static byte[] charToUTF16(char ch){
\r
50 byte[] result = new byte[2];
\r
51 result[0] = (byte)(ch >> 8);
\r
52 result[1] = (byte)(ch & 0xff);
\r
61 private void initImpl(){
\r
62 this.content.init();
\r
70 protected void init(){
\r
77 * @param seq {@inheritDoc}
\r
78 * @throws DecodeException {@inheritDoc}
\r
81 public void charContent(CharSequence seq)
\r
82 throws DecodeException{
\r
85 int length = seq.length();
\r
88 for(int pos = 0; pos < length; pos++){
\r
89 char ch = seq.charAt(pos);
\r
91 if( ! Character.isHighSurrogate(ch)
\r
92 && ! Character.isLowSurrogate (ch) ){
\r
97 CharSequence chopped = seq.subSequence(startPos, pos);
\r
98 this.content.append(chopped);
\r
102 byte[] barr = charToUTF16(ch);
\r
103 for(byte bval : barr){
\r
104 this.content.addDecodeError(bval);
\r
108 if(startPos < length){
\r
109 CharSequence chopped = seq.subSequence(startPos, length);
\r
110 this.content.append(chopped);
\r
118 * @param errorArray {@inheritDoc}
\r
119 * @param offset {@inheritDoc}
\r
120 * @param length {@inheritDoc}
\r
121 * @throws DecodeException {@inheritDoc}
\r
124 public void decodingError(byte[] errorArray, int offset, int length)
\r
125 throws DecodeException{
\r
126 int limit = offset + length;
\r
128 for(int bpos = offset; bpos < limit; bpos++){
\r
129 byte bval = errorArray[bpos];
\r
130 this.content.addDecodeError(bval);
\r