public final class BinaryOut extends Object
boolean
, byte
, char
,
int
, long
, float
, and 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).
The client must flush()
the output stream when finished writing bits.
The client should not intermix calls to BinaryOut
with calls
to Out
; otherwise unexpected behavior will result.
Constructor and Description |
---|
BinaryOut()
Initializes a binary output stream from standard output.
|
BinaryOut(OutputStream os)
Initializes a binary output stream from an
OutputStream . |
BinaryOut(Socket socket)
Initializes a binary output stream from a socket.
|
BinaryOut(String filename)
Initializes a binary output stream from a file.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Flushes and closes the binary output stream.
|
void |
flush()
Flushes the binary output stream, padding 0s if number of bits written so far
is not a multiple of 8.
|
static void |
main(String[] args)
Test client.
|
void |
write(boolean x)
Writes the specified bit to the binary output stream.
|
void |
write(byte x)
Writes the 8-bit byte to the binary output stream.
|
void |
write(char x)
Writes the 8-bit char to the binary output stream.
|
void |
write(char x,
int r)
Writes the r-bit char to the binary output stream.
|
void |
write(double x)
Writes the 64-bit double to the binary output stream.
|
void |
write(float x)
Writes the 32-bit float to the binary output stream.
|
void |
write(int x)
Writes the 32-bit int to the binary output stream.
|
void |
write(int x,
int r)
Writes the r-bit int to the binary output stream.
|
void |
write(long x)
Writes the 64-bit long to the binary output stream.
|
void |
write(short x)
Write the 16-bit int to the binary output stream.
|
void |
write(String s)
Writes the string of 8-bit characters to the binary output stream.
|
void |
write(String s,
int r)
Writes the string of r-bit characters to the binary output stream.
|
public BinaryOut()
public BinaryOut(OutputStream os)
OutputStream
.os
- the OutputStream
public BinaryOut(String filename)
filename
- the name of the filepublic BinaryOut(Socket socket)
socket
- the socketpublic void flush()
public void close()
public void write(boolean x)
x
- the boolean
to writepublic void write(byte x)
x
- the byte
to write.public void write(int x)
x
- the int
to writepublic void write(int x, int r)
x
- the int
to writer
- the number of relevant bits in the charIllegalArgumentException
- unless r
is between 1 and 32IllegalArgumentException
- unless x
is between 0 and 2r - 1public void write(double x)
x
- the double
to writepublic void write(long x)
x
- the long
to writepublic void write(float x)
x
- the float
to writepublic void write(short x)
x
- the short
to write.public void write(char x)
x
- the char
to writeIllegalArgumentException
- unless x
is between 0 and 255public void write(char x, int r)
x
- the char
to writer
- the number of relevant bits in the charIllegalArgumentException
- unless r
is between 1 and 16IllegalArgumentException
- unless x
is between 0 and 2r - 1public void write(String s)
s
- the String
to writeIllegalArgumentException
- if any character in the string is not
between 0 and 255public void write(String s, int r)
s
- the String
to writer
- the number of relevant bits in each characterIllegalArgumentException
- unless r is between 1 and 16IllegalArgumentException
- if any character in the string is not
between 0 and 2r - 1public static void main(String[] args)
args
- the command-line arguments