package com.jcomeau.misc; import com.jcomeau.Common; public class RLECompressor { public static String decompress(String rledata) throws Exception { StringBuffer compressed = new StringBuffer(rledata); StringBuffer decompressed = new StringBuffer(); Common.debugprintln("rledata: " + rledata); Common.debugprintln("compressed: " + compressed); for (int i = 0; i < compressed.length(); i++) { int j = compressed.charAt(i); Common.debugprintln("j: " + j); if (j < 0 || j > 0x7f) { // bytes would show negative, chars more reliable j = j & 0x7f; char c = decompressed.charAt(decompressed.length() - 1); for (int k = 0; k <= j; k++) { decompressed.append(c); } } else { decompressed.append((char)j); } } return decompressed.toString(); } public static void main(String[] args) throws Exception { Common.debugging = true; String test = "thi\203s is a test"; Common.debugprintln("test: " + test); if (args.length > 0) { test = args[0]; } System.out.println(decompress(test)); } }