/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
import java.util.LinkedList;
// one class needs to have a main() method
class VariableByte {
int[] abyte;
abyte = new int[8];
}
public void readInt(int n) {
// n must be less than 128 !!
for(int i = 0 ; i < (8 - bin.length()) ; i++) {
abyte[i] = 0;
}
for(int i = 0 ; i < bin.length() ; i++){
abyte[i + (8 - bin.length())] = bin.charAt(i) - 48; // ASCII code for '0' is 48
}
//System.out.println(" Byte ***** " + this.toString());
}
public void switchFirst(){
abyte[0] = 1;
}
public int toInt(){
//System.out.println(" Byte ***** " + this.toString());
int res = 0;
for (int i = 0 ; i < 8 ; i++){
res
+= abyte
[i
] * Math.
pow(2,
(7 - i
)); }
//System.out.println(" Value ***** " + res);
return res;
}
for(int i = 0 ; i < 8 ; i++) {
res += abyte[i];
}
return res;
}
}
public static LinkedList<Byte> vbEncode(LinkedList<Integer> numbers) {
LinkedList<Byte> code = new LinkedList<Byte>();
while (numbers.size() > 0) {
int n = numbers.poll();
code.addAll(vbEncodeNumber(n));
}
return code;
}
public static LinkedList<Byte> vbEncodeNumber(int n) {
LinkedList<Byte> bytestream = new LinkedList<Byte>();
int num = n;
while (true) {
b.readInt(num % 128);
bytestream.addFirst(b);
if (num < 128) {break;}
num /= 128; //right-shift of length 7 (128 = 2^7)
}
Byte last
= bytestream.
get(bytestream.
size() - 1); //retrieving the last byte last.switchFirst(); //setting the continuation bit to 1
return bytestream;
}
public static LinkedList<Integer> vbDecode(LinkedList<Byte> code){
LinkedList<Integer> numbers = new LinkedList<Integer>();
int n = 0;
for(int i = 0 ; !(code.isEmpty()) ; i++){
Byte b
= code.
poll(); // read leading byte //System.out.println(" Reading byte " + b.toString() );
int bi = b.toInt(); // decimal value of this byte
if (bi < 128) { //continuation bit is set to 0
n = 128 * n + bi;
} else { // continuation bit is set to 1
n = 128 * n + (bi - 128);
numbers.add(n); // number is stored
n = 0; // reset
}
}
return numbers;
}
public static void main
(String[] args
) { LinkedList<Integer> test = new LinkedList<Integer>();
test.add(138);
//test.add(Integer.parseInt(args[0]));
//test.add(1234);
//System.out.println("Input values: 5 - 824 - 1234");
LinkedList<Byte> code = vbEncode(test);
System.
out.
println("Variable-byte code:"); for(int i = 0 ; i < code.size() ; i++){
System.
out.
print(code.
get(i
).
toString() + " "); }
LinkedList<Integer> decode = vbDecode(code);
System.
out.
println("After decoding:"); for(int i = 0 ; i < decode.size() ; i++){
System.
out.
print(decode.
get(i
) + " "); }
}
}