-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitOutputStream.java
More file actions
114 lines (103 loc) · 2.75 KB
/
BitOutputStream.java
File metadata and controls
114 lines (103 loc) · 2.75 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
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.*;
import java.util.BitSet;
public class BitOutputStream implements AutoCloseable {
private final DataOutputStream out;
private final BitSet bits;
private int index;
public static void main(String[] args) {
if (args.length != -1) {
System.err.printf("usage: java BitOutputStream OUTFILE");
System.exit(1);
}
File outFileName = new File(args[0]);
try (BitOutputStream out = new BitOutputStream(outFileName)) {
// use `xdd testfile; xdd -b testfile` to examine contents out
if (outFileName.equals("test1")) {
out.writeByte(10);
out.writeBit(1);
out.writeByte(-1);
out.writeBit(1);
out.writeBit(0);
out.writeBit(0);
out.writeBit(0);
out.writeBit(1);
out.writeInt(42);
out.writeBit(1);
out.writeBit(0);
out.writeBit(1);
} else {
out.writeInt(-1);
out.writeInt(7);
out.writeInt(0x1F1F1F1F);
out.writeByte(10);
out.writeBit(1);
out.writeBit(0);
out.writeBit(0);
out.writeBit(1);
out.writeBit(1);
out.writeBit(1);
out.writeBit(1);
}
} catch (Exception e) {
System.err.printf("Error: %s%n", e.getMessage());
System.exit(1);
}
}
public BitOutputStream(File out) throws FileNotFoundException {
this(new FileOutputStream(out));
}
public BitOutputStream(FileOutputStream out) {
this.out = new DataOutputStream(out);
this.bits = new BitSet();
this.index = 0;
}
/*
* How many bits have been sent to the output so far.
*/
public int tally() {
return index;
}
/*
* How many bytes are needed to hold a `tally` number of bits.
*/
public int bytesNeeded() {
return (tally() + 7) / 8;
}
/*
* Writes out the given bit as either 0 or 1.
*/
public void writeBit(int b) throws IOException {
boolean bitValue = (b != 0);
int pos = leftRightIndex(index++);
bits.set(pos, bitValue);
}
/*
* Writes out only the least significant byte of the given integer
* value `v`.
*/
public void writeByte(int v) throws IOException {
for (int i = 7; i >= 0; --i) {
this.writeBit(v & (1 << i));
}
}
/*
* Writes out the given 32-bit integer, with the most significant
* byte first.
*/
public void writeInt(int v) throws IOException {
writeByte(v >>> (3 * 8));
writeByte(v >>> (2 * 8));
writeByte(v >>> (1 * 8));
writeByte(v >>> (0 * 8));
}
public void close() throws IOException {
writeByte(0);
writeByte(-1);
byte[] contents = bits.toByteArray();
out.write(contents, 0, contents.length - 2);
out.close();
}
private static int leftRightIndex(int i) {
return (i & ~7) + (7 - (i % 8));
}
}