fork(1) download
  1. import java.util.zip.Deflater;
  2. import java.util.zip.Inflater;
  3.  
  4. class HelloWorld {
  5.  
  6. public HelloWorld() {
  7. }
  8.  
  9. private static void printB(byte[] d, int len) {
  10. for (int i = 0; i < len; i++) {
  11. System.out.printf("%02X ", d[i]);
  12. }
  13. System.out.println();
  14. }
  15.  
  16. private static void printD(byte[] d, int len) {
  17. for (int i = 0; i < len; i++) {
  18. String s = "00000000" + Integer.toString(0xFF & d[i], 2) + " ";
  19. System.out.print(s.substring(s.length() - 9));
  20. }
  21. System.out.println();
  22. }
  23.  
  24. private static void printR(byte[] d, int len) {
  25. for (int i = 0; i < len; i++) {
  26. String s = "00000000" + Integer.toString(0xFF & d[i], 2);
  27. StringBuilder b = new StringBuilder(s.substring(s.length() - 8));
  28. System.out.print(b.reverse() + " ");
  29. }
  30. System.out.println();
  31. }
  32.  
  33. public static void main(String[] args) throws java.lang.Exception {
  34.  
  35. try {
  36. // Encode a String into bytes
  37. String inputString = "blahblahblah";
  38. byte[] input = inputString.getBytes("UTF-8");
  39.  
  40. System.out.println("input:");
  41. HelloWorld.printB(input, input.length);
  42. HelloWorld.printD(input, input.length);
  43. HelloWorld.printR(input, input.length);
  44.  
  45. // Compress the bytes
  46. byte[] output = new byte[100];
  47. Deflater compresser = new Deflater();
  48. compresser.setInput(input);
  49. compresser.finish();
  50. int compressedDataLength = compresser.deflate(output);
  51. compresser.end();
  52.  
  53. System.out.println("output:");
  54. HelloWorld.printB(output, compressedDataLength);
  55. HelloWorld.printD(output, compressedDataLength);
  56. HelloWorld.printR(output, compressedDataLength);
  57.  
  58. // Decompress the bytes
  59. Inflater decompresser = new Inflater();
  60. decompresser.setInput(output, 0, compressedDataLength);
  61. byte[] result = new byte[100];
  62. int resultLength = decompresser.inflate(result);
  63. decompresser.end();
  64.  
  65. System.out.println("decompress:");
  66. HelloWorld.printB(result, resultLength);
  67.  
  68. // Decode the bytes into a String
  69. String outputString = new String(result, 0, resultLength, "UTF-8");
  70. System.out.println(outputString);
  71. } catch(java.io.UnsupportedEncodingException ex) {
  72. // handle
  73. } catch (java.util.zip.DataFormatException ex) {
  74. // handle
  75. }
  76.  
  77.  
  78. }
  79.  
  80. }
Success #stdin #stdout 0.08s 380544KB
stdin
Standard input is empty
stdout
input:
62 6C 61 68 62 6C 61 68 62 6C 61 68 
01100010 01101100 01100001 01101000 01100010 01101100 01100001 01101000 01100010 01101100 01100001 01101000 
01000110 00110110 10000110 00010110 01000110 00110110 10000110 00010110 01000110 00110110 10000110 00010110 
output:
78 9C 4B CA 49 CC 48 82 62 00 1F 02 04 C6 
01111000 10011100 01001011 11001010 01001001 11001100 01001000 10000010 01100010 00000000 00011111 00000010 00000100 11000110 
00011110 00111001 11010010 01010011 10010010 00110011 00010010 01000001 01000110 00000000 11111000 01000000 00100000 01100011 
decompress:
62 6C 61 68 62 6C 61 68 62 6C 61 68 
blahblahblah