java.nio.channels.SocketChannel.write()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(7.0k)|赞(0)|评价(0)|浏览(366)

本文整理了Java中java.nio.channels.SocketChannel.write()方法的一些代码示例,展示了SocketChannel.write()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。SocketChannel.write()方法的具体详情如下:
包路径:java.nio.channels.SocketChannel
类名称:SocketChannel
方法名:write

SocketChannel.write介绍

[英]Writes bytes from all the given byte buffers to this socket channel.

Calling this method is equivalent to calling write(sources, 0,
[中]将所有给定字节缓冲区中的字节写入此套接字通道。
调用此方法相当于调用write(sources,0,

代码示例

代码示例来源:origin: apache/kafka

/**
* Writes a sequence of bytes to this channel from the given buffer.
*
* @param src The buffer from which bytes are to be retrieved
* @return The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream
* @throws IOException If some other I/O error occurs
*/
@Override
public int write(ByteBuffer src) throws IOException {
  return socketChannel.write(src);
}

代码示例来源:origin: apache/kafka

/**
* Writes a sequence of bytes to this channel from the given buffer.
*
* @param srcs The buffer from which bytes are to be retrieved
* @return The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream
* @throws IOException If some other I/O error occurs
*/
@Override
public long write(ByteBuffer[] srcs) throws IOException {
  return socketChannel.write(srcs);
}

代码示例来源:origin: iluwatar/java-design-patterns

/**
  * Writes the pending {@link ByteBuffer} to the underlying channel sending data to the intended
  * receiver of the packet.
  */
 @Override
 protected void doWrite(Object pendingWrite, SelectionKey key) throws IOException {
  ByteBuffer pendingBuffer = (ByteBuffer) pendingWrite;
  ((SocketChannel) key.channel()).write(pendingBuffer);
 }
}

代码示例来源:origin: apache/kafka

/**
* Writes a sequence of bytes to this channel from the subsequence of the given buffers.
*
* @param srcs The buffers from which bytes are to be retrieved
* @param offset The offset within the buffer array of the first buffer from which bytes are to be retrieved; must be non-negative and no larger than srcs.length.
* @param length - The maximum number of buffers to be accessed; must be non-negative and no larger than srcs.length - offset.
* @return returns no.of bytes written , possibly zero.
* @throws IOException If some other I/O error occurs
*/
@Override
public long write(ByteBuffer[] srcs, int offset, int length) throws IOException {
  return socketChannel.write(srcs, offset, length);
}

代码示例来源:origin: apache/kafka

/**
* Flushes the buffer to the network, non blocking.
* Visible for testing.
* @param buf ByteBuffer
* @return boolean true if the buffer has been emptied out, false otherwise
* @throws IOException
*/
protected boolean flush(ByteBuffer buf) throws IOException {
  int remaining = buf.remaining();
  if (remaining > 0) {
    int written = socketChannel.write(buf);
    return written >= remaining;
  }
  return true;
}

代码示例来源:origin: square/okhttp

@Override public int write(ByteBuffer src) throws IOException {
 return getChannel().write(src);
}

代码示例来源:origin: oldmanpushcart/greys-anatomy

private void writeToSocketChannel(SocketChannel socketChannel, ByteBuffer buffer) throws IOException {
  while (buffer.hasRemaining()) {
    socketChannel.write(buffer);
  }
}

代码示例来源:origin: wildfly/wildfly

public void send(Object dest, byte[] buf, int offset, int length) throws Exception {
  ByteBuffer sbuf=ByteBuffer.wrap(buf, offset, length);
  client_channel.write(sbuf);
}

代码示例来源:origin: apache/zookeeper

@Override
  void sendPacket(Packet p) throws IOException {
    SocketChannel sock = (SocketChannel) sockKey.channel();
    if (sock == null) {
      throw new IOException("Socket is null!");
    }
    p.createBB();
    ByteBuffer pbb = p.bb;
    sock.write(pbb);
  }
}

代码示例来源:origin: apache/incubator-gobblin

private void write() throws IOException {
 while (this.proxy.write(this.buffer) > 0) {}
 if (this.buffer.remaining() == 0) {
  this.proxy.register(this.selector, SelectionKey.OP_READ, this);
  this.state = HandlerState.READING;
  this.buffer = ByteBuffer.allocate(1024);
 }
}

代码示例来源:origin: oldmanpushcart/greys-anatomy

private void write(SocketChannel socketChannel, ByteBuffer buffer) throws IOException {
  while (buffer.hasRemaining()
      && socketChannel.isConnected()) {
    if (-1 == socketChannel.write(buffer)) {
      // socket broken
      throw new IOException("write EOF");
    }
  }
}

代码示例来源:origin: wildfly/wildfly

public int write(final ByteBuffer src) throws IOException {
  int res = socketChannel.write(src);
  checkWriteTimeout(res > 0);
  return res;
}

代码示例来源:origin: apache/ignite

/**
 * Copies data from out net buffer and passes it to the underlying chain.
 *
 * @throws GridNioException If send failed.
 */
private void writeNetBuffer() throws IgniteCheckedException {
  try {
    ch.write(outNetBuf);
  }
  catch (IOException e) {
    throw new IgniteCheckedException("Failed to write byte to socket.", e);
  }
}

代码示例来源:origin: apache/rocketmq

private boolean reportSlaveMaxOffset(final long maxOffset) {
  this.reportOffset.position(0);
  this.reportOffset.limit(8);
  this.reportOffset.putLong(maxOffset);
  this.reportOffset.position(0);
  this.reportOffset.limit(8);
  for (int i = 0; i < 3 && this.reportOffset.hasRemaining(); i++) {
    try {
      this.socketChannel.write(this.reportOffset);
    } catch (IOException e) {
      log.error(this.getServiceName()
        + "reportSlaveMaxOffset this.socketChannel.write exception", e);
      return false;
    }
  }
  return !this.reportOffset.hasRemaining();
}

代码示例来源:origin: TooTallNate/Java-WebSocket

public void close() throws IOException {
  sslEngine.closeOutbound();
  sslEngine.getSession().invalidate();
  if( socketChannel.isOpen() )
    socketChannel.write( wrap( emptybuffer ) );// FIXME what if not all bytes can be written
  socketChannel.close();
}

代码示例来源:origin: TooTallNate/Java-WebSocket

public SSLSocketChannel2( SocketChannel channel , SSLEngine sslEngine , ExecutorService exec , SelectionKey key ) throws IOException {
  if( channel == null || sslEngine == null || exec == null )
    throw new IllegalArgumentException( "parameter must not be null" );
  this.socketChannel = channel;
  this.sslEngine = sslEngine;
  this.exec = exec;
  readEngineResult = writeEngineResult = new SSLEngineResult( Status.BUFFER_UNDERFLOW, sslEngine.getHandshakeStatus(), 0, 0 ); // init to prevent NPEs
  tasks = new ArrayList<Future<?>>( 3 );
  if( key != null ) {
    key.interestOps( key.interestOps() | SelectionKey.OP_WRITE );
    this.selectionKey = key;
  }
  createBuffers( sslEngine.getSession() );
  // kick off handshake
  socketChannel.write( wrap( emptybuffer ) );// initializes res
  processHandshake();
}

代码示例来源:origin: TooTallNate/Java-WebSocket

public int write( ByteBuffer src ) throws IOException {
  if( !isHandShakeComplete() ) {
    processHandshake();
    return 0;
  }
  // assert ( bufferallocations > 1 ); //see #190
  //if( bufferallocations <= 1 ) {
  //	createBuffers( sslEngine.getSession() );
  //}
  int num = socketChannel.write( wrap( src ) );
  if (writeEngineResult.getStatus() == SSLEngineResult.Status.CLOSED) {
    throw new EOFException("Connection is closed");
  }
  return num;
}

代码示例来源:origin: AsyncHttpClient/async-http-client

void newRemoteData() throws IOException {
 ByteBuffer buf = ByteBuffer.allocate(1024);
 if (remote.read(buf) == -1)
  throw new IOException("disconnected");
 lastData = System.currentTimeMillis();
 buf.flip();
 client.write(buf);
}

代码示例来源:origin: wildfly/wildfly

public long write(final ByteBuffer[] srcs, final int offset, final int length) throws IOException {
  if (length == 1) {
    return write(srcs[offset]);
  }
  long res = socketChannel.write(srcs, offset, length);
  checkWriteTimeout(res > 0L);
  return res;
}

代码示例来源:origin: robovm/robovm

@Override
public void write(byte[] buffer, int offset, int byteCount) throws IOException {
  Arrays.checkOffsetAndCount(buffer.length, offset, byteCount);
  ByteBuffer buf = ByteBuffer.wrap(buffer, offset, byteCount);
  if (!channel.isBlocking()) {
    throw new IllegalBlockingModeException();
  }
  channel.write(buf);
}

相关文章