2010-08-31 13 views
5

Sto leggendo tutti i caratteri nello stream. Lo sto leggendo con inputStream.read. Questo è java.io.Reader inputStream. Come posso ignorare caratteri speciali come @ durante la lettura nel buffer.Rimuovi o ignora carattere dal lettore

codice

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; 
    } 
    } 

risposta

0

Si potrebbe implementare un proprio InputStream derivato da InputStream. Quindi eseguire l'override dei metodi di lettura in modo da filtrare un carattere speciale dallo stream.

+0

InputStreams legge byte, non caratteri. Intendi Reader. –

+0

oh. scusa. naturalmente - intendo la classe Reader. –

0
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; 
} 
} 
+0

ciao, che tipo è risultato? – bombac

+0

e che cos'è il metodo specialeCarattere? – bombac

7

È possibile utilizzare un numero personalizzato 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; 
    } 
} 

Risorse:

Sullo stesso tema:

+0

Sì, un nuovo decoratore è la strada da percorrere. – atamanroman

+0

Solo una nota. Nel secondo metodo mancano gli argomenti. ;) –

+0

@ codeing.mof, grazie, è corretto. –

4

tutti quei lettori, scrittori e ruscelli implementare il Decorator modello. Ogni decoratore aggiunge ulteriore comportamento e funzionalità all'implementazione sottostante.

Una soluzione per voi requisito potrebbe essere un 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; 
    } 

} 

Nota - la classe java.io.FilterReader è un decoratore con funzionalità pari a zero. Puoi estenderlo o semplicemente ignorarlo e creare il tuo decoratore (che preferisco in questo caso).

+0

Con il metodo 'read (char [])', penso che dovresti continuare a leggere finché il 'charBuf' non è pieno. La tua implementazione legge solo gli elementi validi nei prossimi elementi 'charbuf.length'. Bella implementazione generale con il 'Set' - forse includerei un altro costruttore con un parametro' Set' - ma non è così importante qui. –

+0

Estendere 'java.io.FilterReader', potrebbe essere utile mantenere la classe come Reader e dire esplicitamente che si tratta di un lettore creato per filtrare un flusso di caratteri di input. E il polimorfismo sarebbe ancora disponibile. E nel tuo caso, non puoi incapsulare la tua lezione in un altro Reader, perché non è un altro Reader. –

+0

Yikes - 'Reader' non è un'interfaccia ma una classe astratta. Modificata l'implementazione ... –