-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNewthread.java
More file actions
67 lines (54 loc) · 1.47 KB
/
Newthread.java
File metadata and controls
67 lines (54 loc) · 1.47 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
package Newthread;
import java.util.Arrays;
import MakeHuffmanTree.MakeHuffmanTree;
import Encode.Encode;
import java.util.BitSet;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.File;
public class Newthread implements Runnable {
String text;
String name;
String filename;
String folder;
public Thread thread;
public Newthread(String text,String name ,String filename ,String folder)
{
this.text=text;
this.name=name;
this.filename = filename;
this.folder = folder;
thread = new Thread(this,name);
System.out.println(name+" started...");
thread.start();
}
public void run()
{
String [] codes = new String [128];
for (int i=0;i<128;i++)
codes[i]="";
MakeHuffmanTree tree = new MakeHuffmanTree(text);
tree.MakeTree(codes);
Encode en = new Encode(text);
String result ="";
result = en.encode(codes);
BitSet bits = new BitSet(result.length());
for(int i=0;i<result.length() ;i++)
bits.set(i);
byte[] bytes = new byte[bits.length()/8+1];
for (int i=0; i<bits.length(); i++) {
if (bits.get(i)) {
bytes[bytes.length-i/8-1] |= 1<<(i%8);
}
}
File directory = new File(folder);
if (! directory.exists()){
directory.mkdir();
}
try (FileOutputStream fileOuputStream = new FileOutputStream("./"+folder+"/"+filename)) {
fileOuputStream.write(bytes);
} catch (IOException e) {
e.printStackTrace();
}
}
}