org.apache.hadoop.net
Class SocketOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by org.apache.hadoop.net.SocketOutputStream
All Implemented Interfaces:
Closeable, Flushable, Channel, WritableByteChannel

public class SocketOutputStream
extends OutputStream
implements WritableByteChannel

This implements an output stream that can have a timeout while writing. This sets non-blocking flag on the socket channel. So after creating this object , read() on Socket.getInputStream() and write() on Socket.getOutputStream() on the associated socket will throw llegalBlockingModeException. Please use SocketInputStream for reading.


Constructor Summary
SocketOutputStream(Socket socket, long timeout)
          Same as SocketOutputStream(socket.getChannel(), timeout):

Create a new ouput stream with the given timeout.
SocketOutputStream(WritableByteChannel channel, long timeout)
          Create a new ouput stream with the given timeout.
 
Method Summary
 void close()
           
 WritableByteChannel getChannel()
          Returns underlying channel used by this stream.
 boolean isOpen()
           
 void transferToFully(FileChannel fileCh, long position, int count)
          Transfers data from FileChannel using FileChannel.transferTo(long, long, WritableByteChannel).
 void waitForWritable()
          waits for the underlying channel to be ready for writing.
 void write(byte[] b, int off, int len)
           
 int write(ByteBuffer src)
           
 void write(int b)
           
 
Methods inherited from class java.io.OutputStream
flush, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SocketOutputStream

public SocketOutputStream(WritableByteChannel channel,
                          long timeout)
                   throws IOException
Create a new ouput 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 writing, should also be a SelectableChannel. The channel will be configured to be non-blocking.
timeout - timeout in milliseconds. must not be negative.
Throws:
IOException

SocketOutputStream

public SocketOutputStream(Socket socket,
                          long timeout)
                   throws IOException
Same as SocketOutputStream(socket.getChannel(), timeout):

Create a new ouput 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
See Also:
SocketOutputStream(WritableByteChannel, long)
Method Detail

write

public void write(int b)
           throws IOException
Specified by:
write in class OutputStream
Throws:
IOException

write

public void write(byte[] b,
                  int off,
                  int len)
           throws IOException
Overrides:
write in class OutputStream
Throws:
IOException

close

public void close()
           throws IOException
Specified by:
close in interface Closeable
Specified by:
close in interface Channel
Overrides:
close in class OutputStream
Throws:
IOException

getChannel

public WritableByteChannel getChannel()
Returns underlying channel used by this stream. This is useful in certain cases like channel for FileChannel.transferTo(long, long, WritableByteChannel)


isOpen

public boolean isOpen()
Specified by:
isOpen in interface Channel

write

public int write(ByteBuffer src)
          throws IOException
Specified by:
write in interface WritableByteChannel
Throws:
IOException

waitForWritable

public void waitForWritable()
                     throws IOException
waits for the underlying channel to be ready for writing. 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.

transferToFully

public void transferToFully(FileChannel fileCh,
                            long position,
                            int count)
                     throws IOException
Transfers data from FileChannel using FileChannel.transferTo(long, long, WritableByteChannel). Similar to readFully(), this waits till requested amount of data is transfered.

Parameters:
fileCh - FileChannel to transfer data from.
position - position within the channel where the transfer begins
count - number of bytes to transfer.
Throws:
EOFException - If end of input file is reached before requested number of bytes are transfered.
SocketTimeoutException - If this channel blocks transfer longer than timeout for this stream.
IOException - Includes any exception thrown by FileChannel.transferTo(long, long, WritableByteChannel).


Copyright © 2009 The Apache Software Foundation