Package stdlib

Class BinaryIn

java.lang.Object
stdlib.BinaryIn

public final class BinaryIn extends Object
This class provides methods for reading in bits from a binary input stream, either one bit at a time (as a boolean), 8 bits at a time (as a byte or char), 16 bits at a time (as a short), 32 bits at a time (as an int or float), or 64 bits at a time (as a double or long).

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 to In; otherwise unexpected behavior will result.

Author:
Robert Sedgewick, Kevin Wayne
  • Constructor Summary

    Constructors
    Constructor
    Description
    Initializes a binary input stream from standard input.
    Initializes a binary input stream from an InputStream.
    Initializes a binary input stream from a filename or URL name.
    BinaryIn(Socket socket)
    Initializes a binary input stream from a socket.
    Initializes a binary input stream from a URL.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Returns true if this binary input stream exists.
    boolean
    Returns true if this binary input stream is empty.
    static void
    main(String[] args)
    Unit tests the BinaryIn data type.
    boolean
    Reads the next bit of data from this binary input stream and return as a boolean.
    byte
    Reads the next 8 bits from this binary input stream and return as an 8-bit byte.
    char
    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
    Reads the next 64 bits from this binary input stream and return as a 64-bit double.
    float
    Reads the next 32 bits from this binary input stream and return as a 32-bit float.
    int
    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
    Reads the next 64 bits from this binary input stream and return as a 64-bit long.
    short
    Reads the next 16 bits from this binary input stream and return as a 16-bit short.
    Reads the remaining bytes of data from this binary input stream and return as a string.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • BinaryIn

      public BinaryIn()
      Initializes a binary input stream from standard input.
    • BinaryIn

      public BinaryIn(InputStream is)
      Initializes a binary input stream from an InputStream.
      Parameters:
      is - the InputStream object
    • BinaryIn

      public BinaryIn(Socket socket)
      Initializes a binary input stream from a socket.
      Parameters:
      socket - the socket
    • BinaryIn

      public BinaryIn(URL url)
      Initializes a binary input stream from a URL.
      Parameters:
      url - the URL
    • BinaryIn

      public BinaryIn(String name)
      Initializes a binary input stream from a filename or URL name.
      Parameters:
      name - the name of the file or URL
  • Method Details

    • exists

      public boolean exists()
      Returns true if this binary input stream exists.
      Returns:
      true if this binary input stream exists; false otherwise
    • isEmpty

      public boolean isEmpty()
      Returns true if this binary input stream is empty.
      Returns:
      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.
      Returns:
      the next bit of data from this binary input stream as a boolean
      Throws:
      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.
      Returns:
      the next 8 bits of data from this binary input stream as a char
      Throws:
      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.
      Parameters:
      r - number of bits to read
      Returns:
      the next r bits of data from this binary input streamt as a char
      Throws:
      NoSuchElementException - if there are fewer than r bits available
      IllegalArgumentException - unless 1 <= r <= 16
    • readString

      public String readString()
      Reads the remaining bytes of data from this binary input stream and return as a string.
      Returns:
      the remaining bytes of data from this binary input stream as a String
      Throws:
      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.
      Returns:
      the next 16 bits of data from this binary input stream as a short
      Throws:
      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.
      Returns:
      the next 32 bits of data from this binary input stream as a int
      Throws:
      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.
      Parameters:
      r - number of bits to read
      Returns:
      the next r bits of data from this binary input stream as a int
      Throws:
      NoSuchElementException - if there are fewer than r bits available
      IllegalArgumentException - unless 1 <= r <= 32
    • readLong

      public long readLong()
      Reads the next 64 bits from this binary input stream and return as a 64-bit long.
      Returns:
      the next 64 bits of data from this binary input stream as a long
      Throws:
      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.
      Returns:
      the next 64 bits of data from this binary input stream as a double
      Throws:
      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.
      Returns:
      the next 32 bits of data from this binary input stream as a float
      Throws:
      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.
      Returns:
      the next 8 bits of data from this binary input stream as a byte
      Throws:
      NoSuchElementException - if there are fewer than 8 bits available
    • main

      public static void main(String[] args)
      Unit tests the BinaryIn data type. Reads the name of a file or URL (first command-line argument) and writes it to a file (second command-line argument).
      Parameters:
      args - the command-line arguments