Package edu.princeton.cs.algs4
Class BinaryStdIn
- Object
-
- edu.princeton.cs.algs4.BinaryStdIn
-
public final class BinaryStdIn extends Object
TheBinaryStdInclass provides static methods for reading in bits from standard input. It can process the bits one bit at a time (as aboolean), 8 bits at a time (as abyteorchar), 16 bits at a time (as ashort), 32 bits at a time (as anintorfloat), or 64 bits at a time (as adoubleorlong).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
BinaryStdInwith calls toStdInorSystem.in; otherwise unexpected behavior will result.- Author:
- Robert Sedgewick, Kevin Wayne
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidclose()Close this input stream and release any associated system resources.static booleanisEmpty()Returns true if standard input is empty.static voidmain(String[] args)Test client.static booleanreadBoolean()Reads the next bit of data from standard input and return as a boolean.static bytereadByte()Reads the next 8 bits from standard input and return as an 8-bit byte.static charreadChar()Reads the next 8 bits from standard input and return as an 8-bit char.static charreadChar(int r)Reads the next r bits from standard input and return as an r-bit character.static doublereadDouble()Reads the next 64 bits from standard input and return as a 64-bit double.static floatreadFloat()Reads the next 32 bits from standard input and return as a 32-bit float.static intreadInt()Reads the next 32 bits from standard input and return as a 32-bit int.static intreadInt(int r)Reads the next r bits from standard input and return as an r-bit int.static longreadLong()Reads the next 64 bits from standard input and return as a 64-bit long.static shortreadShort()Reads the next 16 bits from standard input and return as a 16-bit short.static StringreadString()Reads the remaining bytes of data from standard input and return as a string.
-
-
-
Method Detail
-
close
public static void close()
Close this input stream and release any associated system resources.
-
isEmpty
public static boolean isEmpty()
Returns true if standard input is empty.- Returns:
- true if and only if standard input is empty
-
readBoolean
public static boolean readBoolean()
Reads the next bit of data from standard input and return as a boolean.- Returns:
- the next bit of data from standard input as a
boolean - Throws:
NoSuchElementException- if standard input is empty
-
readChar
public static char readChar()
Reads the next 8 bits from standard input and return as an 8-bit char. Note thatcharis a 16-bit type; to read the next 16 bits as a char, usereadChar(16).- Returns:
- the next 8 bits of data from standard input as a
char - Throws:
NoSuchElementException- if there are fewer than 8 bits available on standard input
-
readChar
public static char readChar(int r)
Reads the next r bits from standard input and return as an r-bit character.- Parameters:
r- number of bits to read.- Returns:
- the next r bits of data from standard input as a
char - Throws:
NoSuchElementException- if there are fewer thanrbits available on standard inputIllegalArgumentException- unless1 <= r <= 16
-
readString
public static String readString()
Reads the remaining bytes of data from standard input and return as a string.- Returns:
- the remaining bytes of data from standard input as a
String - Throws:
NoSuchElementException- if standard input is empty or if the number of bits available on standard input is not a multiple of 8 (byte-aligned)
-
readShort
public static short readShort()
Reads the next 16 bits from standard input and return as a 16-bit short.- Returns:
- the next 16 bits of data from standard input as a
short - Throws:
NoSuchElementException- if there are fewer than 16 bits available on standard input
-
readInt
public static int readInt()
Reads the next 32 bits from standard input and return as a 32-bit int.- Returns:
- the next 32 bits of data from standard input as a
int - Throws:
NoSuchElementException- if there are fewer than 32 bits available on standard input
-
readInt
public static int readInt(int r)
Reads the next r bits from standard input and return as an r-bit int.- Parameters:
r- number of bits to read.- Returns:
- the next r bits of data from standard input as a
int - Throws:
NoSuchElementException- if there are fewer thanrbits available on standard inputIllegalArgumentException- unless1 <= r <= 32
-
readLong
public static long readLong()
Reads the next 64 bits from standard input and return as a 64-bit long.- Returns:
- the next 64 bits of data from standard input as a
long - Throws:
NoSuchElementException- if there are fewer than 64 bits available on standard input
-
readDouble
public static double readDouble()
Reads the next 64 bits from standard input and return as a 64-bit double.- Returns:
- the next 64 bits of data from standard input as a
double - Throws:
NoSuchElementException- if there are fewer than 64 bits available on standard input
-
readFloat
public static float readFloat()
Reads the next 32 bits from standard input and return as a 32-bit float.- Returns:
- the next 32 bits of data from standard input as a
float - Throws:
NoSuchElementException- if there are fewer than 32 bits available on standard input
-
readByte
public static byte readByte()
Reads the next 8 bits from standard input and return as an 8-bit byte.- Returns:
- the next 8 bits of data from standard input as a
byte - Throws:
NoSuchElementException- if there are fewer than 8 bits available on standard input
-
main
public static void main(String[] args)
Test client. Reads in a binary input file from standard input and writes it to standard output.- Parameters:
args- the command-line arguments
-
-