Class BoundedInputStream
java.lang.Object
java.io.InputStream
java.io.FilterInputStream
cloud.opencode.base.io.stream.BoundedInputStream
- All Implemented Interfaces:
Closeable, AutoCloseable
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:
-
Field Summary
Fields inherited from class FilterInputStream
in -
Constructor Summary
ConstructorsConstructorDescriptionBoundedInputStream(InputStream in, long maxSize) Creates a bounded input stream 创建限制大小输入流BoundedInputStream(InputStream in, long maxSize, boolean throwOnExceeding) Creates a bounded input stream with exception option 创建带异常选项的限制大小输入流 -
Method Summary
Modifier and TypeMethodDescriptionintlongGets the number of bytes read 获取已读取字节数longGets the maximum size limit 获取最大大小限制longGets the remaining bytes that can be read 获取剩余可读字节数booleanChecks if the limit has been reached 检查是否已达到限制voidmark(int readlimit) intread()intread(byte[] b, int off, int len) voidreset()longskip(long n) Methods inherited from class FilterInputStream
close, markSupported, readMethods inherited from class InputStream
nullInputStream, readAllBytes, readNBytes, readNBytes, skipNBytes, transferTo
-
Constructor Details
-
BoundedInputStream
Creates a bounded input stream 创建限制大小输入流- Parameters:
in- the underlying input stream | 底层输入流maxSize- the maximum bytes to read | 最大读取字节数
-
BoundedInputStream
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
- Overrides:
readin classFilterInputStream- Throws:
IOException
-
read
- Overrides:
readin classFilterInputStream- Throws:
IOException
-
skip
- Overrides:
skipin classFilterInputStream- Throws:
IOException
-
available
- Overrides:
availablein classFilterInputStream- Throws:
IOException
-
mark
public void mark(int readlimit) - Overrides:
markin classFilterInputStream
-
reset
- Overrides:
resetin classFilterInputStream- 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
-