这个LimitedInputStream正确吗?

发布于 2024-11-10 09:35:33 字数 2263 浏览 4 评论 0原文

我编写了一个名为 LimitedInputStream 的类。它环绕现有的输入流,将从其读取的字节数限制为指定长度。它是作为以下内容的替代方案:

byte[] data = readAll(length);
InputStream ins = new ByteArrayInputStream(data);

需要额外的缓冲区。

这是该类:

public static class LimitedInputStream extends InputStream {
    private final InputStream ins;
    private int left;
    private int mark = -1;

    public LimitedInputStream(InputStream ins, int limit) {
        this.ins = ins;
        left = limit;
    }

    public void skipRest() throws IOException {
        ByteStreams.skipFully(ins, left);
        left = 0;
    }

    @Override
    public int read() throws IOException {
        if (left == 0) return -1;
        final int read = ins.read();
        if (read > 0) left--;
        return read;
    }

    @Override
    public int read(byte[] b, int off, int len) throws IOException {
        if (left == 0) return -1;
        if (len > left) len = left;
        final int read = ins.read(b, off, len);
        if (read > 0) left -= read;
        return read;
    }

    @Override
    public int available() throws IOException {
        final int a = ins.available();
        return a > left ? left : a;
    }

    @Override
    public void mark(int readlimit) {
        ins.mark(readlimit);
        mark = left;
    }

    @Override
    public void reset() throws IOException {
        if (!ins.markSupported()) throw new IOException("Mark not supported");
        if (mark == -1) throw new IOException("Mark not set");

        ins.reset();
        left = mark;
    }

    @Override
    public long skip(long n) throws IOException {
        if (n > left) n = left;
        long skipped = ins.skip(n);
        left -= skipped;
        return skipped;
    }

}

用例:

Object readObj() throws IOException {
    int len = readInt();
    final LimitedInputStream lis = new LimitedInputStream(this, len);
    try {
        return deserialize(new CompactInputStream(lis));
    } finally {
        lis.skipRest();
    }
}

for (something) {
  Object obj;
 try {
   obj = readObj();
 } catch (Exception e) {
   obj = null;
 }
 list.add(obj);
}

您能否对我的类进行代码审查是否存在任何严重错误,例如更新 left 时可能出现的错误?

I've written a class called LimitedInputStream. It wraps around an existing input stream to limit the number of bytes read from it to a specified length. It's meant as an alternative to:

byte[] data = readAll(length);
InputStream ins = new ByteArrayInputStream(data);

Which requires the extra buffer.

This is the class:

public static class LimitedInputStream extends InputStream {
    private final InputStream ins;
    private int left;
    private int mark = -1;

    public LimitedInputStream(InputStream ins, int limit) {
        this.ins = ins;
        left = limit;
    }

    public void skipRest() throws IOException {
        ByteStreams.skipFully(ins, left);
        left = 0;
    }

    @Override
    public int read() throws IOException {
        if (left == 0) return -1;
        final int read = ins.read();
        if (read > 0) left--;
        return read;
    }

    @Override
    public int read(byte[] b, int off, int len) throws IOException {
        if (left == 0) return -1;
        if (len > left) len = left;
        final int read = ins.read(b, off, len);
        if (read > 0) left -= read;
        return read;
    }

    @Override
    public int available() throws IOException {
        final int a = ins.available();
        return a > left ? left : a;
    }

    @Override
    public void mark(int readlimit) {
        ins.mark(readlimit);
        mark = left;
    }

    @Override
    public void reset() throws IOException {
        if (!ins.markSupported()) throw new IOException("Mark not supported");
        if (mark == -1) throw new IOException("Mark not set");

        ins.reset();
        left = mark;
    }

    @Override
    public long skip(long n) throws IOException {
        if (n > left) n = left;
        long skipped = ins.skip(n);
        left -= skipped;
        return skipped;
    }

}

Use case:

Object readObj() throws IOException {
    int len = readInt();
    final LimitedInputStream lis = new LimitedInputStream(this, len);
    try {
        return deserialize(new CompactInputStream(lis));
    } finally {
        lis.skipRest();
    }
}

for (something) {
  Object obj;
 try {
   obj = readObj();
 } catch (Exception e) {
   obj = null;
 }
 list.add(obj);
}

Could you code review my class for any serious bugs, e.g. possible mistakes in updating left?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千里故人稀 2024-11-17 09:35:33

Guava 包含一个 LimitInputStream,所以你可能只想使用它。

Guava includes a LimitInputStream, so you may want to just use that.

猫性小仙女 2024-11-17 09:35:33

Apache Commons IO 有一个 BoundedInputStream 可以使用自己的实现。

Apache Commons IO has a BoundedInputStream which can be used instead of a own implementation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文