TheAlgorithms/Java · error · IllegalArgumentException
Window size and lookahead buffer size must be positive.
Error message
Window size and lookahead buffer size must be positive.
What it means
LZ77.compress(String, int, int) uses windowSize to bound the search buffer and lookaheadBufferSize to bound the lookahead region. Both must be strictly positive because they define how far backward and forward the matcher scans; a zero or negative size makes the matching loop degenerate or impossible, so the method rejects it.
Source
Thrown at src/main/java/com/thealgorithms/compression/LZ77.java:59
*/
public record Token(int offset, int length, char nextChar) {
}
/**
* Compresses the input text using the LZ77 algorithm.
*
* @param text The input string to compress. Must not be null.
* @param windowSize The size of the sliding window (search buffer). Must be positive.
* @param lookaheadBufferSize The size of the lookahead buffer. Must be positive.
* @return A list of {@link Token} objects representing the compressed data.
* @throws IllegalArgumentException if windowSize or lookaheadBufferSize are not positive.
*/
public static List<Token> compress(String text, int windowSize, int lookaheadBufferSize) {
if (text == null) {
return new ArrayList<>();
}
if (windowSize <= 0 || lookaheadBufferSize <= 0) {
throw new IllegalArgumentException("Window size and lookahead buffer size must be positive.");
}
List<Token> compressedOutput = new ArrayList<>();
int currentPosition = 0;
while (currentPosition < text.length()) {
int bestMatchDistance = 0;
int bestMatchLength = 0;
// Define the start of the search window
int searchBufferStart = Math.max(0, currentPosition - windowSize);
// Define the end of the lookahead buffer (don't go past text length)
int lookaheadEnd = Math.min(currentPosition + lookaheadBufferSize, text.length());
// Search for the longest match in the window
for (int i = searchBufferStart; i < currentPosition; i++) {
int currentMatchLength = 0;
View on GitHub (pinned to fdfb9a395b)
Solutions
- Pass positive integers for both windowSize and lookaheadBufferSize (e.g. compress(text, 256, 64)).
- Validate config values are >= 1 before calling compress.
- Bound computed sizes with Math.max(1, value).
Example fix
// before List<Token> out = LZ77.compress(text, winSize, laSize); // one is 0 // after int win = Math.max(1, winSize); int la = Math.max(1, laSize); List<Token> out = LZ77.compress(text, win, la);
Defensive patterns
Strategy: validation
Validate before calling
if (windowSize <= 0 || lookaheadBufferSize <= 0) {
throw new IllegalArgumentException("windowSize and lookaheadBufferSize must be >= 1");
}
List<LZ77.Token> out = LZ77.compress(text, windowSize, lookaheadBufferSize); Prevention
- Validate config-derived sizes are >= 1 before calling compress.
- Bound computed sizes with Math.max(1, value).
- Default sizes to sensible positives (e.g. 256 and 64) rather than 0.
When it happens
Trigger: Calling compress(text, 0, 16), compress(text, 16, 0), compress(text, -1, 16), or compress(text, 16, -5).
Common situations: Window/lookahead sizes read from config as 0 or unset (defaulting to 0); a computed size that went negative; sizes derived from input length on very short input.
Related errors
- Input string cannot be null or empty.
- Original index must be between 0 and {}, got: {}
- Character '%c' (U+%04X) not found in Huffman dictionary.
- Invalid binary sequence for single-character tree.
- Encoded text contains invalid characters: {}
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/8d857aa86f21b949.
Report an issue: GitHub.