Java 8 AsynchronousSocketChannel - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 AsynchronousSocketChannel class.

This code example shows how to use the following methods:close, read, write
 
    private CompletionHandler getHandler(final ByteBuffer messageBuffer, final AsynchronousSocketChannel socketChannel) {
        return new CompletionHandler<AsynchronousSocketChannel, Object>() {
            @Override 
            public void completed(AsynchronousSocketChannel result, Object attachment) { 
                socketChannel.write(messageBuffer); 
                messageBuffer.clear(); 
                Future<Integer> bytesRead = socketChannel.read(messageBuffer); 
                try { 
                    int numBytes = bytesRead.get(); 
                    byte[] bytes = new byte[numBytes]; 
                    messageBuffer.flip(); 
                    messageBuffer.get(bytes, 0, numBytes); 
                    String reply = new String(bytes,Charset.forName("UTF-8")); 
                    System.out.println("Server Reply " + reply); 
                    socketChannel.close(); 
                } catch (Exception e) { 
                    e.printStackTrace(); 
                } 
            } 
 
6
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:read, isOpen
     * Enqueue a read 
     * @param completionHandler callback on completed read 
     */ 
    public void read(CompletionHandler<Integer, ? super ByteBuffer> completionHandler) {
        ByteBuffer input = ByteBuffer.allocate(256);
        if (!channel.isOpen()) {
            return; 
        } 
        channel.read(input, input, completionHandler);
    } 
 
    /** 
     * Closes the channel 
     */ 
    public void close() { 
        try { 
            channel.close();
        } catch (IOException e) {
            e.printStackTrace();
        } 
5
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:read, isOpen
     * Enqueue a read 
     * @param completionHandler callback on completed read 
     */ 
    public void read(CompletionHandler<Integer, ? super ByteBuffer> completionHandler) {
        ByteBuffer input = ByteBuffer.allocate(256);
        if (!channel.isOpen()) {
            return; 
        } 
        channel.read(input, input, completionHandler);
    } 
 
    /** 
     * Closes the channel 
     */ 
    public void close() { 
        try { 
            channel.close();
        } catch (IOException e) {
            e.printStackTrace();
        } 
5
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:close
    } 
 
    @Override 
    public void close() { 
        try { 
            socketChannel.close();
        } catch (IOException e) {
            // Ignore 
        } 
    } 
 
    @Override 
    public Future<Void> handshake() {
        return NOOP_FUTURE;
    } 
 
 
    private static final class NoOpFuture implements Future<Void> {
 
        @Override 
5
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:read, write
 
    private void writeEchoMessage(AsynchronousSocketChannel socket, String message) {
        ByteBuffer byteBuffer = getByteBuffer();
        byteBuffer.put(message.getBytes());
        byteBuffer.flip();
        socket.write(byteBuffer);
    } 
 
    private String readMessage(AsynchronousSocketChannel socket) {
        ByteBuffer byteBuffer = getByteBuffer();
        int totalBytes = 0;
        String message = null;
        //TODO use charset encoders here 
        //TODO reuse byteBuffers for lifecycle of entire call 
        try { 
            totalBytes = socket.read(byteBuffer).get();
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        } 
        byteBuffer.flip();
        if (totalBytes > 0) {
5
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:read
        System.out.println("EchoServer is done in " + stopwatch.elapsedMillis());
    } 
 
    private static void processConnection(AsynchronousSocketChannel socket) {
        ByteBuffer byteBuffer = ByteBuffer.allocate(1000);
        Future<Integer> read = socket.read(byteBuffer);
        int totalBytes = 0;
        try { 
            totalBytes = read.get();
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        } 
        //TODO use charset encoders here USE UTF8 
        byteBuffer.flip();
        byte[] bytes = new byte[totalBytes];
        if (bytes.length > 0) {
            byteBuffer.get(bytes, 0, totalBytes);
            String message = new String(bytes);
            System.out.println(Thread.currentThread().getName());
            System.out.println("Message " + message);
4
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:close
    } 
 
    @Override 
    public void close() { 
        try { 
            socketChannel.close();
        } catch (IOException e) {
            // Ignore 
        } 
    } 
 
    @Override 
    public Future<Void> handshake() {
        return NOOP_FUTURE;
    } 
 
 
    private static final class NoOpFuture implements Future<Void> {
 
        @Override 
3
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:setOption
	protected abstract FrontendConnection getConnection( 
			AsynchronousSocketChannel channel) throws IOException ;
 
	public FrontendConnection make(AsynchronousSocketChannel channel)
			throws IOException {
		channel.setOption(StandardSocketOptions.SO_RCVBUF, socketRecvBuffer);
		channel.setOption(StandardSocketOptions.SO_SNDBUF, socketSendBuffer);
		channel.setOption(StandardSocketOptions.TCP_NODELAY, true);
		channel.setOption(StandardSocketOptions.SO_KEEPALIVE, true);
 
		FrontendConnection c = getConnection(channel);
		c.setPacketHeaderSize(packetHeaderSize);
		c.setMaxPacketSize(maxPacketSize);
		c.setWriteQueue(new BufferQueue(writeQueueCapcity));
		c.setIdleTimeout(idleTimeout);
		c.setCharset(charset);
		return c;
	} 
 
	public int getSocketRecvBuffer() { 
4
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
See Code Examples for Java 8 AsynchronousSocketChannel Methods: