read SocketChannel and return Object - Java java.nio.channels

Java examples for java.nio.channels:SocketChannel

Description

read SocketChannel and return Object

Demo Code


import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class Main{
    /* w ww  .  j  a  v  a 2 s .  c  o  m*/
    public static Object read(SocketChannel channel) {
        try {
            ByteBuffer buffer = ByteBuffer.allocate(102400);
            channel.read(buffer);
            buffer.flip();

            byte[] data = buffer.array();
            return ByteUtil.getObject(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

Related Tutorials