-/*\r
- * decoded source\r
- *\r
- * Copyright(c) 2009 olyutorskii\r
- * $Id: DecodedContent.java 894 2009-11-04 07:26:59Z olyutorskii $\r
- */\r
-\r
-package jp.sourceforge.jindolf.parser;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Collections;\r
-import java.util.List;\r
-import java.util.RandomAccess;\r
-\r
-/**\r
- * ShiftJISデコードエラー情報を含む再利用可能な文字列。\r
- * デコードエラーを起こした箇所は代替文字{@link #ALTCHAR}で置き換えられる。\r
- * マルチスレッドには非対応。\r
- * UCS-4コードポイントには未対応。\r
- */\r
-public class DecodedContent\r
- implements CharSequence,\r
- Appendable {\r
-\r
- /**\r
- * 代替文字。\r
- * {@literal HTMLで使うなら < や > や & や " や ' はやめて!}\r
- */\r
- public static final char ALTCHAR = '?';\r
-\r
- private static final List<DecodeErrorInfo> EMPTY_LIST =\r
- Collections.emptyList();\r
-\r
- private static final int BSEARCH_THRESHOLD = 16;\r
-\r
- static{\r
- assert ALTCHAR != '<';\r
- assert ALTCHAR != '>';\r
- assert ALTCHAR != '&';\r
- assert ALTCHAR != '"';\r
- assert ALTCHAR != '\'';\r
- assert ALTCHAR != '\\';\r
- }\r
-\r
- /**\r
- * 与えられた文字位置を含むか、またはそれ以降で最も小さな位置情報を持つ\r
- * デコードエラーのインデックス位置を返す。※リニアサーチ版。\r
- * @param errList デコードエラーのリスト\r
- * @param startPos 文字位置\r
- * @return 0から始まるリスト内の位置。\r
- * 一致する文字位置がなければ挿入ポイント。\r
- */\r
- protected static int lsearchErrorIndex(List<DecodeErrorInfo> errList,\r
- int startPos){\r
- // assert errList instanceof RandomAccess;\r
-\r
- int errSize = errList.size();\r
-\r
- int idx;\r
- for(idx = 0; idx < errSize; idx++){\r
- DecodeErrorInfo einfo = errList.get(idx);\r
- int errPos = einfo.getCharPosition();\r
- if(startPos <= errPos) break;\r
- }\r
-\r
- return idx;\r
- }\r
-\r
- /**\r
- * 与えられた文字位置を含むか、またはそれ以降で最も小さな位置情報を持つ\r
- * デコードエラーのインデックス位置を返す。※バイナリサーチ版。\r
- * @param errList デコードエラーのリスト\r
- * @param startPos 文字位置\r
- * @return 0から始まるリスト内の位置。\r
- * 一致する文字位置がなければ挿入ポイント。\r
- */\r
- protected static int bsearchErrorIndex(List<DecodeErrorInfo> errList,\r
- int startPos){\r
- // assert errList instanceof RandomAccess;\r
-\r
- int floor = 0;\r
- int roof = errList.size() - 1;\r
-\r
- while(floor <= roof){\r
- int midpoint = (floor + roof) / 2; // 切り捨て\r
- DecodeErrorInfo einfo = errList.get(midpoint);\r
- int cmp = einfo.getCharPosition() - startPos;\r
-\r
- if(cmp == 0) return midpoint;\r
-\r
- if (cmp < 0) floor = midpoint + 1;\r
- else if(cmp > 0) roof = midpoint - 1;\r
- }\r
-\r
- return floor;\r
- }\r
-\r
- /**\r
- * 与えられた文字位置を含むか、またはそれ以降で最も小さな位置情報を持つ\r
- * デコードエラーのインデックス位置を返す。\r
- * 要素数の増減に応じてリニアサーチとバイナリサーチを使い分ける。\r
- * @param errList デコードエラーのリスト\r
- * @param startPos 文字位置\r
- * @return 0から始まるリスト内の位置。\r
- * 一致する文字位置がなければ挿入ポイント。\r
- */\r
- protected static int searchErrorIndex(List<DecodeErrorInfo> errList,\r
- int startPos){\r
- int result;\r
-\r
- int errSize = errList.size();\r
- if(errSize < BSEARCH_THRESHOLD){\r
- // linear-search\r
- result = lsearchErrorIndex(errList, startPos);\r
- }else{\r
- // binary-search\r
- result = bsearchErrorIndex(errList, startPos);\r
- }\r
-\r
- return result;\r
- }\r
-\r
- /**\r
- * ギャップ情報が加味されたデコードエラー情報を、\r
- * 範囲指定込みで指定エラーリストに追加転記する。\r
- * 追加先エラーリストがnullだった場合、必要に応じてエラーリストが生成され\r
- * 戻り値となる場合がありうる。\r
- * @param sourceContent 元の文字列\r
- * @param startPos 範囲開始位置\r
- * @param endPos 範囲終了位置\r
- * @param targetError 追加先エラーリスト。nullでもよい。\r
- * @param gap ギャップ量\r
- * @return 引数targetErrorもしくは新規生成されたリストを返す。\r
- */\r
- protected static List<DecodeErrorInfo>\r
- appendGappedErrorInfo(DecodedContent sourceContent,\r
- int startPos, int endPos,\r
- List<DecodeErrorInfo> targetError,\r
- int gap){\r
- List<DecodeErrorInfo> sourceError = sourceContent.decodeError;\r
- List<DecodeErrorInfo> result = targetError;\r
-\r
- int startErrorIdx = searchErrorIndex(sourceError, startPos);\r
- int endErrorIdx = sourceError.size() - 1;\r
- assert endErrorIdx >= 0;\r
-\r
- for(int index = startErrorIdx; index <= endErrorIdx; index++){\r
- DecodeErrorInfo einfo = sourceError.get(index);\r
- int pos = einfo.getCharPosition();\r
- if(pos < startPos) continue;\r
- if(pos >= endPos) break;\r
- DecodeErrorInfo newInfo = einfo.createGappedClone(gap);\r
- if(result == null){\r
- result = createErrorList();\r
- }\r
- result.add(newInfo);\r
- }\r
-\r
- return result;\r
- }\r
-\r
- /**\r
- * エラー格納用リストを生成する。\r
- * @return リスト\r
- */\r
- private static List<DecodeErrorInfo> createErrorList(){\r
- List<DecodeErrorInfo> result = new ArrayList<DecodeErrorInfo>();\r
- return result;\r
- }\r
-\r
- static{\r
- assert createErrorList() instanceof RandomAccess;\r
- }\r
-\r
- private final StringBuilder rawContent = new StringBuilder();\r
-\r
- private List<DecodeErrorInfo> decodeError;\r
-\r
- /**\r
- * コンストラクタ。\r
- */\r
- public DecodedContent(){\r
- this("");\r
- return;\r
- }\r
-\r
- /**\r
- * コンストラクタ。\r
- * @param seq 初期化文字列\r
- * @throws NullPointerException 引数がnull\r
- */\r
- public DecodedContent(CharSequence seq) throws NullPointerException{\r
- super();\r
- if(seq == null) throw new NullPointerException();\r
- initImpl();\r
- this.rawContent.append(seq);\r
- return;\r
- }\r
-\r
- /**\r
- * コンストラクタ。\r
- * @param capacity 文字数の初期容量\r
- * @throws NegativeArraySizeException 容量が負の値\r
- */\r
- public DecodedContent(int capacity) throws NegativeArraySizeException{\r
- super();\r
- if(capacity < 0) throw new NegativeArraySizeException();\r
- initImpl();\r
- this.rawContent.ensureCapacity(capacity);\r
- return;\r
- }\r
-\r
- /**\r
- * 初期化下請け。\r
- * 長さ0の文字列&デコードエラー無しの状態になる。\r
- */\r
- private void initImpl(){\r
- this.rawContent.setLength(0);\r
-\r
- if(this.decodeError != null){\r
- this.decodeError.clear();\r
- }\r
-\r
- return;\r
- }\r
-\r
- /**\r
- * 事前にキャパシティを確保する。\r
- * 指定されたキャパシティの範囲内で再割り当てが起きないことを保証する。\r
- * @param minimumCapacity キャラクタ単位のキャパシティ長。\r
- */\r
- public void ensureCapacity(int minimumCapacity){\r
- this.rawContent.ensureCapacity(minimumCapacity);\r
- return;\r
- }\r
-\r
- /**\r
- * 初期化。\r
- * 長さ0の文字列&デコードエラー無しの状態になる。\r
- * コンストラクタで新インスタンスを作るより低コスト。\r
- */\r
- public void init(){\r
- initImpl();\r
- return;\r
- }\r
-\r
- /**\r
- * デコードエラーを含むか判定する。\r
- * @return デコードエラーを含むならtrue\r
- */\r
- public boolean hasDecodeError(){\r
- if(this.decodeError == null) return false;\r
- if(this.decodeError.isEmpty()) return false;\r
- return true;\r
- }\r
-\r
- /**\r
- * デコードエラーの一覧を取得する。\r
- * @return デコードエラーの一覧\r
- */\r
- public List<DecodeErrorInfo> getDecodeErrorList(){\r
- if( ! hasDecodeError() ){\r
- return EMPTY_LIST;\r
- }\r
- return Collections.unmodifiableList(this.decodeError);\r
- }\r
-\r
- /**\r
- * 生の文字列を得る。\r
- * 高速なCharSequenceアクセス用途。\r
- * @return 生の文字列。\r
- */\r
- public CharSequence getRawContent(){\r
- return this.rawContent;\r
- }\r
-\r
- /**\r
- * 指定された位置の文字を変更する。\r
- * @param index 文字位置\r
- * @param ch 文字\r
- * @throws IndexOutOfBoundsException 不正な位置指定\r
- */\r
- public void setCharAt(int index, char ch)\r
- throws IndexOutOfBoundsException{\r
- this.rawContent.setCharAt(index, ch);\r
- return;\r
- }\r
-\r
- /**\r
- * {@inheritDoc}\r
- * @param index {@inheritDoc}\r
- * @return {@inheritDoc}\r
- */\r
- public char charAt(int index){\r
- return this.rawContent.charAt(index);\r
- }\r
-\r
- /**\r
- * {@inheritDoc}\r
- * @return {@inheritDoc}\r
- */\r
- public int length(){\r
- return this.rawContent.length();\r
- }\r
-\r
- /**\r
- * {@inheritDoc}\r
- * @param start {@inheritDoc}\r
- * @param end {@inheritDoc}\r
- * @return {@inheritDoc}\r
- */\r
- public CharSequence subSequence(int start, int end){\r
- return this.rawContent.subSequence(start, end);\r
- }\r
-\r
- /**\r
- * 範囲指定されたサブコンテントを切り出す。\r
- * サブコンテントにはデコードエラー情報が引き継がれる。\r
- * @param start 開始位置\r
- * @param end 終了位置\r
- * @return サブコンテント\r
- * @throws IndexOutOfBoundsException start または end が負の値の場合、\r
- * end が length() より大きい場合、あるいは start が end より大きい場合\r
- */\r
- public DecodedContent subContent(int start, int end)\r
- throws IndexOutOfBoundsException{\r
- int length = end - start;\r
- if(length < 0) throw new IndexOutOfBoundsException();\r
- DecodedContent result = new DecodedContent(length);\r
- result.append(this, start, end);\r
- return result;\r
- }\r
-\r
- /**\r
- * 文字を追加する。\r
- * @param letter 追加する文字\r
- * @return thisオブジェクト\r
- */\r
- public DecodedContent append(char letter){\r
- this.rawContent.append(letter);\r
- return this;\r
- }\r
-\r
- /**\r
- * 文字列を追加する。\r
- * @param seq 追加する文字列\r
- * @return thisオブジェクト\r
- */\r
- public DecodedContent append(CharSequence seq){\r
- if(seq == null){\r
- this.rawContent.append("null");\r
- }else if(seq instanceof DecodedContent){\r
- append((DecodedContent)seq, 0, seq.length());\r
- }else{\r
- this.rawContent.append(seq);\r
- }\r
- return this;\r
- }\r
-\r
- /**\r
- * 文字列を追加する。\r
- * @param seq 追加する文字列\r
- * @param startPos 開始位置\r
- * @param endPos 終了位置\r
- * @return thisオブジェクト\r
- * @throws IndexOutOfBoundsException 範囲指定が変。\r
- */\r
- public DecodedContent append(CharSequence seq,\r
- int startPos, int endPos)\r
- throws IndexOutOfBoundsException{\r
- if(seq == null){\r
- this.rawContent.append("null", startPos, endPos);\r
- }else if(seq instanceof DecodedContent){\r
- append((DecodedContent)seq, startPos, endPos);\r
- }else{\r
- this.rawContent.append(seq, startPos, endPos);\r
- }\r
-\r
- return this;\r
- }\r
-\r
- /**\r
- * 文字列を追加する。\r
- * @param source 追加する文字列\r
- * @param startPos 開始位置\r
- * @param endPos 終了位置\r
- * @return thisオブジェクト\r
- * @throws IndexOutOfBoundsException 範囲指定が変。\r
- */\r
- public DecodedContent append(DecodedContent source,\r
- int startPos, int endPos)\r
- throws IndexOutOfBoundsException{\r
- if(source == null){\r
- return append("null", startPos, endPos);\r
- }\r
-\r
- int gap = startPos - this.rawContent.length();\r
-\r
- this.rawContent.append(source.rawContent, startPos, endPos);\r
-\r
- if( ! source.hasDecodeError() ) return this;\r
-\r
- List<DecodeErrorInfo> targetErrorList;\r
- if(source != this) targetErrorList = this.decodeError;\r
- else targetErrorList = null;\r
-\r
- targetErrorList = appendGappedErrorInfo(source,\r
- startPos, endPos,\r
- targetErrorList,\r
- gap);\r
-\r
- if(targetErrorList == null) return this;\r
- if(targetErrorList == this.decodeError) return this;\r
-\r
- if(this.decodeError == null){\r
- this.decodeError = targetErrorList;\r
- }else{\r
- this.decodeError.addAll(targetErrorList);\r
- }\r
-\r
- return this;\r
- }\r
-\r
- /**\r
- * 代替文字とともにデコードエラーを追加する。\r
- * ※呼び出し側は、追加されるデコードエラーの位置情報が\r
- * 既存のデコードエラーよりも大きいことを保証しなければならない。\r
- * @param errorInfo デコードエラー\r
- */\r
- private void addDecodeError(DecodeErrorInfo errorInfo){\r
- if(this.decodeError == null){\r
- this.decodeError = createErrorList();\r
- }\r
- this.decodeError.add(errorInfo);\r
- this.rawContent.append(ALTCHAR);\r
- return;\r
- }\r
-\r
- /**\r
- * 代替文字とともにデコードエラーを追加する。\r
- * @param b1st 1バイト目の値\r
- */\r
- public void addDecodeError(byte b1st){\r
- DecodeErrorInfo errInfo =\r
- new DecodeErrorInfo(this.rawContent.length(), b1st);\r
- addDecodeError(errInfo);\r
- return;\r
- }\r
-\r
- /**\r
- * 代替文字とともにデコードエラーを追加する。\r
- * @param b1st 1バイト目の値\r
- * @param b2nd 2バイト目の値\r
- */\r
- public void addDecodeError(byte b1st, byte b2nd){\r
- DecodeErrorInfo errInfo =\r
- new DecodeErrorInfo(this.rawContent.length(), b1st, b2nd);\r
- addDecodeError(errInfo);\r
- return;\r
- }\r
-\r
- /**\r
- * {@inheritDoc}\r
- * @return {@inheritDoc}\r
- */\r
- @Override\r
- public String toString(){\r
- return this.rawContent.toString();\r
- }\r
-\r
- // TODO Windows-31Jへの再デコード処理など\r
-}\r
+/*
+ * decoded source
+ *
+ * License : The MIT License
+ * Copyright(c) 2009 olyutorskii
+ */
+
+package jp.sourceforge.jindolf.parser;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.RandomAccess;
+
+/**
+ * ShiftJISデコードエラー情報を含む再利用可能な文字列。
+ * デコードエラーを起こした箇所は代替文字{@link #ALTCHAR}で置き換えられる。
+ * マルチスレッドには非対応。
+ * UCS-4コードポイントには未対応。
+ */
+public class DecodedContent
+ implements CharSequence,
+ Appendable {
+
+ /**
+ * 代替文字。
+ * {@literal HTMLで使うなら < や > や & や " や ' はやめて!}
+ */
+ public static final char ALTCHAR = '?';
+
+ private static final String NULLTEXT = "null";
+
+ private static final List<DecodeErrorInfo> EMPTY_LIST =
+ Collections.emptyList();
+
+ private static final int BSEARCH_THRESHOLD = 16;
+
+ static{
+ assert ALTCHAR != '<';
+ assert ALTCHAR != '>';
+ assert ALTCHAR != '&';
+ assert ALTCHAR != '"';
+ assert ALTCHAR != '\'';
+ assert ALTCHAR != '\\';
+ }
+
+
+ private final StringBuilder rawContent = new StringBuilder();
+
+ private List<DecodeErrorInfo> decodeError;
+
+
+ /**
+ * コンストラクタ。
+ */
+ public DecodedContent(){
+ this("");
+ return;
+ }
+
+ /**
+ * コンストラクタ。
+ * @param seq 初期化文字列
+ * @throws NullPointerException 引数がnull
+ */
+ public DecodedContent(CharSequence seq) throws NullPointerException{
+ super();
+ if(seq == null) throw new NullPointerException();
+ initImpl();
+ this.rawContent.append(seq);
+ return;
+ }
+
+ /**
+ * コンストラクタ。
+ * @param capacity 文字数の初期容量
+ * @throws NegativeArraySizeException 容量が負の値
+ */
+ public DecodedContent(int capacity) throws NegativeArraySizeException{
+ super();
+ if(capacity < 0) throw new NegativeArraySizeException();
+ initImpl();
+ this.rawContent.ensureCapacity(capacity);
+ return;
+ }
+
+
+ /**
+ * 与えられた文字位置を含むか、またはそれ以降で最も小さな位置情報を持つ
+ * デコードエラーのインデックス位置を返す。※リニアサーチ版。
+ * @param errList デコードエラーのリスト
+ * @param startPos 文字位置
+ * @return 0から始まるリスト内の位置。
+ * 一致する文字位置がなければ挿入ポイント。
+ */
+ protected static int lsearchErrorIndex(List<DecodeErrorInfo> errList,
+ int startPos){
+ // assert errList instanceof RandomAccess;
+
+ int errSize = errList.size();
+
+ int idx;
+ for(idx = 0; idx < errSize; idx++){
+ DecodeErrorInfo einfo = errList.get(idx);
+ int errPos = einfo.getCharPosition();
+ if(startPos <= errPos) break;
+ }
+
+ return idx;
+ }
+
+ /**
+ * 与えられた文字位置を含むか、またはそれ以降で最も小さな位置情報を持つ
+ * デコードエラーのインデックス位置を返す。※バイナリサーチ版。
+ * @param errList デコードエラーのリスト
+ * @param startPos 文字位置
+ * @return 0から始まるリスト内の位置。
+ * 一致する文字位置がなければ挿入ポイント。
+ */
+ protected static int bsearchErrorIndex(List<DecodeErrorInfo> errList,
+ int startPos){
+ // assert errList instanceof RandomAccess;
+
+ int floor = 0;
+ int roof = errList.size() - 1;
+
+ while(floor <= roof){
+ int midpoint = (floor + roof) / 2; // 切り捨て
+ DecodeErrorInfo einfo = errList.get(midpoint);
+ int cmp = einfo.getCharPosition() - startPos;
+
+ if(cmp == 0) return midpoint;
+
+ if (cmp < 0) floor = midpoint + 1;
+ else if(cmp > 0) roof = midpoint - 1;
+ }
+
+ return floor;
+ }
+
+ /**
+ * 与えられた文字位置を含むか、またはそれ以降で最も小さな位置情報を持つ
+ * デコードエラーのインデックス位置を返す。
+ * 要素数の増減に応じてリニアサーチとバイナリサーチを使い分ける。
+ * @param errList デコードエラーのリスト
+ * @param startPos 文字位置
+ * @return 0から始まるリスト内の位置。
+ * 一致する文字位置がなければ挿入ポイント。
+ */
+ protected static int searchErrorIndex(List<DecodeErrorInfo> errList,
+ int startPos){
+ int result;
+
+ int errSize = errList.size();
+ if(errSize < BSEARCH_THRESHOLD){
+ // linear-search
+ result = lsearchErrorIndex(errList, startPos);
+ }else{
+ // binary-search
+ result = bsearchErrorIndex(errList, startPos);
+ }
+
+ return result;
+ }
+
+ /**
+ * ギャップ情報が加味されたデコードエラー情報を、
+ * 範囲指定込みで指定エラーリストに追加転記する。
+ * 追加先エラーリストがnullだった場合、必要に応じてエラーリストが生成され
+ * 戻り値となる場合がありうる。
+ * @param sourceContent 元の文字列
+ * @param startPos 範囲開始位置
+ * @param endPos 範囲終了位置
+ * @param targetError 追加先エラーリスト。nullでもよい。
+ * @param gap ギャップ量
+ * @return 引数targetErrorもしくは新規生成されたリストを返す。
+ */
+ protected static List<DecodeErrorInfo>
+ appendGappedErrorInfo(DecodedContent sourceContent,
+ int startPos, int endPos,
+ List<DecodeErrorInfo> targetError,
+ int gap){
+ List<DecodeErrorInfo> sourceError = sourceContent.decodeError;
+ List<DecodeErrorInfo> result = targetError;
+
+ int startErrorIdx = searchErrorIndex(sourceError, startPos);
+ int endErrorIdx = sourceError.size() - 1;
+ assert endErrorIdx >= 0;
+
+ for(int index = startErrorIdx; index <= endErrorIdx; index++){
+ DecodeErrorInfo einfo = sourceError.get(index);
+ int pos = einfo.getCharPosition();
+ if(pos < startPos) continue;
+ if(pos >= endPos) break;
+ DecodeErrorInfo newInfo = einfo.createGappedClone(gap);
+ if(result == null){
+ result = createErrorList();
+ }
+ result.add(newInfo);
+ }
+
+ return result;
+ }
+
+ /**
+ * エラー格納用リストを生成する。
+ * @return リスト
+ */
+ private static List<DecodeErrorInfo> createErrorList(){
+ List<DecodeErrorInfo> result = new ArrayList<>();
+ return result;
+ }
+
+ static{
+ assert createErrorList() instanceof RandomAccess;
+ }
+
+ /**
+ * 初期化下請け。
+ * 長さ0の文字列&デコードエラー無しの状態になる。
+ */
+ private void initImpl(){
+ this.rawContent.setLength(0);
+
+ if(this.decodeError != null){
+ this.decodeError.clear();
+ }
+
+ return;
+ }
+
+ /**
+ * 事前にキャパシティを確保する。
+ * 指定されたキャパシティの範囲内で再割り当てが起きないことを保証する。
+ * @param minimumCapacity キャラクタ単位のキャパシティ長。
+ */
+ public void ensureCapacity(int minimumCapacity){
+ this.rawContent.ensureCapacity(minimumCapacity);
+ return;
+ }
+
+ /**
+ * 初期化。
+ * 長さ0の文字列&デコードエラー無しの状態になる。
+ * コンストラクタで新インスタンスを作るより低コスト。
+ */
+ public void init(){
+ initImpl();
+ return;
+ }
+
+ /**
+ * デコードエラーを含むか判定する。
+ * @return デコードエラーを含むならtrue
+ */
+ public boolean hasDecodeError(){
+ if(this.decodeError == null) return false;
+ if(this.decodeError.isEmpty()) return false;
+ return true;
+ }
+
+ /**
+ * デコードエラーの一覧を取得する。
+ * @return デコードエラーの一覧
+ */
+ public List<DecodeErrorInfo> getDecodeErrorList(){
+ if( ! hasDecodeError() ){
+ return EMPTY_LIST;
+ }
+ return Collections.unmodifiableList(this.decodeError);
+ }
+
+ /**
+ * 生の文字列を得る。
+ * 高速なCharSequenceアクセス用途。
+ * @return 生の文字列。
+ */
+ public CharSequence getRawContent(){
+ return this.rawContent;
+ }
+
+ /**
+ * 指定された位置の文字を変更する。
+ * @param index 文字位置
+ * @param ch 文字
+ * @throws IndexOutOfBoundsException 不正な位置指定
+ */
+ public void setCharAt(int index, char ch)
+ throws IndexOutOfBoundsException{
+ this.rawContent.setCharAt(index, ch);
+ return;
+ }
+
+ /**
+ * {@inheritDoc}
+ * @param index {@inheritDoc}
+ * @return {@inheritDoc}
+ */
+ @Override
+ public char charAt(int index){
+ return this.rawContent.charAt(index);
+ }
+
+ /**
+ * {@inheritDoc}
+ * @return {@inheritDoc}
+ */
+ @Override
+ public int length(){
+ return this.rawContent.length();
+ }
+
+ /**
+ * {@inheritDoc}
+ * @param start {@inheritDoc}
+ * @param end {@inheritDoc}
+ * @return {@inheritDoc}
+ */
+ @Override
+ public CharSequence subSequence(int start, int end){
+ return this.rawContent.subSequence(start, end);
+ }
+
+ /**
+ * 範囲指定されたサブコンテントを切り出す。
+ * サブコンテントにはデコードエラー情報が引き継がれる。
+ * @param start 開始位置
+ * @param end 終了位置
+ * @return サブコンテント
+ * @throws IndexOutOfBoundsException start または end が負の値の場合、
+ * end が length() より大きい場合、
+ * あるいは start が end より大きい場合
+ */
+ public DecodedContent subContent(int start, int end)
+ throws IndexOutOfBoundsException{
+ int length = end - start;
+ if(length < 0) throw new IndexOutOfBoundsException();
+ DecodedContent result = new DecodedContent(length);
+ result.append(this, start, end);
+ return result;
+ }
+
+ /**
+ * 文字を追加する。
+ * @param letter 追加する文字
+ * @return thisオブジェクト
+ */
+ @Override
+ public DecodedContent append(char letter){
+ this.rawContent.append(letter);
+ return this;
+ }
+
+ /**
+ * 文字列を追加する。
+ * @param seq 追加する文字列
+ * @return thisオブジェクト
+ */
+ @Override
+ public DecodedContent append(CharSequence seq){
+ if(seq == null){
+ this.rawContent.append(NULLTEXT);
+ }else if(seq instanceof DecodedContent){
+ append((DecodedContent) seq, 0, seq.length());
+ }else{
+ this.rawContent.append(seq);
+ }
+ return this;
+ }
+
+ /**
+ * 文字列を追加する。
+ * @param seq 追加する文字列
+ * @param startPos 開始位置
+ * @param endPos 終了位置
+ * @return thisオブジェクト
+ * @throws IndexOutOfBoundsException 範囲指定が変。
+ */
+ @Override
+ public DecodedContent append(CharSequence seq,
+ int startPos, int endPos)
+ throws IndexOutOfBoundsException{
+ if(seq == null){
+ this.rawContent.append(NULLTEXT, startPos, endPos);
+ }else if(seq instanceof DecodedContent){
+ append((DecodedContent) seq, startPos, endPos);
+ }else{
+ this.rawContent.append(seq, startPos, endPos);
+ }
+
+ return this;
+ }
+
+ /**
+ * 文字列を追加する。
+ * @param source 追加する文字列
+ * @param startPos 開始位置
+ * @param endPos 終了位置
+ * @return thisオブジェクト
+ * @throws IndexOutOfBoundsException 範囲指定が変。
+ */
+ public DecodedContent append(DecodedContent source,
+ int startPos, int endPos)
+ throws IndexOutOfBoundsException{
+ if(source == null){
+ return append(NULLTEXT, startPos, endPos);
+ }
+
+ int gap = startPos - this.rawContent.length();
+
+ this.rawContent.append(source.rawContent, startPos, endPos);
+
+ if( ! source.hasDecodeError() ) return this;
+
+ List<DecodeErrorInfo> targetErrorList;
+ if(source != this) targetErrorList = this.decodeError;
+ else targetErrorList = null;
+
+ targetErrorList = appendGappedErrorInfo(source,
+ startPos, endPos,
+ targetErrorList,
+ gap);
+
+ if(targetErrorList == null) return this;
+ if(targetErrorList == this.decodeError) return this;
+
+ if(this.decodeError == null){
+ this.decodeError = targetErrorList;
+ }else{
+ this.decodeError.addAll(targetErrorList);
+ }
+
+ return this;
+ }
+
+ /**
+ * 代替文字とともにデコードエラーを追加する。
+ * ※呼び出し側は、追加されるデコードエラーの位置情報が
+ * 既存のデコードエラーよりも大きいことを保証しなければならない。
+ * @param errorInfo デコードエラー
+ */
+ private void addDecodeError(DecodeErrorInfo errorInfo){
+ if(this.decodeError == null){
+ this.decodeError = createErrorList();
+ }
+ this.decodeError.add(errorInfo);
+ this.rawContent.append(ALTCHAR);
+ return;
+ }
+
+ /**
+ * 代替文字とともにデコードエラーを追加する。
+ * @param b1st 1バイト目の値
+ */
+ public void addDecodeError(byte b1st){
+ DecodeErrorInfo errInfo =
+ new DecodeErrorInfo(this.rawContent.length(), b1st);
+ addDecodeError(errInfo);
+ return;
+ }
+
+ /**
+ * 代替文字とともにデコードエラーを追加する。
+ * @param b1st 1バイト目の値
+ * @param b2nd 2バイト目の値
+ */
+ public void addDecodeError(byte b1st, byte b2nd){
+ DecodeErrorInfo errInfo =
+ new DecodeErrorInfo(this.rawContent.length(), b1st, b2nd);
+ addDecodeError(errInfo);
+ return;
+ }
+
+ /**
+ * {@inheritDoc}
+ * @return {@inheritDoc}
+ */
+ @Override
+ public String toString(){
+ return this.rawContent.toString();
+ }
+
+}