我正在将所有字符读入流中。我正在使用 inputStream.read 读取它。这是 java.io.Reader 输入流。
读入缓冲区时如何忽略@等特殊字符。
code
private final void FillBuff() throws java.io.IOException
{
int i;
if (maxNextCharInd == 4096)
maxNextCharInd = nextCharInd = 0;
try {
if ((i = inputStream.read(nextCharBuf, maxNextCharInd,
4096 - maxNextCharInd)) == -1)
{
inputStream.close();
throw new java.io.IOException();
}
else
maxNextCharInd += i;
return;
}
catch(java.io.IOException e) {
if (bufpos != 0)
{
--bufpos;
backup(0);
}
else
{
bufline[bufpos] = line;
bufcolumn[bufpos] = column;
}
throw e;
}
}
您可以使用自定义FilterReader
.
class YourFilterReader extends FilterReader{
@Override
public int read() throws IOException{
int read;
do{
read = super.read();
} while(read == '@');
return read;
}
@Override
public int read(char[] cbuf, int off, int len) throws IOException{
int read = super.read(cbuf, off, len);
if (read == -1) {
return -1;
}
int pos = off - 1;
for (int readPos = off; readPos < off + read; readPos++) {
if (read == '@') {
continue;
} else {
pos++;
}
if (pos < readPos) {
cbuf[pos] = cbuf[readPos];
}
}
return pos - off + 1;
}
}
资源 :
- Javadoc - FilterReader
- BCRDF - 使用 ReaderFilter 跳过无效的 XML 字符
关于同一主题:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)