/** * Returns OutputStream for the socket. If the socket has an associated * SocketChannel then it returns a * {@link SocketOutputStream} with the given timeout. If the socket does not * have a channel, {@link Socket#getOutputStream()} is returned. In the later * case, the timeout argument is ignored and the write will wait until * data is available.<br><br> * * Any socket created using socket factories returned by {@link NetUtils}, * must use this interface instead of {@link Socket#getOutputStream()}. * * @see Socket#getChannel() * * @param socket * @param timeout timeout in milliseconds. This may not always apply. zero * for waiting as long as necessary. * @return OutputStream for writing to the socket. * @throws IOException */ public static OutputStream getOutputStream(Socket socket, long timeout) throws IOException { return (socket.getChannel() == null) ? socket.getOutputStream() : new SocketOutputStream(socket, timeout); }
/** * Call * {@link #transferToFully(FileChannel, long, int, MutableRate, MutableRate)} * with null <code>waitForWritableTime</code> and <code>transferToTime</code> */ public void transferToFully(FileChannel fileCh, long position, int count) throws IOException { transferToFully(fileCh, position, count, null, null); }
@Override public void write(int b) throws IOException { /* If we need to, we can optimize this allocation. * probably no need to optimize or encourage single byte writes. */ byte[] buf = new byte[1]; buf[0] = (byte)b; write(buf, 0, 1); }
waitForWritable(); long wait = System.nanoTime(); int nTransfered = (int) fileCh.transferTo(position, count, getChannel());
sockOut.write(buf, 0, dataOff); sockOut.transferToFully(((FileInputStream)blockIn).getChannel(), blockInPosition, len);
@Override public void close() throws IOException { // We always close the outermost streams-- in this case, 'in' and 'out' // Closing either one of these will also close the Socket. try { in.close(); } finally { out.close(); } }
@Override public void setWriteTimeout(int timeoutMs) throws IOException { out.setTimeout(timeoutMs); }
waitForWritable(); int nTransfered = (int) fileCh.transferTo(position, count, getChannel());
buf, checksumOff, dataOff, len, checksum ); sockOut.write(buf, 0, dataOff + len); } else { sockOut.write(buf, 0, dataOff); sockOut.transferToFully(fileChannel, blockInPosition, len);
@Override public void close() throws IOException { // We always close the outermost streams-- in this case, 'in' and 'out' // Closing either one of these will also close the Socket. try { in.close(); } finally { out.close(); } }
@Override public void setWriteTimeout(int timeoutMs) throws IOException { out.setTimeout(timeoutMs); }
/** * Returns OutputStream for the socket. If the socket has an associated * SocketChannel then it returns a * {@link SocketOutputStream} with the given timeout. If the socket does not * have a channel, {@link Socket#getOutputStream()} is returned. In the later * case, the timeout argument is ignored and the write will wait until * data is available.<br><br> * * Any socket created using socket factories returned by {@link NetUtils}, * must use this interface instead of {@link Socket#getOutputStream()}. * * @see Socket#getChannel() * * @param socket * @param timeout timeout in milliseconds. This may not always apply. zero * for waiting as long as necessary. * @return OutputStream for writing to the socket. * @throws IOException */ public static OutputStream getOutputStream(Socket socket, long timeout) throws IOException { return (socket.getChannel() == null) ? socket.getOutputStream() : new SocketOutputStream(socket, timeout); }
waitForWritable(); int nTransfered = (int) fileCh.transferTo(position, count, getChannel());
SocketOutputStream sockOut = (SocketOutputStream)out; sockOut.write(buf, headerOff, dataOff - headerOff); LongWritable waitTime = new LongWritable(); LongWritable transferTime = new LongWritable(); sockOut.transferToFully(fileCh, blockInPosition, dataLen, waitTime, transferTime); datanode.metrics.addSendDataPacketBlockedOnNetworkNanos(waitTime.get());
@Override public void write(byte[] b, int off, int len) throws IOException { ByteBuffer buf = ByteBuffer.wrap(b, off, len); while (buf.hasRemaining()) { try { if (write(buf) < 0) { throw new IOException("The stream is closed"); } } catch (IOException e) { /* Unlike read, write can not inform user of partial writes. * So will close this if there was a partial write. */ if (buf.capacity() > buf.remaining()) { writer.close(); } throw e; } } }
/** * Transfer data from a FileChannel to a SocketOutputStream. * * @param volume target volume. null if unavailable. * @param sockOut SocketOutputStream to write the data. * @param fileCh FileChannel from which to read data. * @param position position within the channel where the transfer begins. * @param count number of bytes to transfer. * @param waitTime returns the nanoseconds spent waiting for the socket * to become writable. * @param transferTime returns the nanoseconds spent transferring data. * @throws IOException */ public void transferToSocketFully( @Nullable FsVolumeSpi volume, SocketOutputStream sockOut, FileChannel fileCh, long position, int count, LongWritable waitTime, LongWritable transferTime) throws IOException { final long begin = profilingEventHook.beforeFileIo(volume, TRANSFER, count); try { faultInjectorEventHook.beforeFileIo(volume, TRANSFER, count); sockOut.transferToFully(fileCh, position, count, waitTime, transferTime); profilingEventHook.afterFileIo(volume, TRANSFER, begin, count); } catch (Exception e) { onFailure(volume, begin); throw e; } }
@Override public void close() throws IOException { // We always close the outermost streams-- in this case, 'in' and 'out' // Closing either one of these will also close the Socket. try { in.close(); } finally { out.close(); } }
@Override public void setWriteTimeout(int timeoutMs) throws IOException { out.setTimeout(timeoutMs); }
/** * Returns OutputStream for the socket. If the socket has an associated * SocketChannel then it returns a * {@link SocketOutputStream} with the given timeout. If the socket does not * have a channel, {@link Socket#getOutputStream()} is returned. In the later * case, the timeout argument is ignored and the write will wait until * data is available.<br><br> * * Any socket created using socket factories returned by {@link NetUtils}, * must use this interface instead of {@link Socket#getOutputStream()}. * * @see Socket#getChannel() * * @param socket * @param timeout timeout in milliseconds. This may not always apply. zero * for waiting as long as necessary. * @return OutputStream for writing to the socket. * @throws IOException */ public static OutputStream getOutputStream(Socket socket, long timeout) throws IOException { return (socket.getChannel() == null) ? socket.getOutputStream() : new SocketOutputStream(socket, timeout); }
waitForWritable(); long wait = System.nanoTime(); int nTransfered = (int) fileCh.transferTo(position, count, getChannel());