-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBitWriter.java
115 lines (98 loc) · 2.37 KB
/
BitWriter.java
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class BitWriter{
private OutputStream stream;
private int BitsWritten;
private int TotalBytes;
private String str;
public BitWriter(String filename) throws IOException{
try {
stream = new FileOutputStream(filename);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BitsWritten=0;
str = "";
}
public void total(int bits) {
TotalBytes = bits;
}
public void writing(boolean b) throws IOException {
BitsWritten++;
int Zeros = 0;
int BytesWritten = 0;
byte y = 00000000;
byte t = 00000001;
if(!b)
str+="0";
else if(b)
str+="1";
//Method One: String to Integer to Byte
/*if(BitsWritten == 8) {
System.out.print("str: ");
System.out.println(str);
Byte a= (byte)(int)Integer.valueOf(str, 2);
System.out.println(Integer.toBinaryString(Integer.valueOf(str, 2)));
stream.write(a);
numBitsWritten = 0;
str="";
written++;
}
*/
//Method Two: Bit Manipulation
if(BitsWritten == 8) {
System.out.print("str: ");
System.out.println(str);
for(int j = 0; j<str.length()-1; j++) {
BytesWritten++;
if(str.charAt(j)=='1') {
y = (byte) (y|t);
y = (byte) (y << 1);
}
else if(str.charAt(j)=='0') {
y = (byte) (y << 1);
}
}
if(str.charAt(str.length()-1) =='1')
y = (byte) (y|t);
String s1 = String.format("%8s", Integer.toBinaryString(y & 0xFF)).replace(' ', '0');
System.out.println(s1);
stream.write(y);
BitsWritten = 0;
BytesWritten++;
str="";
}
}
public void end() throws IOException {
int Zeros = 0;
int BytesWritten = 0;
byte y = 00000000;
byte t = 00000001;
System.out.print("str: ");
System.out.println(str);
for(int j = 0; j<str.length(); j++) {
BytesWritten++;
if(str.charAt(j)=='1') {
y = (byte) (y|t);
y = (byte) (y << 1);
}
else if(str.charAt(j)=='0') {
y = (byte) (y << 1);
}
}
Zeros = (8 - str.length());
y = (byte) (y << (Zeros-1));
String s1 = String.format("%8s", Integer.toBinaryString(y & 0xFF)).replace(' ', '0');
System.out.println(s1);
stream.write(y);
stream.write(-1);
stream.write(Zeros);
System.out.println(Zeros);
}
public void close() throws IOException {
stream.close();
}
}