{"record":{"id":"8d857aa86f21b949","repo":"TheAlgorithms/Java","slug":"window-size-and-lookahead-buffer-size-must-be-posi","errorCode":null,"errorMessage":"Window size and lookahead buffer size must be positive.","messagePattern":"Window size and lookahead buffer size must be positive\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/compression/LZ77.java","lineNumber":59,"sourceCode":"     */\n    public record Token(int offset, int length, char nextChar) {\n    }\n\n    /**\n     * Compresses the input text using the LZ77 algorithm.\n     *\n     * @param text The input string to compress. Must not be null.\n     * @param windowSize The size of the sliding window (search buffer). Must be positive.\n     * @param lookaheadBufferSize The size of the lookahead buffer. Must be positive.\n     * @return A list of {@link Token} objects representing the compressed data.\n     * @throws IllegalArgumentException if windowSize or lookaheadBufferSize are not positive.\n     */\n    public static List<Token> compress(String text, int windowSize, int lookaheadBufferSize) {\n        if (text == null) {\n            return new ArrayList<>();\n        }\n        if (windowSize <= 0 || lookaheadBufferSize <= 0) {\n            throw new IllegalArgumentException(\"Window size and lookahead buffer size must be positive.\");\n        }\n\n        List<Token> compressedOutput = new ArrayList<>();\n        int currentPosition = 0;\n\n        while (currentPosition < text.length()) {\n            int bestMatchDistance = 0;\n            int bestMatchLength = 0;\n\n            // Define the start of the search window\n            int searchBufferStart = Math.max(0, currentPosition - windowSize);\n            // Define the end of the lookahead buffer (don't go past text length)\n            int lookaheadEnd = Math.min(currentPosition + lookaheadBufferSize, text.length());\n\n            // Search for the longest match in the window\n            for (int i = searchBufferStart; i < currentPosition; i++) {\n                int currentMatchLength = 0;\n","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/compression/LZ77.java#L41-L77","documentation":"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.","triggerScenarios":"Calling compress(text, 0, 16), compress(text, 16, 0), compress(text, -1, 16), or compress(text, 16, -5).","commonSituations":"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.","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)."],"exampleFix":"// before\nList<Token> out = LZ77.compress(text, winSize, laSize); // one is 0\n\n// after\nint win = Math.max(1, winSize);\nint la = Math.max(1, laSize);\nList<Token> out = LZ77.compress(text, win, la);","handlingStrategy":"validation","validationCode":"if (windowSize <= 0 || lookaheadBufferSize <= 0) {\n    throw new IllegalArgumentException(\"windowSize and lookaheadBufferSize must be >= 1\");\n}\nList<LZ77.Token> out = LZ77.compress(text, windowSize, lookaheadBufferSize);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["compression","lz77","validation","illegal-argument"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}