且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

"购"来自Socket的DataInputStream

更新时间:2022-12-08 10:52:22

放一个 PushbackInputStream 允许你读取一些字节而不用破坏数据。

Putting a PushbackInputStream in between allows you to read some bytes without corrupting the data.

编辑:下面未经测试的代码示例。这是来自内存。

Untested code example below. This is from memory.

static class MyWrapper extends PushbackInputStream {
    MyWrapper(InputStream in) {
        super(in);
    }

    @Override
    public int available() throws IOException {
        int b = super.read();
        // do something specific?
        super.unread(b);
        return super.available();
    }
}

public static void main(String... args) {
    InputStream originalSocketStream = null;
    DataInputStream dis = new DataInputStream(new MyWrapper(originalSocketStream));
}