Class SafeImageService
java.lang.Object
cloud.opencode.base.image.security.SafeImageService
- All Implemented Interfaces:
AutoCloseable
Safe Image Service
安全图片服务
Thread-safe Raster-based image processing service with security
controls (size validation, timeout, concurrency limits).
带安全控制(大小验证、超时、并发限制)的线程安全 Raster 图像处理服务。
Features | 特性:
- Size validation | 大小验证
- Timeout control | 超时控制
- Concurrency limits | 并发限制
Usage Examples | 使用示例:
SafeImageService service = SafeImageService.builder()
.maxFileSize(5_000_000)
.maxWidth(4000)
.maxHeight(4000)
.timeout(Duration.ofSeconds(30))
.maxConcurrent(10)
.build();
Raster raster = service.read(path);
Raster resized = service.process(raster, r -> ResizeOp.resize(r, 800, 600));
- Since:
- JDK 25, opencode-base-image V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classSafe Image Service Builder 安全图片服务构建器 -
Constructor Summary
ConstructorsConstructorDescriptionSafeImageService(long maxFileSize, int maxWidth, int maxHeight, Duration timeout, int maxConcurrent) Create safe image service 创建安全图片服务 -
Method Summary
Modifier and TypeMethodDescriptionstatic SafeImageService.Builderbuilder()Create builder 创建构建器voidclose()static SafeImageServiceCreate with default settings 使用默认设置创建intGet active operation count 获取活动操作数intGet available permits 获取可用许可数Get image info safely 安全获取图片信息process(Raster raster, ImageOperation operation) Process raster with operation under timeout / concurrency controls.read(byte[] bytes) Read raster safely from bytes 从字节数组安全读取 rasterRead raster safely from path 从路径安全读取 rastervoidsave(Raster raster, Path path, ImageFormat format, Path baseDir) Save raster safely with explicit format 以指定格式安全保存 rastervoidSave raster safely 安全保存 raster
-
Constructor Details
-
SafeImageService
public SafeImageService(long maxFileSize, int maxWidth, int maxHeight, Duration timeout, int maxConcurrent) Create safe image service 创建安全图片服务- Parameters:
maxFileSize- maximum file size in bytes | 最大文件大小(字节)maxWidth- maximum width | 最大宽度maxHeight- maximum height | 最大高度timeout- operation timeout | 操作超时时间maxConcurrent- maximum concurrent operations | 最大并发操作数
-
-
Method Details
-
builder
-
createDefault
Create with default settings 使用默认设置创建- Returns:
- the service | 服务
-
read
Read raster safely from path 从路径安全读取 raster- Parameters:
path- the file path | 文件路径- Returns:
- the raster | raster
- Throws:
ImageException- if reading or validation fails | 如果读取或验证失败
-
read
Read raster safely from bytes 从字节数组安全读取 raster- Parameters:
bytes- the image bytes | 图片字节数组- Returns:
- the raster | raster
- Throws:
ImageException- if reading or validation fails | 如果读取或验证失败
-
process
Process raster with operation under timeout / concurrency controls. 在超时与并发控制下处理 raster。- Parameters:
raster- the input raster | 输入 rasteroperation- the operation | 操作- Returns:
- the processed raster | 处理后的 raster
- Throws:
ImageException- if processing fails | 如果处理失败- Since:
- opencode-base-image V1.0.4
-
save
Save raster safely 安全保存 raster- Parameters:
raster- the raster | rasterpath- the output path | 输出路径baseDir- the base directory for validation | 用于验证的基础目录- Throws:
ImageException- if saving fails | 如果保存失败
-
save
Save raster safely with explicit format 以指定格式安全保存 raster- Parameters:
raster- the raster | rasterpath- the output path | 输出路径format- the image format | 图片格式baseDir- the base directory for validation | 用于验证的基础目录- Throws:
ImageException- if saving fails | 如果保存失败
-
getInfo
Get image info safely 安全获取图片信息- Parameters:
path- the file path | 文件路径- Returns:
- the image info | 图片信息
- Throws:
ImageException- if reading fails | 如果读取失败
-
getActiveCount
public int getActiveCount()Get active operation count 获取活动操作数- Returns:
- the count | 数量
-
getAvailablePermits
public int getAvailablePermits()Get available permits 获取可用许可数- Returns:
- the available permits | 可用许可数
-
close
public void close()- Specified by:
closein interfaceAutoCloseable
-