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
20 * 長さ0で空の{@link DecodedContent}がセットされる。
\r
22 public ContentBuilderUCS2(){
\r
29 * 長さ0で空の{@link DecodedContent}がセットされる。
\r
30 * @param capacity 初期容量
\r
31 * @throws NegativeArraySizeException 容量指定が負。
\r
33 public ContentBuilderUCS2(int capacity)
\r
34 throws NegativeArraySizeException{
\r
42 * サロゲートペア文字(上位,下位)をUTF-16BEバイト列に変換する。
\r
46 public static byte[] charToUTF16(char ch){
\r
47 byte[] result = new byte[2];
\r
48 result[0] = (byte)(ch >> 8);
\r
49 result[1] = (byte)(ch & 0xff);
\r
58 private void initImpl(){
\r
59 this.content.init();
\r
67 protected void init(){
\r
74 * @param seq {@inheritDoc}
\r
75 * @throws DecodeException {@inheritDoc}
\r
78 public void charContent(CharSequence seq)
\r
79 throws DecodeException{
\r
82 int length = seq.length();
\r
85 for(int pos = 0; pos < length; pos++){
\r
86 char ch = seq.charAt(pos);
\r
88 if( ! Character.isHighSurrogate(ch)
\r
89 && ! Character.isLowSurrogate (ch) ){
\r
94 CharSequence chopped = seq.subSequence(startPos, pos);
\r
95 this.content.append(chopped);
\r
99 byte[] barr = charToUTF16(ch);
\r
100 for(byte bval : barr){
\r
101 this.content.addDecodeError(bval);
\r
105 if(startPos < length){
\r
106 CharSequence chopped = seq.subSequence(startPos, length);
\r
107 this.content.append(chopped);
\r
115 * @param errorArray {@inheritDoc}
\r
116 * @param offset {@inheritDoc}
\r
117 * @param length {@inheritDoc}
\r
118 * @throws DecodeException {@inheritDoc}
\r
121 public void decodingError(byte[] errorArray, int offset, int length)
\r
122 throws DecodeException{
\r
123 int limit = offset + length;
\r
125 for(int bpos = offset; bpos < limit; bpos++){
\r
126 byte bval = errorArray[bpos];
\r
127 this.content.addDecodeError(bval);
\r