java.net.Socket.getChannel()方法的使用及代码示例

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

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

Socket.getChannel介绍

[英]Returns this socket's SocketChannel, if one exists. A channel is available only if this socket wraps a channel. (That is, you can go from a channel to a socket and back again, but you can't go from an arbitrary socket to a channel.) In practice, this means that the socket must have been created by java.nio.channels.ServerSocketChannel#accept or java.nio.channels.SocketChannel#open.
[中]返回此套接字的SocketChannel(如果存在)。仅当此套接字包装通道时,通道才可用。(也就是说,您可以从一个通道转到一个插座,然后再返回,但不能从任意插座转到一个通道。)实际上,这意味着套接字必须是由java创建的。尼奥。频道。ServerSocketChannel#接受或java。尼奥。频道。袜子通道#打开。

代码示例

代码示例来源:origin: aws/aws-sdk-java

@Override
public SocketChannel getChannel() {
  return sock.getChannel();
}

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

SelectableChannel getSelectableChannel() {
 return this.theSocket.getChannel();
}

代码示例来源:origin: org.apache.hadoop/hadoop-common

/**
 * Same as SocketOutputStream(socket.getChannel(), timeout):<br><br>
 * 
 * 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.
 * 
 * @see SocketOutputStream#SocketOutputStream(WritableByteChannel, long)
 *  
 * @param socket should have a channel associated with it.
 * @param timeout timeout timeout in milliseconds. must not be negative.
 * @throws IOException
 */
public SocketOutputStream(Socket socket, long timeout) 
                    throws IOException {
 this(socket.getChannel(), timeout);
}

代码示例来源:origin: org.apache.hadoop/hadoop-common

/**
 * Same as SocketInputStream(socket.getChannel(), timeout): <br><br>
 * 
 * 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.
 * 
 * @see SocketInputStream#SocketInputStream(ReadableByteChannel, long)
 *  
 * @param socket should have a channel associated with it.
 * @param timeout timeout timeout in milliseconds. must not be negative.
 * @throws IOException
 */
public SocketInputStream(Socket socket, long timeout) 
                    throws IOException {
 this(socket.getChannel(), timeout);
}

代码示例来源:origin: org.apache.hadoop/hadoop-common

SocketInputWrapper(Socket s, InputStream is) {
 super(is);
 this.socket = s;
 this.hasChannel = s.getChannel() != null;
 if (hasChannel) {
  Preconditions.checkArgument(is instanceof SocketInputStream,
    "Expected a SocketInputStream when there is a channel. " +
    "Got: %s", is);
 }
}

代码示例来源:origin: org.apache.hadoop/hadoop-common

/**
 * Same as SocketInputStream(socket.getChannel(), socket.getSoTimeout())
 * :<br><br>
 * 
 * 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.
 * @see SocketInputStream#SocketInputStream(ReadableByteChannel, long)
 *  
 * @param socket should have a channel associated with it.
 * @throws IOException
 */
public SocketInputStream(Socket socket) throws IOException {
 this(socket.getChannel(), socket.getSoTimeout());
}

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

/**
 * See {@link Socket#getChannel()}. Calling this method does not trigger mode detection.
 */
@Override
public SocketChannel getChannel() {
  return getSocketAllowUnknownMode().getChannel();
}

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

public RequestReader(Socket socket, Protocol protocol) {
 buffer = ByteBuffer.allocate(getBufferSize(socket.getChannel()));
 // set position to limit so that first read attempt
 // returns hasRemaining() false
 buffer.position(buffer.limit());
 this.socket = socket;
 this.protocol = protocol;
}

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

public void setComms(Socket socket, InputStream is, OutputStream os, ByteBuffer bb,
  MessageStats msgStats) {
 Assert.assertTrue(socket != null);
 this.socket = socket;
 this.socketChannel = socket.getChannel();
 this.inputStream = is;
 this.outputStream = os;
 this.cachedCommBuffer = bb;
 this.messageStats = msgStats;
}

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

/**
 * Switch this connection to blocking mode so we can use oldIO to read and write messages.
 */
void makeBlocking() throws IOException {
 SelectableChannel c = this.theSocket.getChannel();
 c.configureBlocking(true);
}

代码示例来源:origin: igniterealtime/Openfire

public TLSStreamWriter(TLSWrapper tlsWrapper, Socket socket) throws IOException {
  wrapper = tlsWrapper;
  // DANIELE: Add code to use directly the socket channel
  if (socket.getChannel() != null) {
    wbc = ServerTrafficCounter.wrapWritableChannel(socket.getChannel());
  }
  else {
    wbc = Channels.newChannel(
        ServerTrafficCounter.wrapOutputStream(socket.getOutputStream()));
  }
  outAppData = ByteBuffer.allocate(tlsWrapper.getAppBuffSize());
}

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

public static ByteBuffer allocateCommBuffer(int size, Socket sock) {
 // I expect that size will almost always be the same value
 if (sock.getChannel() == null) {
  // The socket this commBuffer will be used for is old IO (it has no channel).
  // So the commBuffer should be heap based.
  return ByteBuffer.allocate(size);
 }
 LinkedBlockingQueue<ByteBuffer> q = commBufferMap.get(size);
 ByteBuffer result = null;
 if (q != null) {
  result = q.poll();
 }
 if (result == null) {
  result = ByteBuffer.allocateDirect(size);
 } else {
  result.position(0);
  result.limit(result.capacity());
 }
 return result;
}

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

private Command readAsciiCommand() throws IOException {
 SocketChannel channel = this.socket.getChannel();
 if (channel == null || !channel.isOpen()) {
  throw new IllegalStateException("cannot read from channel");
 }
 buffer.clear();
 int bytesRead = channel.read(buffer);
 if (bytesRead == -1) {
  throw new IOException("EOF");
 }
 buffer.flip();
 return Command.valueOf(readCommand(buffer));
}

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

void setComms(Socket socket, ByteBuffer bb, MessageStats msgStats) throws IOException {
 this.socketChannel = socket.getChannel();
 if (this.socketChannel == null) {
  setComms(socket, socket.getInputStream(), socket.getOutputStream(), bb, msgStats);
 } else {
  setComms(socket, null, null, bb, msgStats);
 }
}

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

public void sendException(Exception e) {
  SocketChannel channel = this.socket.getChannel();
  if (channel == null || !channel.isOpen()) {
   throw new IllegalStateException("cannot write to channel");
  }
  try {
   if (e instanceof ClientError) {
    channel.write(charsetASCII.encode(Reply.CLIENT_ERROR.toString()));
   } else {
    channel.write(charsetASCII.encode(Reply.ERROR.toString()));
   }
  } catch (IOException ex) {
  }
 }
}

代码示例来源:origin: igniterealtime/Openfire

public TLSStreamReader(TLSWrapper tlsWrapper, Socket socket) throws IOException {
  wrapper = tlsWrapper;
  // DANIELE: Add code to use directly the socket channel
  if (socket.getChannel() != null) {
    rbc = ServerTrafficCounter.wrapReadableChannel(socket.getChannel());
  }
  else {
    rbc = Channels.newChannel(
        ServerTrafficCounter.wrapInputStream(socket.getInputStream()));
  }
  inNetBB = ByteBuffer.allocate(wrapper.getNetBuffSize());
  inAppBB = ByteBuffer.allocate(wrapper.getAppBuffSize());
}

代码示例来源:origin: org.apache.hadoop/hadoop-common

/**
 * 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);            
}

代码示例来源:origin: spotify/docker-client

@Override
 public Socket connectSocket(final int connectTimeout,
               final Socket socket,
               final HttpHost host,
               final InetSocketAddress remoteAddress,
               final InetSocketAddress localAddress,
               final HttpContext context) throws IOException {
  if (!(socket instanceof UnixSocket)) {
   throw new AssertionError("Unexpected socket: " + socket);
  }

  socket.setSoTimeout(connectTimeout);
  try {
   socket.getChannel().connect(new UnixSocketAddress(socketFile));
  } catch (SocketTimeoutException e) {
   throw new ConnectTimeoutException(e, null, remoteAddress.getAddress());
  }
  return socket;
 }
}

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

public void sendReply(ByteBuffer reply) throws IOException {
 // for binary set the response opCode
 if (this.protocol == Protocol.BINARY) {
  reply.rewind();
  reply.put(POSITION_OPCODE, buffer.get(POSITION_OPCODE));
  reply.putInt(POSITION_OPAQUE, buffer.getInt(POSITION_OPAQUE));
  if (ConnectionHandler.getLogger().finerEnabled()) {
   ConnectionHandler.getLogger()
     .finer("sending reply:" + reply + " " + Command.buffertoString(reply));
  }
 }
 SocketChannel channel = this.socket.getChannel();
 if (channel == null || !channel.isOpen()) {
  throw new IllegalStateException("cannot write to channel");
 }
 channel.write(reply);
}

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

private ByteBuffer readAtLeast(int bytes) throws IOException {
 peerNetData = ioFilter.ensureWrappedCapacity(bytes, peerNetData,
   Buffers.BufferType.TRACKED_RECEIVER, getStats());
 return ioFilter.readAtLeast(conn.getSocket().getChannel(), bytes, peerNetData, getStats());
}

相关文章