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