OSDN Git Service

Merge commit '2234b50cfbe7c86237086a3bf4e62397814a390e'
[jindolf/JinParser.git] / src / main / java / jp / sourceforge / jindolf / parser / ContentBuilderUCS2.java
index 4f1e7d5..5ab7832 100644 (file)
-/*\r
- * content builder for UTF-8 (UCS2 only)\r
- *\r
- * Copyright(c) 2010 olyutorskii\r
- * $Id: ContentBuilderUCS2.java 1001 2010-03-15 12:09:35Z olyutorskii $\r
- */\r
-\r
-package jp.sourceforge.jindolf.parser;\r
-\r
-/**\r
- * "UTF-8"エンコーディング用デコードハンドラ。\r
- * {@link StreamDecoder}からの通知に従い、\r
- * {@link DecodedContent}へとデコードする。\r
- * UCS-4はUTF-16エラー扱い。\r
- */\r
-public class ContentBuilderUCS2 extends ContentBuilder{\r
-\r
-    /**\r
-     * サロゲートペア文字(上位,下位)をUTF-16BEバイト列に変換する。\r
-     * @param ch 文字\r
-     * @return UTF-8バイト列\r
-     */\r
-    public static byte[] charToUTF16(char ch){\r
-        byte[] result = new byte[2];\r
-        result[0] = (byte)(ch >> 8);\r
-        result[1] = (byte)(ch & 0xff);\r
-\r
-        return result;\r
-    }\r
-\r
-    /**\r
-     * コンストラクタ。\r
-     * 長さ0で空の{@link DecodedContent}がセットされる。\r
-     */\r
-    public ContentBuilderUCS2(){\r
-        this(128);\r
-        return;\r
-    }\r
-\r
-    /**\r
-     * コンストラクタ。\r
-     * 長さ0で空の{@link DecodedContent}がセットされる。\r
-     * @param capacity 初期容量\r
-     * @throws NegativeArraySizeException 容量指定が負。\r
-     */\r
-    public ContentBuilderUCS2(int capacity)\r
-            throws NegativeArraySizeException{\r
-        super(capacity);\r
-        initImpl();\r
-        return;\r
-    }\r
-\r
-    /**\r
-     * デコード処理の初期化下請。\r
-     */\r
-    private void initImpl(){\r
-        this.content.init();\r
-        return;\r
-    }\r
-\r
-    /**\r
-     * デコード処理の初期化。\r
-     */\r
-    @Override\r
-    protected void init(){\r
-        initImpl();\r
-        return;\r
-    }\r
-\r
-    /**\r
-     * {@inheritDoc}\r
-     * @param seq {@inheritDoc}\r
-     * @throws DecodeException {@inheritDoc}\r
-     */\r
-    public void charContent(CharSequence seq)\r
-            throws DecodeException{\r
-        flushError();\r
-\r
-        int length = seq.length();\r
-        int startPos = 0;\r
-\r
-        for(int pos = 0; pos < length; pos++){\r
-            char ch = seq.charAt(pos);\r
-\r
-            if(   ! Character.isHighSurrogate(ch)\r
-               && ! Character.isLowSurrogate (ch) ){\r
-                continue;\r
-            }\r
-\r
-            if(startPos < pos){\r
-                CharSequence chopped = seq.subSequence(startPos, pos);\r
-                this.content.append(chopped);\r
-                startPos = pos + 1;\r
-            }\r
-\r
-            byte[] barr = charToUTF16(ch);\r
-            for(byte bval : barr){\r
-                this.content.addDecodeError(bval);\r
-            }\r
-        }\r
-\r
-        if(startPos < length){\r
-            CharSequence chopped = seq.subSequence(startPos, length);\r
-            this.content.append(chopped);\r
-        }\r
-\r
-        return;\r
-    }\r
-\r
-    /**\r
-     * {@inheritDoc}\r
-     * @param errorArray {@inheritDoc}\r
-     * @param offset {@inheritDoc}\r
-     * @param length {@inheritDoc}\r
-     * @throws DecodeException {@inheritDoc}\r
-     */\r
-    public void decodingError(byte[] errorArray, int offset, int length)\r
-            throws DecodeException{\r
-        int limit = offset + length;\r
-\r
-        for(int bpos = offset; bpos < limit; bpos++){\r
-            byte bval = errorArray[bpos];\r
-            this.content.addDecodeError(bval);\r
-        }\r
-\r
-        return;\r
-    }\r
-\r
-}\r
+/*
+ * content builder for UTF-8
+ *
+ * License : The MIT License
+ * Copyright(c) 2010 olyutorskii
+ */
+
+package jp.sourceforge.jindolf.parser;
+
+/**
+ * "UTF-8"エンコーディング用デコードハンドラ。
+ * {@link StreamDecoder}からの通知に従い、
+ * {@link DecodedContent}へとデコードする。
+ */
+public class ContentBuilderUCS2 extends ContentBuilder{
+
+    private static final int DEF_BUF_SZ = 128;
+
+
+    /**
+     * コンストラクタ。
+     * 長さ0で空の{@link DecodedContent}がセットされる。
+     */
+    public ContentBuilderUCS2(){
+        this(DEF_BUF_SZ);
+        return;
+    }
+
+    /**
+     * コンストラクタ。
+     * 長さ0で空の{@link DecodedContent}がセットされる。
+     * @param capacity 初期容量
+     * @throws NegativeArraySizeException 容量指定が負。
+     */
+    public ContentBuilderUCS2(int capacity)
+            throws NegativeArraySizeException{
+        super(capacity);
+        initImpl();
+        return;
+    }
+
+
+    /**
+     * デコード処理の初期化下請。
+     */
+    private void initImpl(){
+        this.getContent().init();
+        return;
+    }
+
+    /**
+     * デコード処理の初期化。
+     */
+    @Override
+    protected void init(){
+        initImpl();
+        return;
+    }
+
+    /**
+     * {@inheritDoc}
+     * @param seq {@inheritDoc}
+     * @throws DecodeException {@inheritDoc}
+     */
+    @Override
+    public void charContent(CharSequence seq)
+            throws DecodeException{
+        flushError();
+        getContent().append(seq);
+        return;
+    }
+
+    /**
+     * {@inheritDoc}
+     * @param errorArray {@inheritDoc}
+     * @param offset {@inheritDoc}
+     * @param length {@inheritDoc}
+     * @throws DecodeException {@inheritDoc}
+     */
+    @Override
+    public void decodingError(byte[] errorArray, int offset, int length)
+            throws DecodeException{
+        int limit = offset + length;
+
+        for(int bpos = offset; bpos < limit; bpos++){
+            byte bval = errorArray[bpos];
+            getContent().addDecodeError(bval);
+        }
+
+        return;
+    }
+
+}