fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. import java.util.LinkedList;
  9.  
  10. // one class needs to have a main() method
  11. class VariableByte {
  12.  
  13. static class Byte {
  14. int[] abyte;
  15.  
  16. Byte(){
  17. abyte = new int[8];
  18. }
  19.  
  20. public void readInt(int n) {
  21. // n must be less than 128 !!
  22. String bin = Integer.toBinaryString(n);
  23.  
  24. for(int i = 0 ; i < (8 - bin.length()) ; i++) {
  25. abyte[i] = 0;
  26. }
  27. for(int i = 0 ; i < bin.length() ; i++){
  28. abyte[i + (8 - bin.length())] = bin.charAt(i) - 48; // ASCII code for '0' is 48
  29. }
  30. //System.out.println(" Byte ***** " + this.toString());
  31. }
  32.  
  33. public void switchFirst(){
  34. abyte[0] = 1;
  35. }
  36.  
  37. public int toInt(){
  38. //System.out.println(" Byte ***** " + this.toString());
  39. int res = 0;
  40. for (int i = 0 ; i < 8 ; i++){
  41. res += abyte[i] * Math.pow(2, (7 - i));
  42. }
  43. //System.out.println(" Value ***** " + res);
  44. return res;
  45. }
  46.  
  47. public String toString(){
  48. String res ="";
  49. for(int i = 0 ; i < 8 ; i++) {
  50. res += abyte[i];
  51. }
  52. return res;
  53. }
  54.  
  55. }
  56.  
  57.  
  58. public static LinkedList<Byte> vbEncode(LinkedList<Integer> numbers) {
  59. LinkedList<Byte> code = new LinkedList<Byte>();
  60. while (numbers.size() > 0) {
  61. int n = numbers.poll();
  62. code.addAll(vbEncodeNumber(n));
  63. }
  64. return code;
  65. }
  66.  
  67. public static LinkedList<Byte> vbEncodeNumber(int n) {
  68. LinkedList<Byte> bytestream = new LinkedList<Byte>();
  69. int num = n;
  70. while (true) {
  71. Byte b = new Byte();
  72. b.readInt(num % 128);
  73. bytestream.addFirst(b);
  74. if (num < 128) {break;}
  75. num /= 128; //right-shift of length 7 (128 = 2^7)
  76. }
  77. Byte last = bytestream.get(bytestream.size() - 1); //retrieving the last byte
  78. last.switchFirst(); //setting the continuation bit to 1
  79. return bytestream;
  80. }
  81.  
  82. public static LinkedList<Integer> vbDecode(LinkedList<Byte> code){
  83. LinkedList<Integer> numbers = new LinkedList<Integer>();
  84. int n = 0;
  85. for(int i = 0 ; !(code.isEmpty()) ; i++){
  86. Byte b = code.poll(); // read leading byte
  87. //System.out.println(" Reading byte " + b.toString() );
  88.  
  89. int bi = b.toInt(); // decimal value of this byte
  90. if (bi < 128) { //continuation bit is set to 0
  91. n = 128 * n + bi;
  92. } else { // continuation bit is set to 1
  93. n = 128 * n + (bi - 128);
  94. numbers.add(n); // number is stored
  95. n = 0; // reset
  96. }
  97. }
  98. return numbers;
  99. }
  100.  
  101. public static void main(String[] args) {
  102. LinkedList<Integer> test = new LinkedList<Integer>();
  103. test.add(825);
  104. //test.add(Integer.parseInt(args[0]));
  105. //test.add(1234);
  106.  
  107. //System.out.println("Input values: 5 - 824 - 1234");
  108.  
  109. LinkedList<Byte> code = vbEncode(test);
  110.  
  111. System.out.println("Variable-byte code:");
  112. for(int i = 0 ; i < code.size() ; i++){
  113. System.out.print(code.get(i).toString() + " ");
  114. }
  115. System.out.println();
  116.  
  117. LinkedList<Integer> decode = vbDecode(code);
  118.  
  119. System.out.println("After decoding:");
  120. for(int i = 0 ; i < decode.size() ; i++){
  121. System.out.print(decode.get(i) + " ");
  122. }
  123. System.out.println();
  124. }
  125. }
Success #stdin #stdout 0.05s 711168KB
stdin
Standard input is empty
stdout
Variable-byte code:
00000110 10111001 
After decoding:
825