Package stdlib

Class BinaryIn

java.lang.Object
stdlib.BinaryIn

public final class BinaryIn extends Object
Binary input. 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.

  • Field Summary Link icon

    Fields
    Modifier and Type
    Field
    Description
    private int
     
    private static final int
     
     
    private int
     
  • Constructor Summary Link icon

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

    Modifier and Type
    Method
    Description
    boolean
    Does the binary input stream exist?
    private void
     
    boolean
    Returns true if the binary input stream is empty.
    static void
    main(String[] args)
    Test client.
    boolean
    Read the next bit of data from the binary input stream and return as a boolean.
    byte
    Read the next 8 bits from the binary input stream and return as an 8-bit byte.
    char
    Read the next 8 bits from the binary input stream and return as an 8-bit char.
    char
    readChar(int r)
    Read the next r bits from the binary input stream and return as an r-bit character.
    double
    Read the next 64 bits from the binary input stream and return as a 64-bit double.
    float
    Read the next 32 bits from standard input and return as a 32-bit float.
    int
    Read the next 32 bits from the binary input stream and return as a 32-bit int.
    int
    readInt(int r)
    Read the next r bits from the binary input stream return as an r-bit int.
    long
    Read the next 64 bits from the binary input stream and return as a 64-bit long.
    short
    Read the next 16 bits from the binary input stream and return as a 16-bit short.
    Read the remaining bytes of data from the binary input stream and return as a string.

    Methods inherited from class java.lang.Object Link icon

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details Link icon

  • Constructor Details Link icon

    • BinaryIn Link icon

      public BinaryIn()
      Create a binary input stream from standard input.
    • BinaryIn Link icon

      public BinaryIn(InputStream is)
      Create a binary input stream from an InputStream.
    • BinaryIn Link icon

      public BinaryIn(Socket socket)
      Create a binary input stream from a socket.
    • BinaryIn Link icon

      public BinaryIn(URL url)
      Create a binary input stream from a URL.
    • BinaryIn Link icon

      public BinaryIn(String s)
      Create a binary input stream from a filename or URL name.
  • Method Details Link icon

    • fillBuffer Link icon

      private void fillBuffer()
    • exists Link icon

      public boolean exists()
      Does the binary input stream exist?
    • isEmpty Link icon

      public boolean isEmpty()
      Returns true if the binary input stream is empty.
      Returns:
      true if and only if the binary input stream is empty
    • readBoolean Link icon

      public boolean readBoolean()
      Read the next bit of data from the binary input stream and return as a boolean.
      Returns:
      the next bit of data from the binary input stream as a boolean
      Throws:
      RuntimeException - if the input stream is empty
    • readChar Link icon

      public char readChar()
      Read the next 8 bits from the binary input stream and return as an 8-bit char.
      Returns:
      the next 8 bits of data from the binary input stream as a char
      Throws:
      RuntimeException - if there are fewer than 8 bits available
    • readChar Link icon

      public char readChar(int r)
      Read the next r bits from the 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 the binary input streamt as a char
      Throws:
      RuntimeException - if there are fewer than r bits available
    • readString Link icon

      public String readString()
      Read the remaining bytes of data from the binary input stream and return as a string.
      Returns:
      the remaining bytes of data from the binary input stream as a String
      Throws:
      RuntimeException - if the input stream is empty or if the number of bits available is not a multiple of 8 (byte-aligned)
    • readShort Link icon

      public short readShort()
      Read the next 16 bits from the binary input stream and return as a 16-bit short.
      Returns:
      the next 16 bits of data from the binary standard input as a short
      Throws:
      RuntimeException - if there are fewer than 16 bits available
    • readInt Link icon

      public int readInt()
      Read the next 32 bits from the binary input stream and return as a 32-bit int.
      Returns:
      the next 32 bits of data from the binary input stream as a int
      Throws:
      RuntimeException - if there are fewer than 32 bits available
    • readInt Link icon

      public int readInt(int r)
      Read the next r bits from the binary input stream return as an r-bit int.
      Parameters:
      r - number of bits to read.
      Returns:
      the next r bits of data from the binary input stream as a int
      Throws:
      RuntimeException - if there are fewer than r bits available on standard input
    • readLong Link icon

      public long readLong()
      Read the next 64 bits from the binary input stream and return as a 64-bit long.
      Returns:
      the next 64 bits of data from the binary input stream as a long
      Throws:
      RuntimeException - if there are fewer than 64 bits available
    • readDouble Link icon

      public double readDouble()
      Read the next 64 bits from the binary input stream and return as a 64-bit double.
      Returns:
      the next 64 bits of data from the binary input stream as a double
      Throws:
      RuntimeException - if there are fewer than 64 bits available
    • readFloat Link icon

      public float readFloat()
      Read 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:
      RuntimeException - if there are fewer than 32 bits available on standard input
    • readByte Link icon

      public byte readByte()
      Read the next 8 bits from the binary input stream and return as an 8-bit byte.
      Returns:
      the next 8 bits of data from the binary input stream as a byte
      Throws:
      RuntimeException - if there are fewer than 8 bits available
    • main Link icon

      public static void main(String[] args)
      Test client. Reads in the name of a file or url (first command-line argument) and writes it to a file (second command-line argument).