fork(2) 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.setStrategy(Deflater.DEFLATED);
  49. compresser.setInput(input);
  50. compresser.finish();
  51. int compressedDataLength = compresser.deflate(output);
  52. compresser.end();
  53.  
  54. System.out.println("output:");
  55. HelloWorld.printB(output, compressedDataLength);
  56. HelloWorld.printD(output, compressedDataLength);
  57. HelloWorld.printR(output, compressedDataLength);
  58.  
  59. // Decompress the bytes
  60. Inflater decompresser = new Inflater();
  61. decompresser.setInput(output, 0, compressedDataLength);
  62. byte[] result = new byte[100];
  63. int resultLength = decompresser.inflate(result);
  64. decompresser.end();
  65.  
  66. System.out.println("decompress:");
  67. HelloWorld.printB(result, resultLength);
  68.  
  69. // Decode the bytes into a String
  70. String outputString = new String(result, 0, resultLength, "UTF-8");
  71. System.out.println(outputString);
  72. } catch(java.lang.IllegalArgumentException ex) {
  73. ex.printStackTrace();
  74. } catch(java.io.UnsupportedEncodingException ex) {
  75. // handle
  76. } catch (java.util.zip.DataFormatException ex) {
  77. // handle
  78. }
  79.  
  80.  
  81. }
  82.  
  83. }
Success #stdin #stdout #stderr 0.09s 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 
stderr
java.lang.IllegalArgumentException
	at java.util.zip.Deflater.setStrategy(Deflater.java:273)
	at HelloWorld.main(Main.java:48)