-/*\r
- * stream decoder for Shift_JIS\r
- *\r
- * Copyright(c) 2009 olyutorskii\r
- * $Id: SjisDecoder.java 894 2009-11-04 07:26:59Z olyutorskii $\r
- */\r
-\r
-package jp.sourceforge.jindolf.parser;\r
-\r
-import java.io.IOException;\r
-import java.nio.ByteBuffer;\r
-import java.nio.charset.CoderResult;\r
-\r
-/**\r
- * Shift_JISバイト列のデコードに特化した、{@link StreamDecoder}の派生クラス。\r
- * Java実行系の細かな仕様差異による\r
- * デコードエラー出現パターンゆらぎの正規化も行う。\r
- * 0x5Cが{@literal U+005C}にデコードされるか\r
- * {@literal U+00A5}にデコードされるかはJava実行系の実装依存。\r
- * @see <a href="http://www.iana.org/assignments/character-sets">\r
- * CHARACTER SETS</a>\r
- */\r
-public class SjisDecoder extends StreamDecoder{\r
-\r
- /** 入力バッファに必要な最小サイズ(={@value})。 */\r
- public static final int MIN_INBUFSZ = ShiftJis.MAX_BYTES_PER_CHAR * 2 + 1;\r
-\r
- static{\r
- assert MIN_INBUFSZ <= BYTEBUF_DEFSZ;\r
- }\r
-\r
- /**\r
- * コンストラクタ。\r
- */\r
- public SjisDecoder(){\r
- this(BYTEBUF_DEFSZ, CHARBUF_DEFSZ);\r
- return;\r
- }\r
-\r
- /**\r
- * コンストラクタ。\r
- * @param inbuf_sz 入力バッファサイズ\r
- * @param outbuf_sz 出力バッファサイズ\r
- * @throws IllegalArgumentException バッファサイズが小さすぎる。\r
- */\r
- public SjisDecoder(int inbuf_sz, int outbuf_sz)\r
- throws IllegalArgumentException{\r
- super(ShiftJis.CHARSET.newDecoder(), inbuf_sz, outbuf_sz);\r
- if(inbuf_sz < MIN_INBUFSZ){\r
- throw new IllegalArgumentException();\r
- }\r
- return;\r
- }\r
-\r
- /**\r
- * 1バイトのエラーを2バイトに統合できないか試す。\r
- * @param result デコード異常系\r
- * @return 修正されたデコード異常系。修正がなければ引数と同じものを返す。\r
- * @throws IOException 入力エラー\r
- */\r
- private CoderResult modify1ByteError(CoderResult result)\r
- throws IOException {\r
- ByteBuffer inbuffer = getByteBuffer();\r
-\r
- int currPos;\r
- int nextPos;\r
-\r
- currPos = inbuffer.position();\r
- nextPos = currPos + 1;\r
- if(nextPos >= inbuffer.limit()){\r
- readByteBuffer();\r
- currPos = inbuffer.position();\r
- nextPos = currPos + 1;\r
- }\r
-\r
- // 入力バイト列の最後がこのデコードエラーだった場合。\r
- if(nextPos >= inbuffer.limit()) return result;\r
-\r
- byte curr = inbuffer.get(currPos); // 絶対的get\r
- byte next = inbuffer.get(nextPos);\r
- if( ShiftJis.isShiftJIS(curr, next) ){\r
- return CoderResult.unmappableForLength(2);\r
- }\r
-\r
- return result;\r
- }\r
-\r
- /**\r
- * 2バイトのエラーを1バイトに分割できないか試す。\r
- * ※ バイト列"FF:32" のShift_JISデコードに際して、\r
- * 2バイト長のデコードエラーを返す1.6系実行系が存在する。\r
- * @param result デコード異常系\r
- * @return 修正されたデコード異常系。修正がなければ引数と同じものを返す。\r
- */\r
- private CoderResult modify2ByteError(CoderResult result){\r
- ByteBuffer inbuffer = getByteBuffer();\r
-\r
- int currPos;\r
- int nextPos;\r
-\r
- currPos = inbuffer.position();\r
- nextPos = currPos + 1;\r
-\r
- byte curr = inbuffer.get(currPos); // 絶対的get\r
- byte next = inbuffer.get(nextPos);\r
- if( ! ShiftJis.isShiftJIS(curr, next) ){\r
- return CoderResult.malformedForLength(1);\r
- }\r
-\r
- return result;\r
- }\r
-\r
- /**\r
- * {@inheritDoc}\r
- * ライブラリ実装によるシフトJISデコードエラー出現パターンの\r
- * 差異を吸収する。\r
- * @param result {@inheritDoc}\r
- * @return {@inheritDoc}\r
- * @throws IOException {@inheritDoc}\r
- */\r
- @Override\r
- protected int chopErrorSequence(CoderResult result)\r
- throws IOException{\r
- int errorLength = result.length();\r
-\r
- CoderResult newResult;\r
- if(errorLength == 1){\r
- newResult = modify1ByteError(result);\r
- }else if(errorLength == 2){\r
- newResult = modify2ByteError(result);\r
- }else{\r
- assert false;\r
- return -1;\r
- }\r
-\r
- return super.chopErrorSequence(newResult);\r
- }\r
-\r
-}\r
+/*
+ * stream decoder for Shift_JIS
+ *
+ * License : The MIT License
+ * Copyright(c) 2009 olyutorskii
+ */
+
+package jp.sourceforge.jindolf.parser;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.charset.CoderResult;
+
+/**
+ * Shift_JISバイト列のデコードに特化した、{@link StreamDecoder}の派生クラス。
+ * Java実行系の細かな仕様差異による
+ * デコードエラー出現パターンゆらぎの正規化も行う。
+ * 0x5Cが{@literal U+005C}にデコードされるか
+ * {@literal U+00A5}にデコードされるかはJava実行系の実装依存。
+ * @see <a href="http://www.iana.org/assignments/character-sets">
+ * CHARACTER SETS</a>
+ */
+public class SjisDecoder extends StreamDecoder{
+
+ /** 入力バッファに必要な最小サイズ(={@value})。 */
+ public static final int MIN_INBUFSZ = ShiftJis.MAX_BYTES_PER_CHAR * 2 + 1;
+
+ static{
+ assert MIN_INBUFSZ <= BYTEBUF_DEFSZ;
+ }
+
+ /**
+ * コンストラクタ。
+ */
+ public SjisDecoder(){
+ this(BYTEBUF_DEFSZ, CHARBUF_DEFSZ);
+ return;
+ }
+
+ /**
+ * コンストラクタ。
+ * @param inbufSz 入力バッファサイズ
+ * @param outbufSz 出力バッファサイズ
+ * @throws IllegalArgumentException バッファサイズが小さすぎる。
+ */
+ public SjisDecoder(int inbufSz, int outbufSz)
+ throws IllegalArgumentException{
+ super(ShiftJis.CHARSET.newDecoder(), inbufSz, outbufSz);
+ if(inbufSz < MIN_INBUFSZ){
+ throw new IllegalArgumentException();
+ }
+ return;
+ }
+
+ /**
+ * 1バイトのエラーを2バイトに統合できないか試す。
+ * @param result デコード異常系
+ * @return 修正されたデコード異常系。修正がなければ引数と同じものを返す。
+ * @throws IOException 入力エラー
+ */
+ private CoderResult modify1ByteError(CoderResult result)
+ throws IOException {
+ ByteBuffer inbuffer = getByteBuffer();
+
+ int currPos;
+ int nextPos;
+
+ currPos = inbuffer.position();
+ nextPos = currPos + 1;
+ if(nextPos >= inbuffer.limit()){
+ readByteBuffer();
+ currPos = inbuffer.position();
+ nextPos = currPos + 1;
+ }
+
+ // 入力バイト列の最後がこのデコードエラーだった場合。
+ if(nextPos >= inbuffer.limit()) return result;
+
+ byte curr = inbuffer.get(currPos); // 絶対的get
+ byte next = inbuffer.get(nextPos);
+ if( ShiftJis.isShiftJIS(curr, next) ){
+ return CoderResult.unmappableForLength(2);
+ }
+
+ return result;
+ }
+
+ /**
+ * 2バイトのエラーを1バイトに分割できないか試す。
+ * ※ バイト列"FF:32" のShift_JISデコードに際して、
+ * 2バイト長のデコードエラーを返す1.6系実行系が存在する。
+ * @param result デコード異常系
+ * @return 修正されたデコード異常系。修正がなければ引数と同じものを返す。
+ */
+ private CoderResult modify2ByteError(CoderResult result){
+ ByteBuffer inbuffer = getByteBuffer();
+
+ int currPos;
+ int nextPos;
+
+ currPos = inbuffer.position();
+ nextPos = currPos + 1;
+
+ byte curr = inbuffer.get(currPos); // 絶対的get
+ byte next = inbuffer.get(nextPos);
+ if( ! ShiftJis.isShiftJIS(curr, next) ){
+ return CoderResult.malformedForLength(1);
+ }
+
+ return result;
+ }
+
+ /**
+ * {@inheritDoc}
+ * ライブラリ実装によるシフトJISデコードエラー出現パターンの
+ * 差異を吸収する。
+ * @param result {@inheritDoc}
+ * @return {@inheritDoc}
+ * @throws IOException {@inheritDoc}
+ */
+ @Override
+ protected int chopErrorSequence(CoderResult result)
+ throws IOException{
+ int errorLength = result.length();
+
+ CoderResult newResult;
+ if(errorLength == 1){
+ newResult = modify1ByteError(result);
+ }else if(errorLength == 2){
+ newResult = modify2ByteError(result);
+ }else{
+ assert false;
+ return -1;
+ }
+
+ return super.chopErrorSequence(newResult);
+ }
+
+}