net.lenni0451.mcping.stream.SocketChannelInputStream Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of MCPing Show documentation
Show all versions of MCPing Show documentation
A simple library to ping minecraft servers
The newest version!
package net.lenni0451.mcping.stream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
public class SocketChannelInputStream extends InputStream {
private final SocketChannel socketChannel;
private final int readTimeout;
private final ByteBuffer buffer;
private final Selector selector;
public SocketChannelInputStream(final SocketChannel socketChannel, final int readTimeout) throws IOException {
this.socketChannel = socketChannel;
this.readTimeout = readTimeout;
this.buffer = ByteBuffer.allocateDirect(1);
this.selector = Selector.open();
this.socketChannel.register(this.selector, SelectionKey.OP_READ);
}
private void removeKey() {
Iterator it = this.selector.selectedKeys().iterator();
if (it.hasNext()) {
it.next();
it.remove();
}
}
@Override
public int read() throws IOException {
this.buffer.clear();
while (this.buffer.position() == 0) {
int numKeys = this.selector.select(this.readTimeout);
if (numKeys == 0) throw new IOException("Read timeout");
this.removeKey();
this.socketChannel.read(this.buffer);
}
this.buffer.flip();
return this.buffer.get() & 0xFF;
}
@Override
public void close() throws IOException {
super.close();
this.selector.close();
this.socketChannel.close();
}
}