?? replacereader.java
字號:
/**
* IgaLib -> String
* Copyright (C) 2001 Tosiki IGA , IgaLib project member
* (http://homepage2.nifty.com/igat/igapyon/index.html)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package jp.ne.nifty.iga.tosiki.string;
import java.io.*;
/**
* 敿妏僇僞僇僫傪慡妏曄姺傪峴偆Reader僋儔僗偱偡丅<BR>
* 偙偺僋儔僗傪棙梡偡傞偲 敿妏僇僞僇僫偺暥帤楍傪 慡妏偵曄姺偟側偑傜
* 張棟傪恑傔偰偄偒傑偡丅戺壒傕張棟懳徾偲側偭偰偍傝傑偡丅
* 敿妏戺壒偼揔愗側慡妏戺壒傊偲曄姺偝傟傑偡丅側偍 敿妏ANK側偳偼張棟懳徾奜偵側傝傑偡丅
*
* @author Tosiki IGA
*/
public class ReplaceReader
extends Reader
{
protected Reader reader=null;
protected String strFind=null;
protected String strDest=null;
protected int iSearchWordLength=-1;
protected boolean isEndOfStream=false;
/**
* 撉傒崬傒僶僢僼傽
*/
protected char[] aryBufOfReader=null;
protected int iAryBufOfReaderPoint=0;
protected int iAryBufOfReaderLength=0;
/**
* 専嶕暥帤楍偑堦抳偡傞傑偱曐棷偟偰偍偔儚乕僋Writer
*/
protected CharArrayWriter writerWork=new CharArrayWriter();
/**
* 抲姺Reader僋儔僗偺僐儞僗僩儔僋僞
*
* @param Reader reader 楢寢偡傞尦偺Reader
* @param String strFind 専嶕偡傞暥帤楍
* @param String strDest 専嶕僸僢僩偟偨応崌偵抲姺偡傞暥帤楍
*/
public ReplaceReader(Reader reader,String strFind,String strDest)
{
this.reader=reader;
this.strFind=strFind;
this.strDest=strDest;
iSearchWordLength=strFind.length();
}
/**
* 1暥帤Reader偐傜撉傒崬傒傑偡丅
*
* @return int 撉傒崬傫偩暥帤楍
* @throws IOException 壗偐偟傜偺I/O椺奜
*/
public int read()
throws IOException
{
if(iAryBufOfReaderPoint<iAryBufOfReaderLength)
{
return aryBufOfReader[iAryBufOfReaderPoint++];
}
if(isEndOfStream)
{
return -1;
}
// 専嶕曐棷暥帤楍偺弶婜壔
writerWork.reset();
for(int iSearchPoint=0;;iSearchPoint++)
{
int iRead=reader.read();
if(iRead<0)
{
isEndOfStream=true;
break;
}
// 専嶕曐棷暥帤楍偵捛壛
writerWork.write(iRead);
if(iRead!=strFind.charAt(iSearchPoint))
{
break;
}
// 擖椡暥帤楍偲僸僢僩拞
if((iSearchPoint+1)==iSearchWordLength)
{
// 抲姺偑姰慡僸僢僩
// 専嶕曐棷暥帤楍偺撪梕偼攋婞偟偰丄抲姺屻暥帤楍偵抲姺偟傑偡丅
writeBufToBuffer(strDest.toCharArray());
return read();
}
}
// 抲姺偼僸僢僩偟側偐偭偨偺偱丄専嶕曐棷暥帤楍傪COMMIT丅
writeBufToBuffer(writerWork.toCharArray());
return read();
}
protected void writeBufToBuffer(char[] arg)
throws IOException
{
aryBufOfReader=arg;
iAryBufOfReaderPoint=0;
iAryBufOfReaderLength=aryBufOfReader.length;
}
/**
* 暥帤攝楍傪Reader偐傜撉傒崬傒傑偡丅<BR>
* 2001.08.14 慡偔僶僌偭偰偄偨偺偱廋惓偟傑偟偨丅JUnit梡僐乕僪傪婰嵹偟傑偟偨丅<BR>
*
* @param char cbuf[] 撉傒崬傒愭僶僢僼傽
* @param int off 撉傒崬傒愭僶僢僼傽偺僆僼僙僢僩
* @param int len 撉傒崬傒壜擻側嵟戝挿偝
* @return int 撉傒崬傫偩暥帤楍偺挿偝
* @throws IOException 壗偐偟傜偺I/O椺奜
*/
public final int read(char cbuf[], int off, int len)
throws IOException
{
if(isEndOfStream)
{
return -1;
}
int iNumberOfCharacters=0;
int iTill=off+len;
for(int index=off;index<iTill;index++)
{
int iRead=read();
if(iRead<0)
{
isEndOfStream=true;
break;
}
cbuf[index]=(char)iRead;
iNumberOfCharacters++;
}
return iNumberOfCharacters;
}
/**
* Reader傪僋儘乕僘偟傑偡
*
* @throws IOException 壗偐偟傜偺I/O椺奜偺敪惗
*/
public final void close()
throws IOException
{
if(reader!=null)
{
reader.close();
reader=null;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -