i am reading all characters into stream. I am reading it with inputStream.read. This is java.io.Reader inputStream. How can i ignore special characters like @ when reading into buffer.
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;
}
}
You can use a custom 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;
}
}
Resources :
- Javadoc - FilterReader
- BCRDF - Skipping Invalid XML Character with ReaderFilter
On the same topic :
- filter/remove invalid xml characters from stream
All those readers, writers and streams implement the Decorator pattern. Each decorator adds additional behaviour and functionality to the underlying implementation.
A solution for you requirement could be a FilterReader:
public class FilterReader implements Readable, Closeable {
private Set<Character> blacklist = new HashSet<Character>();
private Reader reader;
public FilterReader(Reader reader) {
this.reader = reader;
}
public void addFilter(char filtered) {
blacklist.add(filtered);
}
@Override
public void close() throws IOException {reader.close();}
@Override
public int read(char[] charBuf) {
char[] temp = new char[charBuf.length];
int charsRead = reader.read(temp);
int index = -1;
if (!(charsRead == -1)) {
for (char c:temp) {
if (!blacklist.contains(c)) {
charBuf[index] = c;
index++;
}
}
}
return index;
}
}
Note - the class java.io.FilterReader
is a decorator with zero functionality. You can extend it or just ignore it and create your own decorator (which I prefer in this case).
You could implement an own inputstream derived from InputStream. Then override the read methods so that they filter a special character out of the stream.
private final void FillBuff() throws java.io.IOException
{
int i;
if (maxNextCharInd == 4096)
maxNextCharInd = nextCharInd = 0;
try {
Reader filterReader = new FilterReader(inputStream) {
public int read() {
do {
result = super.read();
} while (specialCharacter(result));
return result;
}
};
if ((i = filterReader.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;
}
}
精彩评论