Package org.apache.hadoop.net
Class SocketInputStream
java.lang.Object
java.io.InputStream
org.apache.hadoop.net.SocketInputStream
- All Implemented Interfaces:
Closeable,AutoCloseable,Channel,ReadableByteChannel
@LimitedPrivate("HDFS")
public class SocketInputStream
extends InputStream
implements ReadableByteChannel
This implements an input stream that can have a timeout while reading.
This sets non-blocking flag on the socket channel.
So after create this object, read() on
Socket.getInputStream() and write() on
Socket.getOutputStream() for the associated socket will throw
IllegalBlockingModeException.
Please use SocketOutputStream for writing.-
Constructor Summary
ConstructorsConstructorDescriptionSocketInputStream(Socket socket) Same as SocketInputStream(socket.getChannel(), socket.getSoTimeout()) :
Create a new input stream with the given timeout.SocketInputStream(Socket socket, long timeout) Same as SocketInputStream(socket.getChannel(), timeout):
Create a new input stream with the given timeout.SocketInputStream(ReadableByteChannel channel, long timeout) Create a new input stream with the given timeout. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()booleanisOpen()intread()intread(byte[] b, int off, int len) intread(ByteBuffer dst) voidsetTimeout(long timeoutMs) voidwaits for the underlying channel to be ready for reading.Methods inherited from class java.io.InputStream
available, mark, markSupported, nullInputStream, read, readAllBytes, readNBytes, readNBytes, reset, skip, skipNBytes, transferTo
-
Constructor Details
-
SocketInputStream
Create a new input stream with the given timeout. If the timeout is zero, it will be treated as infinite timeout. The socket's channel will be configured to be non-blocking.- Parameters:
channel- Channel for reading, should also be aSelectableChannel. The channel will be configured to be non-blocking.timeout- timeout in milliseconds. must not be negative.- Throws:
IOException- raised on errors performing I/O.
-
SocketInputStream
Same as SocketInputStream(socket.getChannel(), timeout):
Create a new input stream with the given timeout. If the timeout is zero, it will be treated as infinite timeout. The socket's channel will be configured to be non-blocking.- Parameters:
socket- should have a channel associated with it.timeout- timeout timeout in milliseconds. must not be negative.- Throws:
IOException- raised on errors performing I/O.- See Also:
-
SocketInputStream
Same as SocketInputStream(socket.getChannel(), socket.getSoTimeout()) :
Create a new input stream with the given timeout. If the timeout is zero, it will be treated as infinite timeout. The socket's channel will be configured to be non-blocking.- Parameters:
socket- should have a channel associated with it.- Throws:
IOException- raised on errors performing I/O.- See Also:
-
-
Method Details
-
read
- Specified by:
readin classInputStream- Throws:
IOException
-
read
- Overrides:
readin classInputStream- Throws:
IOException
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceChannel- Specified by:
closein interfaceCloseable- Overrides:
closein classInputStream- Throws:
IOException
-
getChannel
- Returns:
- Returns underlying channel used by inputstream.
This is useful in certain cases like channel for
FileChannel.transferFrom(ReadableByteChannel, long, long).
-
isOpen
public boolean isOpen() -
read
- Specified by:
readin interfaceReadableByteChannel- Throws:
IOException
-
waitForReadable
waits for the underlying channel to be ready for reading. The timeout specified for this stream applies to this wait.- Throws:
SocketTimeoutException- if select on the channel times out.IOException- if any other I/O error occurs.
-
setTimeout
public void setTimeout(long timeoutMs)
-