类 BinaryIn
- java.lang.Object
-
- edu.princeton.cs.algs4.BinaryIn
-
public final class BinaryIn extends java.lang.Object
Binary input. This class provides methods for reading in bits from a binary input stream, either one bit at a time (as aboolean
), 8 bits at a time (as abyte
orchar
), 16 bits at a time (as ashort
), 32 bits at a time (as anint
orfloat
), or 64 bits at a time (as adouble
orlong
).The binary input stream can be from standard input, a filename, a URL name, a Socket, or an InputStream.
All primitive types are assumed to be represented using their standard Java representations, in big-endian (most significant byte first) order.
The client should not intermix calls to
BinaryIn
with calls toIn
; otherwise unexpected behavior will result.
-
-
构造器概要
构造器 构造器 说明 BinaryIn()
Initializes a binary input stream from standard input.BinaryIn(java.io.InputStream is)
Initializes a binary input stream from anInputStream
.BinaryIn(java.lang.String name)
Initializes a binary input stream from a filename or URL name.BinaryIn(java.net.Socket socket)
Initializes a binary input stream from a socket.BinaryIn(java.net.URL url)
Initializes a binary input stream from a URL.
-
方法概要
修饰符和类型 方法 说明 boolean
exists()
Returns true if this binary input stream exists.boolean
isEmpty()
Returns true if this binary input stream is empty.static void
main(java.lang.String[] args)
Unit tests theBinaryIn
data type.boolean
readBoolean()
Reads the next bit of data from this binary input stream and return as a boolean.byte
readByte()
Reads the next 8 bits from this binary input stream and return as an 8-bit byte.char
readChar()
Reads the next 8 bits from this binary input stream and return as an 8-bit char.char
readChar(int r)
Reads the next r bits from this binary input stream and return as an r-bit character.double
readDouble()
Reads the next 64 bits from this binary input stream and return as a 64-bit double.float
readFloat()
Reads the next 32 bits from this binary input stream and return as a 32-bit float.int
readInt()
Reads the next 32 bits from this binary input stream and return as a 32-bit int.int
readInt(int r)
Reads the next r bits from this binary input stream return as an r-bit int.long
readLong()
Reads the next 64 bits from this binary input stream and return as a 64-bit long.short
readShort()
Reads the next 16 bits from this binary input stream and return as a 16-bit short.java.lang.String
readString()
Reads the remaining bytes of data from this binary input stream and return as a string.
-
-
-
构造器详细资料
-
BinaryIn
public BinaryIn()
Initializes a binary input stream from standard input.
-
BinaryIn
public BinaryIn(java.io.InputStream is)
Initializes a binary input stream from anInputStream
.- 参数:
is
- theInputStream
object
-
BinaryIn
public BinaryIn(java.net.Socket socket)
Initializes a binary input stream from a socket.- 参数:
socket
- the socket
-
BinaryIn
public BinaryIn(java.net.URL url)
Initializes a binary input stream from a URL.- 参数:
url
- the URL
-
BinaryIn
public BinaryIn(java.lang.String name)
Initializes a binary input stream from a filename or URL name.- 参数:
name
- the name of the file or URL
-
-
方法详细资料
-
exists
public boolean exists()
Returns true if this binary input stream exists.- 返回:
true
if this binary input stream exists;false
otherwise
-
isEmpty
public boolean isEmpty()
Returns true if this binary input stream is empty.- 返回:
true
if this binary input stream is empty;false
otherwise
-
readBoolean
public boolean readBoolean()
Reads the next bit of data from this binary input stream and return as a boolean.- 返回:
- the next bit of data from this binary input stream as a
boolean
- 抛出:
java.util.NoSuchElementException
- if this binary input stream is empty
-
readChar
public char readChar()
Reads the next 8 bits from this binary input stream and return as an 8-bit char.- 返回:
- the next 8 bits of data from this binary input stream as a
char
- 抛出:
java.util.NoSuchElementException
- if there are fewer than 8 bits available
-
readChar
public char readChar(int r)
Reads the next r bits from this binary input stream and return as an r-bit character.- 参数:
r
- number of bits to read- 返回:
- the next
r
bits of data from this binary input streamt as achar
- 抛出:
java.util.NoSuchElementException
- if there are fewer thanr
bits availablejava.lang.IllegalArgumentException
- unless1 <= r <= 16
-
readString
public java.lang.String readString()
Reads the remaining bytes of data from this binary input stream and return as a string.- 返回:
- the remaining bytes of data from this binary input stream as a
String
- 抛出:
java.util.NoSuchElementException
- if this binary input stream is empty or if the number of bits available is not a multiple of 8 (byte-aligned)
-
readShort
public short readShort()
Reads the next 16 bits from this binary input stream and return as a 16-bit short.- 返回:
- the next 16 bits of data from this binary input stream as a
short
- 抛出:
java.util.NoSuchElementException
- if there are fewer than 16 bits available
-
readInt
public int readInt()
Reads the next 32 bits from this binary input stream and return as a 32-bit int.- 返回:
- the next 32 bits of data from this binary input stream as a
int
- 抛出:
java.util.NoSuchElementException
- if there are fewer than 32 bits available
-
readInt
public int readInt(int r)
Reads the next r bits from this binary input stream return as an r-bit int.- 参数:
r
- number of bits to read- 返回:
- the next
r
bits of data from this binary input stream as aint
- 抛出:
java.util.NoSuchElementException
- if there are fewer than r bits availablejava.lang.IllegalArgumentException
- unless1 <= r <= 32
-
readLong
public long readLong()
Reads the next 64 bits from this binary input stream and return as a 64-bit long.- 返回:
- the next 64 bits of data from this binary input stream as a
long
- 抛出:
java.util.NoSuchElementException
- if there are fewer than 64 bits available
-
readDouble
public double readDouble()
Reads the next 64 bits from this binary input stream and return as a 64-bit double.- 返回:
- the next 64 bits of data from this binary input stream as a
double
- 抛出:
java.util.NoSuchElementException
- if there are fewer than 64 bits available
-
readFloat
public float readFloat()
Reads the next 32 bits from this binary input stream and return as a 32-bit float.- 返回:
- the next 32 bits of data from this binary input stream as a
float
- 抛出:
java.util.NoSuchElementException
- if there are fewer than 32 bits available
-
readByte
public byte readByte()
Reads the next 8 bits from this binary input stream and return as an 8-bit byte.- 返回:
- the next 8 bits of data from this binary input stream as a
byte
- 抛出:
java.util.NoSuchElementException
- if there are fewer than 8 bits available
-
main
public static void main(java.lang.String[] args)
Unit tests theBinaryIn
data type. Reads the name of a file or URL (first command-line argument) and writes it to a file (second command-line argument).- 参数:
args
- the command-line arguments
-
-