| 36 | |
| 37 | // apply move-to-front decoding, reading from standard input and writing to standard output |
| 38 | public static void decode() { |
| 39 | char[] index2Char = init(); |
| 40 | while (!BinaryStdIn.isEmpty()) { |
| 41 | int idx = BinaryStdIn.readChar(); |
| 42 | char c = index2Char[idx]; |
| 43 | for (int i = idx; i > 0; i--) |
| 44 | index2Char[i] = index2Char[i - 1]; |
| 45 | index2Char[0] = c; |
| 46 | |
| 47 | BinaryStdOut.write(c, 8); |
| 48 | } |
| 49 | BinaryStdOut.close(); |
| 50 | } |
| 51 | |
| 52 | // if args[0] is "-", apply move-to-front encoding |
| 53 | // if args[0] is "+", apply move-to-front decoding |