Class BoundedInputStream

java.lang.Object
java.io.InputStream
java.io.FilterInputStream
cloud.opencode.base.io.stream.BoundedInputStream
All Implemented Interfaces:
Closeable, AutoCloseable

public class BoundedInputStream extends FilterInputStream
Bounded Input Stream 限制大小输入流

Input stream wrapper that limits the number of bytes that can be read. Useful for preventing reading excessively large data.

限制可读取字节数的输入流包装器。 用于防止读取过大的数据。

Features | 主要功能:

  • Limit maximum read bytes - 限制最大读取字节数
  • Optional exception on exceed - 超出时可选抛出异常
  • Track read progress - 追踪读取进度

Usage Examples | 使用示例:

try (BoundedInputStream bounded = new BoundedInputStream(input, 1024 * 1024)) {
    byte[] data = bounded.readAllBytes(); // Max 1MB
}

Security | 安全性:

  • Thread-safe: No - 线程安全: 否
Since:
JDK 25, opencode-base-io V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • BoundedInputStream

      public BoundedInputStream(InputStream in, long maxSize)
      Creates a bounded input stream 创建限制大小输入流
      Parameters:
      in - the underlying input stream | 底层输入流
      maxSize - the maximum bytes to read | 最大读取字节数
    • BoundedInputStream

      public BoundedInputStream(InputStream in, long maxSize, boolean throwOnExceeding)
      Creates a bounded input stream with exception option 创建带异常选项的限制大小输入流
      Parameters:
      in - the underlying input stream | 底层输入流
      maxSize - the maximum bytes to read | 最大读取字节数
      throwOnExceeding - whether to throw when limit exceeded | 超出限制时是否抛出异常
  • Method Details

    • read

      public int read() throws IOException
      Overrides:
      read in class FilterInputStream
      Throws:
      IOException
    • read

      public int read(byte[] b, int off, int len) throws IOException
      Overrides:
      read in class FilterInputStream
      Throws:
      IOException
    • skip

      public long skip(long n) throws IOException
      Overrides:
      skip in class FilterInputStream
      Throws:
      IOException
    • available

      public int available() throws IOException
      Overrides:
      available in class FilterInputStream
      Throws:
      IOException
    • mark

      public void mark(int readlimit)
      Overrides:
      mark in class FilterInputStream
    • reset

      public void reset() throws IOException
      Overrides:
      reset in class FilterInputStream
      Throws:
      IOException
    • getBytesRead

      public long getBytesRead()
      Gets the number of bytes read 获取已读取字节数
      Returns:
      bytes read | 已读取字节数
    • getRemaining

      public long getRemaining()
      Gets the remaining bytes that can be read 获取剩余可读字节数
      Returns:
      remaining bytes | 剩余字节数
    • getMaxSize

      public long getMaxSize()
      Gets the maximum size limit 获取最大大小限制
      Returns:
      max size | 最大大小
    • isLimitReached

      public boolean isLimitReached()
      Checks if the limit has been reached 检查是否已达到限制
      Returns:
      true if limit reached | 如果达到限制返回true