com.github.thorbenkuck.network.connection.SocketChannelDataConnection Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of easy-net Show documentation
Show all versions of easy-net Show documentation
An EventStream driven, simple to use Client/Server framework
The newest version!
package com.github.thorbenkuck.network.connection;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
class SocketChannelDataConnection implements DataConnection {
private final SocketChannel channel;
SocketChannelDataConnection(SocketChannel channel) {
this.channel = channel;
}
@Override
public byte[] read(int i) throws IOException {
ByteBuffer byteBuffer = ByteBuffer.allocate(i);
channel.read(byteBuffer);
byteBuffer.flip();
return byteBuffer.array();
}
@Override
public void write(byte[] data) throws IOException {
ByteBuffer wrap = ByteBuffer.wrap(data);
channel.write(wrap);
}
@Override
public void flush() throws IOException {
// This is not needed nor possible
}
@Override
public void close() throws IOException {
channel.shutdownInput()
.shutdownOutput()
.close();
}
@Override
public void closeSilent() {
try {
channel.shutdownInput();
} catch (IOException ignored) {
}
try {
channel.shutdownOutput();
} catch (IOException ignored) {
}
try {
channel.close();
} catch (IOException ignored) {
}
}
}