Decompresses a list of LZ77 tokens back into the original string. @param compressedData The list of Token objects. Must not be null. @return The original, uncompressed string.
(List<Token> compressedData)
| 140 | * @return The original, uncompressed string. |
| 141 | */ |
| 142 | public static String decompress(List<Token> compressedData) { |
| 143 | if (compressedData == null) { |
| 144 | return ""; |
| 145 | } |
| 146 | |
| 147 | StringBuilder decompressedText = new StringBuilder(); |
| 148 | |
| 149 | for (Token token : compressedData) { |
| 150 | // Copy matched characters from the sliding window |
| 151 | if (token.length > 0) { |
| 152 | int startIndex = decompressedText.length() - token.offset; |
| 153 | |
| 154 | // Handle overlapping matches (e.g., when length > offset) |
| 155 | for (int i = 0; i < token.length; i++) { |
| 156 | decompressedText.append(decompressedText.charAt(startIndex + i)); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | // Append the next character (if not END_OF_STREAM) |
| 161 | if (token.nextChar != END_OF_STREAM) { |
| 162 | decompressedText.append(token.nextChar); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | return decompressedText.toString(); |
| 167 | } |
| 168 | } |