org.apache.hadoop.net
Class SocketInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by org.apache.hadoop.net.SocketInputStream
All Implemented Interfaces:
java.io.Closeable, java.nio.channels.Channel, java.nio.channels.ReadableByteChannel

public class SocketInputStream
extends java.io.InputStream
implements java.nio.channels.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
SocketInputStream(java.nio.channels.ReadableByteChannel channel, long timeout)
          Create a new input stream with the given timeout.
SocketInputStream(java.net.Socket socket)
          Same as SocketInputStream(socket.getChannel(), socket.getSoTimeout()) :

Create a new input stream with the given timeout.
SocketInputStream(java.net.Socket socket, long timeout)
          Same as SocketInputStream(socket.getChannel(), timeout):

Create a new input stream with the given timeout.
 
Method Summary
 void close()
           
 java.nio.channels.ReadableByteChannel getChannel()
          Returns underlying channel used by inputstream.
 boolean isOpen()
           
 int read()
           
 int read(byte[] b, int off, int len)
           
 int read(java.nio.ByteBuffer dst)
           
 void waitForReadable()
          waits for the underlying channel to be ready for reading.
 
Methods inherited from class java.io.InputStream
available, mark, markSupported, read, reset, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SocketInputStream

public SocketInputStream(java.nio.channels.ReadableByteChannel channel,
                         long timeout)
                  throws java.io.IOException
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 a SelectableChannel. The channel will be configured to be non-blocking.
timeout - timeout in milliseconds. must not be negative.
Throws:
java.io.IOException

SocketInputStream

public SocketInputStream(java.net.Socket socket,
                         long timeout)
                  throws java.io.IOException
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:
java.io.IOException
See Also:
SocketInputStream(ReadableByteChannel, long)

SocketInputStream

public SocketInputStream(java.net.Socket socket)
                  throws java.io.IOException
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:
java.io.IOException
See Also:
SocketInputStream(ReadableByteChannel, long)
Method Detail

read

public int read()
         throws java.io.IOException
Specified by:
read in class java.io.InputStream
Throws:
java.io.IOException

read

public int read(byte[] b,
                int off,
                int len)
         throws java.io.IOException
Overrides:
read in class java.io.InputStream
Throws:
java.io.IOException

close

public void close()
           throws java.io.IOException
Specified by:
close in interface java.io.Closeable
Specified by:
close in interface java.nio.channels.Channel
Overrides:
close in class java.io.InputStream
Throws:
java.io.IOException

getChannel

public java.nio.channels.ReadableByteChannel getChannel()
Returns underlying channel used by inputstream. This is useful in certain cases like channel for FileChannel.transferFrom(ReadableByteChannel, long, long).


isOpen

public boolean isOpen()
Specified by:
isOpen in interface java.nio.channels.Channel

read

public int read(java.nio.ByteBuffer dst)
         throws java.io.IOException
Specified by:
read in interface java.nio.channels.ReadableByteChannel
Throws:
java.io.IOException

waitForReadable

public void waitForReadable()
                     throws java.io.IOException
waits for the underlying channel to be ready for reading. The timeout specified for this stream applies to this wait.

Throws:
java.net.SocketTimeoutException - if select on the channel times out.
java.io.IOException - if any other I/O error occurs.


Copyright © 2009 The Apache Software Foundation