public final class BinaryIn extends Object
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.
Constructor and Description |
---|
BinaryIn()
Initializes a binary input stream from standard input.
|
BinaryIn(InputStream is)
Initializes a binary input stream from an
InputStream . |
BinaryIn(Socket socket)
Initializes a binary input stream from a socket.
|
BinaryIn(String name)
Initializes a binary input stream from a filename or URL name.
|
BinaryIn(URL url)
Initializes a binary input stream from a URL.
|
Modifier and Type | Method and Description |
---|---|
boolean |
exists()
Returns true if this binary input stream exists.
|
boolean |
isEmpty()
Returns true if this binary input stream is empty.
|
static void |
main(String[] args)
Unit tests the
BinaryIn 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.
|
String |
readString()
Reads the remaining bytes of data from this binary input stream and return as a string.
|
public BinaryIn()
public BinaryIn(InputStream is)
InputStream
.is
- the InputStream
objectpublic BinaryIn(Socket socket)
socket
- the socketpublic BinaryIn(URL url)
url
- the URLpublic BinaryIn(String name)
name
- the name of the file or URLpublic boolean exists()
true
if this binary input stream exists;
false
otherwisepublic boolean isEmpty()
true
if this binary input stream is empty;
false
otherwisepublic boolean readBoolean()
boolean
NoSuchElementException
- if this binary input stream is emptypublic char readChar()
char
NoSuchElementException
- if there are fewer than 8 bits availablepublic char readChar(int r)
r
- number of bits to readr
bits of data from this binary input stream as a char
NoSuchElementException
- if there are fewer than r
bits availableIllegalArgumentException
- unless 1 <= r <= 16
public String readString()
String
NoSuchElementException
- if this binary input stream is empty or if the number of bits
available is not a multiple of 8 (byte-aligned)public short readShort()
short
NoSuchElementException
- if there are fewer than 16 bits availablepublic int readInt()
int
NoSuchElementException
- if there are fewer than 32 bits availablepublic int readInt(int r)
r
- number of bits to readr
bits of data from this binary input stream as a int
NoSuchElementException
- if there are fewer than r bits availableIllegalArgumentException
- unless 1 <= r <= 32
public long readLong()
long
NoSuchElementException
- if there are fewer than 64 bits availablepublic double readDouble()
double
NoSuchElementException
- if there are fewer than 64 bits availablepublic float readFloat()
float
NoSuchElementException
- if there are fewer than 32 bits availablepublic byte readByte()
byte
NoSuchElementException
- if there are fewer than 8 bits availablepublic static void main(String[] args)
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).args
- the command-line arguments