Class BinaryOut
- Object
-
- BinaryOut
-
public final class BinaryOut extends Object
TheBinaryOut
data type provides a basic capability for converting primitive type variables (boolean
,byte
,char
,int
,long
,float
, anddouble
) 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 toOut
; otherwise unexpected behavior will result.- Author:
- Robert Sedgewick, Kevin Wayne
-
-
Constructor Summary
Constructors Constructor Description BinaryOut()
Initializes a binary output stream from standard output.BinaryOut(OutputStream os)
Initializes a binary output stream from anOutputStream
.BinaryOut(String filename)
Initializes a binary output stream from a file.BinaryOut(Socket socket)
Initializes a binary output stream from a socket.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method 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.
-
-
-
Constructor Detail
-
BinaryOut
public BinaryOut()
Initializes a binary output stream from standard output.
-
BinaryOut
public BinaryOut(OutputStream os)
Initializes a binary output stream from anOutputStream
.- Parameters:
os
- theOutputStream
-
BinaryOut
public BinaryOut(String filename)
Initializes a binary output stream from a file.- Parameters:
filename
- the name of the file- Throws:
IllegalArgumentException
- iffilename
isnull
IllegalArgumentException
- iffilename
is the empty stringIllegalArgumentException
- if cannot write the filefilename
-
BinaryOut
public BinaryOut(Socket socket)
Initializes a binary output stream from a socket.- Parameters:
socket
- the socket- Throws:
IllegalArgumentException
- iffilename
isnull
IllegalArgumentException
- if cannot create output stream from socket
-
-
Method Detail
-
flush
public void flush()
Flushes the binary output stream, padding 0s if number of bits written so far is not a multiple of 8.
-
close
public void close()
Flushes and closes the binary output stream. Once it is closed, bits can no longer be written.
-
write
public void write(boolean x)
Writes the specified bit to the binary output stream.- Parameters:
x
- theboolean
to write
-
write
public void write(byte x)
Writes the 8-bit byte to the binary output stream.- Parameters:
x
- thebyte
to write.
-
write
public void write(int x)
Writes the 32-bit int to the binary output stream.- Parameters:
x
- theint
to write
-
write
public void write(int x, int r)
Writes the r-bit int to the binary output stream.- Parameters:
x
- theint
to writer
- the number of relevant bits in the char- Throws:
IllegalArgumentException
- unlessr
is between 1 and 32IllegalArgumentException
- unlessx
is between 0 and 2r - 1
-
write
public void write(double x)
Writes the 64-bit double to the binary output stream.- Parameters:
x
- thedouble
to write
-
write
public void write(long x)
Writes the 64-bit long to the binary output stream.- Parameters:
x
- thelong
to write
-
write
public void write(float x)
Writes the 32-bit float to the binary output stream.- Parameters:
x
- thefloat
to write
-
write
public void write(short x)
Write the 16-bit int to the binary output stream.- Parameters:
x
- theshort
to write.
-
write
public void write(char x)
Writes the 8-bit char to the binary output stream.- Parameters:
x
- thechar
to write- Throws:
IllegalArgumentException
- unlessx
is between 0 and 255
-
write
public void write(char x, int r)
Writes the r-bit char to the binary output stream.- Parameters:
x
- thechar
to writer
- the number of relevant bits in the char- Throws:
IllegalArgumentException
- unlessr
is between 1 and 16IllegalArgumentException
- unlessx
is between 0 and 2r - 1
-
write
public void write(String s)
Writes the string of 8-bit characters to the binary output stream.- Parameters:
s
- theString
to write- Throws:
IllegalArgumentException
- if any character in the string is not between 0 and 255
-
write
public void write(String s, int r)
Writes the string of r-bit characters to the binary output stream.- Parameters:
s
- theString
to writer
- the number of relevant bits in each character- Throws:
IllegalArgumentException
- unless r is between 1 and 16IllegalArgumentException
- if any character in the string is not between 0 and 2r - 1
-
main
public static void main(String[] args)
Test client. Read bits from standard input and write to the file specified on command line.- Parameters:
args
- the command-line arguments
-
-