-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder.java
More file actions
73 lines (69 loc) · 1.97 KB
/
decoder.java
File metadata and controls
73 lines (69 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.BitSet;
public class decoder {
public static void main(String args[]) throws IOException{
File encodedBinaryFile=new File(args[0]);
File codeTable=new File(args[1]);
File DecodedFile=new File("decoded.txt");
FileInputStream fileinputStream=null;
BufferedWriter bW=null;
BufferedReader br=null;
try{
long start=System.currentTimeMillis();
br=new BufferedReader(new FileReader(codeTable));
DecoderTree dTree=new DecoderTree();
codeTableToDecoderTree(br,dTree);
bW=new BufferedWriter(new FileWriter(DecodedFile));
fileinputStream=new FileInputStream(encodedBinaryFile);
long size=encodedBinaryFile.length();
writeDecodeDataToFile(bW,fileinputStream,dTree,(int)size);
//System.out.println("Time taken: "+(System.currentTimeMillis()-start));
//System.out.println("Data Decoded");
}
finally{
if (fileinputStream != null) {
fileinputStream.close();
}
if(bW!=null){
bW.close();
}
if(br!=null){
br.close();
}
}
}
public static void writeDecodeDataToFile(BufferedWriter bW, FileInputStream fis, DecoderTree dTree, int size) throws IOException{
DecoderNode dNode=dTree.getRoot();
byte[] b=new byte[size];
fis.read(b);
BitSet bitset=BitSet.valueOf(b);
bitset.set((b.length)*8);
for(int i=0;i<bitset.length()-1;i++){
if(!bitset.get(i)){
dNode=dNode.left;
}
else{
dNode=dNode.right;
}
if(dNode.isEnd){
bW.write(""+dNode.data);
bW.newLine();
dNode=dTree.getRoot();
}
}
}
public static void codeTableToDecoderTree(BufferedReader br, DecoderTree dTree) throws IOException{
String line=null;
String[] dataValue=null;
while ((line=br.readLine())!=null) {
dataValue=line.split(" ");
dTree.insert(dataValue[1],dTree.getRoot(),Integer.parseInt(dataValue[0]));
}
}
}