[英]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. 例如,如果一个文件包含这个数字序列:824 5 214577应用可变字节编码,这个序列将编码为000001101011100010000101000011010000110010110001。现在我想知道如何在另一个文件中写入,以便从中生成一种压缩文件原版的。 and similarly how to read it. 同样如何阅读它。 I'm using JAVA . 我正在使用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. 第二次尝试似乎有效...输出文件大小为6字节,而第一次尝试则为48字节。 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). 如果我们通常在文件中写入这个整数序列,它的大小将是12个字节(3 * 4个字节)。 but now it is just 6 bytes. 但现在只有6个字节。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.