{"record":{"id":"463ce38147b45fd4","repo":"TheAlgorithms/Java","slug":"bad-compressed-k","errorCode":null,"errorMessage":"Bad compressed k: {}","messagePattern":"Bad compressed k: (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/compression/LZW.java","lineNumber":124,"sourceCode":"        Map<Integer, String> dictionary = new HashMap<>();\n        for (int i = 0; i < dictSize; i++) {\n            dictionary.put(i, \"\" + (char) i);\n        }\n\n        // Decompress the first code\n        String w = \"\" + (char) (int) compressed.removeFirst();\n        StringBuilder result = new StringBuilder(w);\n\n        for (int k : compressed) {\n            String entry;\n            if (dictionary.containsKey(k)) {\n                // The code is in the dictionary\n                entry = dictionary.get(k);\n            } else if (k == dictSize) {\n                // Special case for sequences like \"ababab\"\n                entry = w + w.charAt(0);\n            } else {\n                throw new IllegalArgumentException(\"Bad compressed k: \" + k);\n            }\n\n            result.append(entry);\n\n            // Add new sequence to the dictionary\n            dictionary.put(dictSize++, w + entry.charAt(0));\n\n            w = entry;\n        }\n        return result.toString();\n    }\n}\n","sourceCodeStart":106,"sourceCodeEnd":137,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/compression/LZW.java#L106-L137","documentation":"LZW.decompress walks the code list rebuilding the dictionary. For each code k it must either exist in the current dictionary or equal dictSize (the special next-code case). If k is neither in the dictionary nor the next available code, the stream is invalid — the code references a string that cannot be reconstructed, so the method rejects the corrupt input.","triggerScenarios":"Calling decompress on a hand-edited or truncated list; decompress([99999]) where 99999 is far beyond any dictionary entry; codes reordered or a negative value inserted; mixing codes from a different compression run.","commonSituations":"The compressed code list was corrupted in storage/transit; a partial list was passed (missing leading codes so the dictionary never grew far enough); codes were produced by a different LZW variant/implementation.","solutions":["Pass the exact List<Integer> returned by LZW.compress without modification.","Verify the list is non-null, ordered, and contains no gaps relative to the expected dictionary growth.","Ensure compress and decompress use the same LZW implementation and initial dictionary (ASCII 0-255 here)."],"exampleFix":"// before\nList<Integer> codes = readCodesFromFile(); // may be corrupted/partial\nString out = LZW.decompress(codes);\n\n// after\nList<Integer> codes = LZW.compress(original); // produce + transport intact\nString out = LZW.decompress(codes);","handlingStrategy":"try-catch","validationCode":"// Cannot fully validate a compressed stream without decoding; best pre-check is structural integrity.\nif (compressed == null || compressed.isEmpty()) return \"\";\n// Ensure codes are non-negative and ordered consistently with dictionary growth.\nfor (int k : compressed) {\n    if (k < 0) throw new IllegalArgumentException(\"Negative code in compressed stream: \" + k);\n}","typeGuard":null,"tryCatchPattern":"try {\n    String out = LZW.decompress(compressed);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().startsWith(\"Bad compressed k:\")) {\n        // handle corrupt stream: log, request retransmission, or fall back\n        throw new IllegalStateException(\"LZW stream is corrupt or truncated\", e);\n    }\n    throw e;\n}","preventionTips":["Pass the exact List<Integer> from compress unchanged.","Use the same LZW implementation for compress and decompress.","Round-trip test compress->decompress to confirm the stream is self-consistent."],"tags":["compression","lzw","corruption","validation","illegal-argument"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}