⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filterinputstreamdemo.java

📁 这个都是一些Java的IO相关的一些源码
💻 JAVA
字号:
import java.io.*;

// a filter input stream that skips every other byte in the stream

class SkipFilterInputStream extends FilterInputStream {
    public SkipFilterInputStream (InputStream in) {
        super(in);
    }
    public synchronized int read() throws IOException {
        int c = in.read();
        in.skip(1);
        return (c);
    }
    public synchronized int read(byte[] b, int off, int len) 
        throws IOException {
        byte[] tmp = new byte[len+len];

        int howmany = in.read(tmp);
        int real_count = off;
        for (int i = 0; i < howmany; i += 2) {
            b[real_count++] = tmp[i];
        }
        return (real_count);
    }
    public synchronized int read(byte[] b) throws IOException {
        return (this.read(b, 0, b.length));
    }
    public synchronized long skip(long n) throws IOException {
        byte[] b = new byte[(int)n];
        return (this.read(b));
    }
    public synchronized int available() throws IOException {
        return (in.available() / 2);
    }
    public boolean markSupported() {
        return in.markSupported();
    }
    public synchronized void mark(int readlimit) {
        in.mark(readlimit + readlimit);
    }
    public synchronized void reset() throws IOException {
        in.reset();
    }
    public synchronized void close() throws IOException {
        in.close();
    }
}

public class FilterInputStreamDemo {
    public static void main(String[] args) {
        if (args.length != 1) {
            System.err.println("Usage: java Main <input_file>");
            System.exit(-1);
        }
        try {
            FileInputStream f1 = new FileInputStream(args[0]);
            SkipFilterInputStream s1 = new SkipFilterInputStream(f1);

            // read of single char
            for(int c = s1.read(); c > -1; c = s1.read()) {
                System.out.print((char)c);
            }
            s1.close();

            // read of buffer again, this time check mark/reset/skip
            f1 = new FileInputStream(args[0]);
            s1 = new SkipFilterInputStream(f1);
            byte[] buf = new byte[32];
            int howmany;
            while ((howmany = s1.read(buf)) > 0) {
                for (int i = 0; i < howmany; i++)
                    System.out.print((char)buf[i]);
                if (s1.skip(32) != 32)
                    break;
                if (s1.markSupported()) {
                    s1.mark(50);
                    if (s1.skip(32) != 32)
                        break;
                    s1.reset();
                }
            }
            s1.close();
        } catch (IOException e) {
            System.out.println(e);
        }
    }
}

⌨️ 快捷键说明

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