Package org.apache.hadoop.net
Class SocketOutputStream
java.lang.Object
java.io.OutputStream
org.apache.hadoop.net.SocketOutputStream
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable,Channel,WritableByteChannel
@LimitedPrivate({"HDFS","MapReduce"})
@Unstable
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
ConstructorsConstructorDescriptionSocketOutputStream(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
Modifier and TypeMethodDescriptionvoidclose()booleanisOpen()voidsetTimeout(int timeoutMs) voidtransferToFully(FileChannel fileCh, long position, int count) CalltransferToFully(FileChannel, long, int, LongWritable, LongWritable)with nullwaitForWritableTimeandtransferToTime.voidtransferToFully(FileChannel fileCh, long position, int count, LongWritable waitForWritableTime, LongWritable transferToTime) Transfers data from FileChannel usingFileChannel.transferTo(long, long, WritableByteChannel).voidwaits for the underlying channel to be ready for writing.voidwrite(byte[] b, int off, int len) voidwrite(int b) intwrite(ByteBuffer src) Methods inherited from class java.io.OutputStream
flush, nullOutputStream, write
-
Constructor Details
-
SocketOutputStream
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 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.
-
SocketOutputStream
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- raised on errors performing I/O.- See Also:
-
-
Method Details
-
write
- Specified by:
writein classOutputStream- Throws:
IOException
-
write
- Overrides:
writein classOutputStream- Throws:
IOException
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceChannel- Specified by:
closein interfaceCloseable- Overrides:
closein classOutputStream- Throws:
IOException
-
getChannel
- Returns:
- 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() -
write
- Specified by:
writein interfaceWritableByteChannel- Throws:
IOException
-
waitForWritable
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, LongWritable waitForWritableTime, LongWritable transferToTime) throws IOException Transfers data from FileChannel usingFileChannel.transferTo(long, long, WritableByteChannel). UpdateswaitForWritableTimeandtransferToTimewith the time spent blocked on the network and the time spent transferring data from disk to network respectively. 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 beginscount- number of bytes to transfer.waitForWritableTime- nanoseconds spent waiting for the socket to become writabletransferToTime- nanoseconds spent transferring data- 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 byFileChannel.transferTo(long, long, WritableByteChannel).
-
transferToFully
CalltransferToFully(FileChannel, long, int, LongWritable, LongWritable)with nullwaitForWritableTimeandtransferToTime.- Parameters:
fileCh- input fileCh.position- input position.count- input count.- Throws:
IOException- raised on errors performing I/O.
-
setTimeout
public void setTimeout(int timeoutMs)
-