All Downloads are FREE. Search and download functionalities are using the official Maven repository.

java.nio.channels.AsynchronousSocketChannel Maven / Gradle / Ivy

Go to download

JVM AOT compiler currently generating JavaScript, C++, Haxe, with initial focus on Kotlin and games.

There is a newer version: 0.6.8
Show newest version
package java.nio.channels;

import com.jtransc.annotation.JTranscMethodBody;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.SocketOption;
import java.nio.ByteBuffer;
import java.nio.channels.spi.AsynchronousChannelProvider;
import java.util.Set;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

public class AsynchronousSocketChannel implements AsynchronousByteChannel, NetworkChannel {
	protected AsynchronousSocketChannel(AsynchronousChannelProvider provider) {
	}

	public final AsynchronousChannelProvider provider() {
		return null;
	}

	public static AsynchronousSocketChannel open(AsynchronousChannelGroup group) throws IOException {
		return new AsynchronousSocketChannel();
	}

	public static AsynchronousSocketChannel open()
		throws IOException {
		return open(null);
	}

	AsynchronousSocketChannel() {
	}

	@Override
	native public AsynchronousSocketChannel bind(SocketAddress local) throws IOException;

	@Override
	native public  AsynchronousSocketChannel setOption(SocketOption name, T value) throws IOException;

	@Override
	native public  T getOption(SocketOption name) throws IOException;

	@Override
	native public Set> supportedOptions();

	native public AsynchronousSocketChannel shutdownInput() throws IOException;

	native public AsynchronousSocketChannel shutdownOutput() throws IOException;

	native public SocketAddress getRemoteAddress() throws IOException;

	@JTranscMethodBody(target = "js", value = {
		"var address = N.istr(p0), port = p1, attachment = p2, handler = p3;",
		"var net = require('net');",
		"this.client = new net.Socket();",
		"this.client.on('error', function() {",
		"	handler{% IMETHOD java.nio.channels.CompletionHandler:failed %}(N.createRuntimeException('error'), attachment);",
		"});",
		"this.client.connect(port, address, function() {",
		"	handler{% IMETHOD java.nio.channels.CompletionHandler:completed %}(null, attachment);",
		"});",
	})
	native private  void _connect(String address, int port, A attachment, CompletionHandler handler);

	public  void connect(SocketAddress remote, A attachment, CompletionHandler handler) {
		InetSocketAddress address = (InetSocketAddress) remote;
		_connect(address.getHostName(), address.getPort(), attachment, handler);
	}

	native public Future connect(SocketAddress remote);

	@Override
	public final  void read(ByteBuffer dst, A attachment, CompletionHandler handler) {
		read(dst, 0L, TimeUnit.MILLISECONDS, attachment, handler);
	}

	@Override
	native public Future read(ByteBuffer dst);

	native private  void _read(byte[] data, int offset, int len, double timeout, A attachment, CompletionHandler handler);

	@JTranscMethodBody(target = "js", value = {
		"var data = p0.data, offset = p1, len = p2, timeout = p3, attachment = p4, handler = p5;",
		"this.client.write(new Buffer(new Int8Array(data.buffer, offset, len)), function() {",
		"	handler{% IMETHOD java.nio.channels.CompletionHandler:completed %}(N.boxInt(len), attachment);",
		"});",
	})
	native private  void _write(byte[] data, int offset, int len, double timeout, A attachment, CompletionHandler handler);

	native public  void read(ByteBuffer[] dsts, int offset, int length, long timeout, TimeUnit unit, A attachment, CompletionHandler handler);

	public  void read(ByteBuffer dst, long timeout, TimeUnit unit, A attachment, CompletionHandler handler) {
		_read(dst.array(), dst.arrayOffset(), dst.limit(), (double) unit.toMillis(timeout), attachment, handler);
	}

	public  void write(ByteBuffer src, long timeout, TimeUnit unit, A attachment, CompletionHandler handler) {
		_write(src.array(), src.arrayOffset(), src.limit(), (double) unit.toMillis(timeout), attachment, handler);
	}

	@Override
	public final  void write(ByteBuffer src, A attachment, CompletionHandler handler) {
		write(src, 0L, TimeUnit.MILLISECONDS, attachment, handler);
	}

	@Override
	native public Future write(ByteBuffer src);

	native public  void write(ByteBuffer[] srcs, int offset, int length, long timeout, TimeUnit unit, A attachment, CompletionHandler handler);

	native public SocketAddress getLocalAddress() throws IOException;

	@Override
	native public boolean isOpen();

	@Override
	@JTranscMethodBody(target = "js", value = {
		"this.client.end()",
	})
	native public void close() throws IOException;
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy