类 BinaryOut
- java.lang.Object
-
- edu.princeton.cs.algs4.BinaryOut
-
public final class BinaryOut extends java.lang.ObjectBinary output. This class provides methods for converting primtive 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
BinaryOutwith calls toOut; otherwise unexpected behavior will result.
-
-
构造器概要
构造器 构造器 说明 BinaryOut()Initializes a binary output stream from standard output.BinaryOut(java.io.OutputStream os)Initializes a binary output stream from anOutputStream.BinaryOut(java.lang.String filename)Initializes a binary output stream from a file.BinaryOut(java.net.Socket socket)Initializes a binary output stream from a socket.
-
方法概要
修饰符和类型 方法 说明 voidclose()Flushes and closes the binary output stream.voidflush()Flushes the binary output stream, padding 0s if number of bits written so far is not a multiple of 8.static voidmain(java.lang.String[] args)Test client.voidwrite(boolean x)Writes the specified bit to the binary output stream.voidwrite(byte x)Writes the 8-bit byte to the binary output stream.voidwrite(char x)Writes the 8-bit char to the binary output stream.voidwrite(char x, int r)Writes the r-bit char to the binary output stream.voidwrite(double x)Writes the 64-bit double to the binary output stream.voidwrite(float x)Writes the 32-bit float to the binary output stream.voidwrite(int x)Writes the 32-bit int to the binary output stream.voidwrite(int x, int r)Writes the r-bit int to the binary output stream.voidwrite(long x)Writes the 64-bit long to the binary output stream.voidwrite(short x)Write the 16-bit int to the binary output stream.voidwrite(java.lang.String s)Writes the string of 8-bit characters to the binary output stream.voidwrite(java.lang.String s, int r)Writes the string of r-bit characters to the binary output stream.
-
-
-
构造器详细资料
-
BinaryOut
public BinaryOut()
Initializes a binary output stream from standard output.
-
BinaryOut
public BinaryOut(java.io.OutputStream os)
Initializes a binary output stream from anOutputStream.- 参数:
os- theOutputStream
-
BinaryOut
public BinaryOut(java.lang.String filename)
Initializes a binary output stream from a file.- 参数:
filename- the name of the file
-
BinaryOut
public BinaryOut(java.net.Socket socket)
Initializes a binary output stream from a socket.- 参数:
socket- the socket
-
-
方法详细资料
-
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.- 参数:
x- thebooleanto write
-
write
public void write(byte x)
Writes the 8-bit byte to the binary output stream.- 参数:
x- thebyteto write.
-
write
public void write(int x)
Writes the 32-bit int to the binary output stream.- 参数:
x- theintto write
-
write
public void write(int x, int r)Writes the r-bit int to the binary output stream.- 参数:
x- theintto writer- the number of relevant bits in the char- 抛出:
java.lang.IllegalArgumentException- unlessris between 1 and 32java.lang.IllegalArgumentException- unlessxis between 0 and 2r - 1
-
write
public void write(double x)
Writes the 64-bit double to the binary output stream.- 参数:
x- thedoubleto write
-
write
public void write(long x)
Writes the 64-bit long to the binary output stream.- 参数:
x- thelongto write
-
write
public void write(float x)
Writes the 32-bit float to the binary output stream.- 参数:
x- thefloatto write
-
write
public void write(short x)
Write the 16-bit int to the binary output stream.- 参数:
x- theshortto write.
-
write
public void write(char x)
Writes the 8-bit char to the binary output stream.- 参数:
x- thecharto write- 抛出:
java.lang.IllegalArgumentException- unlessxis betwen 0 and 255
-
write
public void write(char x, int r)Writes the r-bit char to the binary output stream.- 参数:
x- thecharto writer- the number of relevant bits in the char- 抛出:
java.lang.IllegalArgumentException- unlessris between 1 and 16java.lang.IllegalArgumentException- unlessxis between 0 and 2r - 1
-
write
public void write(java.lang.String s)
Writes the string of 8-bit characters to the binary output stream.- 参数:
s- theStringto write- 抛出:
java.lang.IllegalArgumentException- if any character in the string is not between 0 and 255
-
write
public void write(java.lang.String s, int r)Writes the string of r-bit characters to the binary output stream.- 参数:
s- theStringto writer- the number of relevants bits in each character- 抛出:
java.lang.IllegalArgumentException- unless r is between 1 and 16java.lang.IllegalArgumentException- if any character in the string is not between 0 and 2r - 1
-
main
public static void main(java.lang.String[] args)
Test client. Read bits from standard input and write to the file specified on command line.- 参数:
args- the command-line arguments
-
-