简体   繁体   中英

write/read variable byte encoded string representation to/from file in JAVA

everyone! I recently learned about variable byte encoding. for example, if a file contains this sequence of number: 824 5 214577 applying variable byte encoding this sequence would be encoded as 000001101011100010000101000011010000110010110001. Now I want to know how to write that in another file such that to produce a kind of compressed file from the original. and similarly how to read it. I'm using JAVA .

Have tried this:

LinkedList<Integer> numbers = new LinkedList<Integer>();
numbers.add(824);
numbers.add(5);
numbers.add(214577);
String code = VBEncoder.encodeToString(numbers);//returns 000001101011100010000101000011010000110010110001 into code
File file = new File("test.compressed");
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
out.writeBytes(code);
out.flush();

this just writes the binary representation into the file..and this is not what I'm expecting.

I have also tried this:

LinkedList<Integer> code = VBEncoder.encode(numbers);//returns linked list of Byte(i give its describtion later)
File file = new File("test.compressed");
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));

for(Byte b:code){
        out.write(b.toInt());
        System.out.println(b.toInt());
}
out.flush();
// he goes the describtion of the class Byte
class Byte {
    int[] abyte;
    Byte() {
        abyte = new int[8];
    }
    public void readInt(int n) {
        String bin = Integer.toBinaryString(n);

        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; 
        }
    }

    public void switchFirst() {
        abyte[0] = 1;
    }

    public int toInt() {
        int res = 0;
        for (int i = 0; i < 8; i++) {
            res += abyte[i] * Math.pow(2, (7 - i));
        }
        return res;
    }

    public static Byte fromString(String codestring) {
        Byte b = new Byte(); 
        for(int i=0; i < 8; i++)
            b.abyte[i] = (codestring.charAt(i)=='0')?0:1;
        return b;
    }

    public String toString() {
        String res = "";
        for (int i = 0; i < 8; i++) {
            res += abyte[i];
        }
        return res;
    }
}

its prints this in the console:

6
184
133
13
12
177

this second attempt seems to work...the output file size is 6 bytes while for the first attemps it was 48 bytes. but the problem in the second attempt is that I can't successfully read back the file.

InputStreamReader inStream = new InputStreamReader(new FileInputStream(file));

        int c = -1;
        while((c = inStream.read()) != -1){
            System.out.println( c );
        }

i get this:

6
184
8230
13
12
177

..so maybe I'm doing it the wrong way: expecting to receive some good advice from you. thanks!

It is solved; I was just not reading the file the right way:below is the right way:

DataInputStream inStream = null; 
inStream = new DataInputStream(new BufferedInputStream(newFileInputStream(file)));

int c = -1;
while((c = inStream.read()) != -1){
    Byte b = new Byte();
    b.readInt(c);
    System.out.println( c +":" + b.toString());
}

now I get this as the result:

6:00000110
184:10111000
133:10000101
13:00001101
12:00001100
177:10110001

Now the importance of writing the original sequence of integers into variable encoded bytes reduces the size of the file; if we normally write this sequence of integers in the file, its size would be 12 bytes (3 * 4 bytes). but now it is just 6 bytes.

int c = -1;
LinkedList<Byte> bytestream = new LinkedList<Byte>();
while((c = inStream.read()) != -1){
    Byte b = new Byte();
    b.readInt(c);
    bytestream.add(b);
}
LinkedList<Integer> numbers = VBEncoder.decode(bytestream);
for(Integer number:numbers) System.out.println(number);
//
//here goes the code of VBEncoder.decode
public static LinkedList<Integer> decode(LinkedList<Byte> code) {
    LinkedList<Integer> numbers = new LinkedList<Integer>();
    int n = 0;
    for (int i = 0; !(code.isEmpty()); i++) {
        Byte b = code.poll(); 
        int bi = b.toInt(); 
        if (bi < 128) {
            n = 128 * n + bi;
        } else { 
            n = 128 * n + (bi - 128);
            numbers.add(n); 
            n = 0; 
        }
    }
    return numbers;
}

I get back the sequence:

824
5
214577

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM