iflytek/astron-agent · warning · IllegalArgumentException
Compression ratio must be between 0-1
Error message
Compression ratio must be between 0-1
What it means
compressImage validates that the compression ratio (scale) lies strictly between 0 and 1 and throws IllegalArgumentException otherwise. This is a caller-side argument contract, not an image-processing failure.
Solutions
- Convert the caller's value to a fraction: divide a percentage by 100 (e.g. 50 -> 0.5f)
- Use a value in (0, 1]; e.g. 0.5f for half-size. Use 1.0 carefully — the check rejects values > 1, and scale must be > 0
- Clamp or validate user-supplied scale from config/API before invoking
Example fix
// before float scale = 50; // percent InputStream out = ImageUtil.compressImage(in, scale); // throws // after float scale = 50 / 100f; // 0.5f InputStream out = ImageUtil.compressImage(in, scale);
Defensive patterns
Strategy: validation
Validate before calling
if (scale <= 0f || scale > 1f) { throw new BadRequestException("scale must be in (0, 1]"); } Prevention
- Normalize percent inputs (divide by 100) before calling
- Clamp config-driven scale values into (0, 1] at load time
When it happens
Trigger: Calling ImageUtil.compressImage(stream, 0), a negative value like -0.2, or 1.0 / greater values (e.g. 1.5 intending 'enlarge').
Common situations: Confusing scale-as-percentage (50 for 50%) with scale-as-fraction (0.5), passing quality (0-100) instead of scale, or a config default of 0 meaning 'no compression'.
Related errors
- Base64 string cannot be empty
- Input stream cannot be empty
- User UID cannot be null
- DUPLICATE_BOT_NAME
- PARAMETER_ERROR
AI-assisted analysis of iflytek/astron-agent@5e758547a8 (2026-09-12).
Data as JSON: /api/errors/e6aa3c03f075143d.
Report an issue: GitHub.
Appendix: source
Thrown at console/backend/hub/src/main/java/com/iflytek/astron/console/hub/util/ImageUtil.java:54
} catch (Exception e) {
log.error("Failed to convert Base64 string to InputStream", e);
throw new BusinessException(SYSTEM_ERROR);
}
}
/**
* Compress image
*
* @param inputStream Original image input stream
* @param scale Compression ratio (0.0-1.0)
* @return Compressed image input stream
*/
public static InputStream compressImage(InputStream inputStream, float scale) {
if (inputStream == null) {
throw new IllegalArgumentException("Input stream cannot be empty");
}
if (scale <= 0 || scale > 1) {
throw new IllegalArgumentException("Compression ratio must be between 0-1");
}
ByteArrayOutputStream outputStream = null;
try {
outputStream = new ByteArrayOutputStream();
ImgUtil.scale(inputStream, outputStream, scale);
return new ByteArrayInputStream(outputStream.toByteArray());
} catch (Exception e) {
log.error("Image compression failed, scale: {}", scale, e);
throw new BusinessException(SYSTEM_ERROR);
} finally {
IoUtil.close(inputStream);
IoUtil.close(outputStream);
}
}
}
View on GitHub (pinned to 5e758547a8)