Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove or ignore character from reader

Tags:

java

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;
     }
  }
like image 456
bombac Avatar asked Aug 31 '10 07:08

bombac


People also ask

How do I remove the alphabet from a string in Java?

str = str. replaceAll("[^\\d]", ""); You can try this java code in a function by taking the input value and returning the replaced value as per your requirement. Hope this will help you to achieve your requirement.

How do I remove all special characters from a column in R?

To remove a character in an R data frame column, we can use gsub function which will replace the character with blank. For example, if we have a data frame called df that contains a character column say x which has a character ID in each value then it can be removed by using the command gsub("ID","",as.


1 Answers

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
like image 104
Colin Hebert Avatar answered Oct 09 '22 19:10

Colin Hebert