{"record":{"id":"a758411a4b351a9c","repo":"TheAlgorithms/Java","slug":"malformed-encoded-string-incomplete-sequence-endi","errorCode":null,"errorMessage":"Malformed encoded string: incomplete sequence ending.","messagePattern":"Malformed encoded string: incomplete sequence ending\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/compression/HuffmanCoding.java","lineNumber":238,"sourceCode":"            return sb.toString();\n        }\n\n        Node current = root;\n        for (char bit : encodedText.toCharArray()) {\n            if (bit != '0' && bit != '1') {\n                throw new IllegalArgumentException(\"Encoded text contains invalid characters: \" + bit);\n            }\n\n            current = (bit == '0') ? current.left : current.right;\n\n            if (current.isLeaf()) {\n                sb.append(current.ch);\n                current = root;\n            }\n        }\n\n        if (current != root) {\n            throw new IllegalArgumentException(\"Malformed encoded string: incomplete sequence ending.\");\n        }\n\n        return sb.toString();\n    }\n\n    /**\n     * Retrieves the generated Huffman dictionary mapping characters to their binary codes.\n     *\n     * @return An unmodifiable map containing the character-to-binary-code mappings to prevent\n     * external mutation of the algorithm's state.\n     */\n    public Map<Character, String> getHuffmanCodes() {\n        return huffmanCodes;\n    }\n}\n","sourceCodeStart":220,"sourceCodeEnd":254,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/compression/HuffmanCoding.java#L220-L254","documentation":"After consuming all bits, HuffmanCoding.decode checks that the traversal returned to the root — meaning the last sequence of bits ended exactly on a leaf node. If current != root, the payload ended mid-code (trailing bits with no complete leaf), which indicates truncation or corruption, so the library rejects it.","triggerScenarios":"Decoding \"010\" with a tree whose valid codes are all 2 bits long (the trailing '0' leaves the walker stranded); a payload truncated by one or more bits; bits dropped during transport.","commonSituations":"The encoded string was truncated in storage/transit; bits were accidentally dropped or appended; the payload was hand-edited and left incomplete.","solutions":["Use the exact full bit string produced by encode without truncation.","Round-trip test encode->decode to confirm payload integrity.","Store the encoded length alongside the payload so truncation is detectable."],"exampleFix":"// before\nString dec = hc.decode(encoded.substring(0, encoded.length() - 1)); // truncated\n\n// after\nString dec = hc.decode(encoded); // full, complete sequence","handlingStrategy":"validation","validationCode":"// Detect trailing incomplete sequence by checking length is a multiple of max code length is not robust;\n// best defense: use the intact encode() output unchanged.\nString dec = hc.decode(encodedText); // pass the full, untruncated bit string","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use the exact bit string from encode without truncation.","Round-trip test encode->decode to confirm integrity.","Store the encoded length to detect truncation before decoding."],"tags":["compression","huffman","truncation","validation","illegal-argument"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}