Возможно, вас заинтересуют сокетные каналы, если вас интересует отсутствие блокировки.Каналы можно найти в пакете java.nio.В частности, вас может заинтересовать интерфейс ReadableByteChannel
и классы, которые его реализуют.
Вы будете использовать каналы примерно так.
SocketChannel channel = SocketChannel.open(new InetSocketAddress("127.0.0.1",8000));
ByteBuffer buffer = ByteBuffer.allocate(1024);
while(channel.read(buffer) != -1) {
// if -1 is returned then stream has been closed and loop should exit
if (buffer.remaining() == 0) {
// buffer is full, you might want to consume some of the data in buffer
// or allocate a larger buffer before continuing
}
// we have now just read as much was available on the socket channel. Any
// immediate attempts to read from the channel again will result in the
// read method returning immediately.
// Hence try to do something useful with the data before reading again
}
// channel is now closed