grepinputstream.java

来自「《Java与模式》一书的源代码」· Java 代码 · 共 35 行

JAVA
35
字号
package com.javapatterns.decorator.greps;

import java.io.DataInputStream;
import java.io.FilterInputStream;
import java.io.IOException;

public class GrepInputStream extends FilterInputStream
{
    //correction
    private String substring;
    private 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 = null;
        do
        {
            line = in.readLine();
        }
        while ((line != null) && line.indexOf(substring) == -1);
        return line;
    }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?