FileDocCategorySizeDatePackage
GrepInputStream.javaAPI DocExample1107Fri Feb 16 13:29:38 GMT 1996None

GrepInputStream.java

// This example is from the book _Java in a Nutshell_ by David Flanagan.
// Written by David Flanagan.  Copyright (c) 1996 O'Reilly & Associates.
// You may study, use, modify, and distribute this example for any purpose.
// This example is provided WITHOUT WARRANTY either expressed or implied.

import java.io.*;

// This class is a FilterInputStream that filters out all lines that
// do not contain the specified substring.
public class GrepInputStream extends FilterInputStream {
    String substring;
    DataInputStream in;
    
    public GrepInputStream(DataInputStream in, String substring) {
        super(in);
        this.in = in;
        this.substring = substring;
    }
    
    // This is the filter:  read lines from the DataInputStream,
    // but only return the lines that contain the substring.
    // When the DataInputStream returns null, we return null.
    public final String readLine() throws IOException {
        String line;
        do {
            line = in.readLine();
        }
        while ((line != null) && line.indexOf(substring) == -1);
        return line;
    }
}