-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryOut.java
More file actions
122 lines (112 loc) · 3.84 KB
/
BinaryOut.java
File metadata and controls
122 lines (112 loc) · 3.84 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
115
116
117
118
119
120
121
122
import java.io.BufferedOutputStream;
import java.io.IOException;
/*
Pour la sortie binaire nous nous sommes servis d'une partie du code de sedgewick que nous avons
adapté a la situation de notre devoir.
* <i>Binary output</i>. This class provides methods for converting
* primtive type variables ({@code boolean}, {@code byte}, {@code char},
* {@code int}, {@code long}, {@code float}, and {@code double})
* to sequences of bits and writing them to an output stream.
* The output stream can be standard output, a file, an OutputStream or a Socket.
* Uses big-endian (most-significant byte first).
* <p>
* The client must {@code flush()} the output stream when finished writing bits.
* <p>
* The client should not intermix calls to {@code BinaryOut} with calls
* to {@code Out}; otherwise unexpected behavior will result.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public final class BinaryOut {
private BufferedOutputStream out; // the output stream
private int buffer; // 8-bit buffer of bits to write out
private int n; // number of bits remaining in buffer
/**
* Initializes a binary output stream from standard output.
*/
public BinaryOut() {
out = new BufferedOutputStream(System.out);
}
/**
* Writes the specified bit to the binary output stream.
* @param x the bit
*/
public void writeBit(boolean x) {
// add bit to buffer
buffer <<= 1;
if (x) buffer |= 1;
// if buffer is full (8 bits), write out as a single byte
n++;
if (n == 8) clearBuffer();
}
// write out any remaining bits in buffer to the binary output stream, padding with 0s
private void clearBuffer() {
if (n == 0) return;
if (n > 0) buffer <<= (8 - n);
try {
out.write(buffer);
}
catch (IOException e) {
e.printStackTrace();
}
n = 0;
buffer = 0;
}
/*
* Flushes the binary output stream, padding 0s if number of bits written so far
* is not a multiple of 8.
*/
public void flush() {
clearBuffer();
try {
out.flush();
}
catch (IOException e) {
e.printStackTrace();
}
}
/*
* Flushes and closes the binary output stream.
* Once it is closed, bits can no longer be written.
*/
public void close() {
flush();
try {
out.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
/*
* Writes the specified bit to the binary output stream.
* @param x the {@code boolean} to write
*/
public void write(boolean x) {
writeBit(x);
}
}
/******************************************************************************
* Copyright 2002-2018, Robert Sedgewick and Kevin Wayne.
*
* This file is part of algs4.jar, which accompanies the textbook
*
* Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,
* Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.
* http://algs4.cs.princeton.edu
*
*
* algs4.jar is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* algs4.jar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with algs4.jar. If not, see http://www.gnu.org/licenses.
******************************************************************************/