public class NetworkInputStream
extends java.io.InputStream
Socket
s or DatagramSocket
s.Constructor and Description |
---|
NetworkInputStream(java.net.DatagramSocket socketIn)
Constructs a new NetworkInputStream with the specified
DatagramSocket . |
NetworkInputStream(java.net.Socket socketIn)
Constructs a new NetworkInputStream with the specified
Socket . |
Modifier and Type | Method and Description |
---|---|
int |
available()
Returns an estimate of the number of bytes that can be read (or
skipped over) from this input stream without blocking by the next
invocation of a method for this input stream.
|
void |
close()
Closes this input stream and releases any system resources associated
with the stream.
|
java.net.InetAddress |
getAddress()
Gets the current
InetAddress of the stream. |
java.net.DatagramSocket |
getDatagramSocket()
Gets the datagram socket in use or null.
|
java.net.InetAddress |
getLocalAddress()
Gets the local
InetAddress of the stream. |
java.lang.Integer |
getLocalPort()
Gets the local port of the stream.
|
int |
getPacketSize()
Gets the current datagram packet size.
|
java.lang.Integer |
getPort()
Gets the current port of the stream.
|
java.net.Socket |
getSocket()
Gets the socket in use or null.
|
int |
read()
Reads the next byte of data from the input stream.
|
byte[] |
readPacket()
Reads a single datagram packet.
|
int |
readPacket(byte[] b)
Reads a single datagram packet into the specified buffer storing with no offset.
|
int |
readPacket(byte[] b,
int off,
int len)
Reads a single datagram packet into the specified buffer
storing from the specified offset and the specified number of bytes.
|
void |
setDatagramSocket(java.net.DatagramSocket socketIn)
Sets the datagram socket in use.
|
void |
setSocket(java.net.Socket socketIn)
Sets the socket in use.
|
public NetworkInputStream(java.net.Socket socketIn)
Socket
.socketIn
- The socket to use.java.lang.NullPointerException
- socketIn is null.public NetworkInputStream(java.net.DatagramSocket socketIn)
DatagramSocket
.socketIn
- The datagram socket to use.java.lang.NullPointerException
- socketIn is null.public int read() throws java.io.IOException
int
in the range 0
to
255
. If no byte is available because the end of the stream
has been reached, the value -1
is returned. This method
blocks until input data is available, the end of the stream is detected,
or an exception is thrown.read
in class java.io.InputStream
-1
if the end of the
stream is reached.java.io.IOException
- if an I/O error occurs.public byte[] readPacket() throws java.io.IOException
java.io.IOException
- if an I/O error occurs, stream closed or not using a datagram socket.public int readPacket(byte[] b) throws java.io.IOException
b
- The buffer to store the packet in.java.lang.NullPointerException
- b is null.java.io.IOException
- if an I/O error occurs, stream closed or not using a datagram socket.public int readPacket(byte[] b, int off, int len) throws java.io.IOException
b
- The buffer to store the packet in.off
- The offset to store in the buffer from.len
- The number of bytes to store in the buffer.java.lang.NullPointerException
- b is null.java.lang.IndexOutOfBoundsException
- if off is negative, len is negative, or len is greater than the difference of the length of the buffer and off.java.io.IOException
- if an I/O error occurs, stream closed or not using a datagram socket.public int getPacketSize() throws java.io.IOException
java.io.IOException
- stream closed or not using a datagram socket.public java.net.InetAddress getAddress()
InetAddress
of the stream.
Can be null.public java.lang.Integer getPort()
public java.net.InetAddress getLocalAddress()
InetAddress
of the stream.
Can be null.public java.lang.Integer getLocalPort()
public java.net.Socket getSocket()
public void setSocket(java.net.Socket socketIn) throws java.io.IOException
socketIn
- The socket to now use.java.lang.NullPointerException
- socketIn is null.java.io.IOException
- stream closed or not using a socket.public java.net.DatagramSocket getDatagramSocket()
public void setDatagramSocket(java.net.DatagramSocket socketIn) throws java.io.IOException
socketIn
- The datagram socket to now use.java.lang.NullPointerException
- socketIn is null.java.io.IOException
- stream closed or not using a datagram socket.public int available() throws java.io.IOException
available
in class java.io.InputStream
0
when
it reaches the end of the input stream.java.io.IOException
- if an I/O error occurs.public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.InputStream
java.io.IOException
- if an I/O error occurs.