@Test public void testAddressIpv4() throws Exception { InetSocketAddress inetAddress = new InetSocketAddress(NetUtil.LOCALHOST4, 9999); byte[] bytes = new byte[8]; ByteBuffer buffer = ByteBuffer.wrap(bytes); buffer.put(inetAddress.getAddress().getAddress()); buffer.putInt(inetAddress.getPort()); Assert.assertEquals(inetAddress, Native.address(buffer.array(), 0, bytes.length)); }
static InetSocketAddress address(byte[] addr, int offset, int len) { final int port = decodeInt(addr, offset + len - 4); final InetAddress address; try { switch (len) { case 8: byte[] ipv4 = new byte[4]; System.arraycopy(addr, offset, ipv4, 0, 4); address = InetAddress.getByAddress(ipv4); break; case 24: byte[] ipv6 = new byte[16]; System.arraycopy(addr, offset, ipv6, 0, 16); int scopeId = decodeInt(addr, offset + len - 8); address = Inet6Address.getByAddress(null, ipv6, scopeId); break; default: throw new Error(); } return new InetSocketAddress(address, port); } catch (UnknownHostException e) { throw new Error("Should never happen", e); } }
Native { static InetSocketAddress address(byte[] addr, int offset, int len) { final int port = decodeInt(addr, offset + len - 4); final InetAddress address; try { switch (len) { case 8: byte[] ipv4 = new byte[4]; System.arraycopy(addr, offset, ipv4, 0, 4); address = InetAddress.getByAddress(ipv4); break; case 24: byte[] ipv6 = new byte[16]; System.arraycopy(addr, offset, ipv6, 0, 16); int scopeId = decodeInt(addr, offset + len - 8); address = Inet6Address.getByAddress(null, ipv6, scopeId); break; default: throw new Error(); } return new InetSocketAddress(address, port); } catch (UnknownHostException e) { throw new Error("Should never happen", e); } } }
Native { static InetSocketAddress address(byte[] addr, int offset, int len) { final int port = decodeInt(addr, offset + len - 4); final InetAddress address; try { switch (len) { case 8: byte[] ipv4 = new byte[4]; System.arraycopy(addr, offset, ipv4, 0, 4); address = InetAddress.getByAddress(ipv4); break; case 24: byte[] ipv6 = new byte[16]; System.arraycopy(addr, offset, ipv6, 0, 16); int scopeId = decodeInt(addr, offset + len - 8); address = Inet6Address.getByAddress(null, ipv6, scopeId); break; default: throw new Error(); } return new InetSocketAddress(address, port); } catch (UnknownHostException e) { throw new Error("Should never happen", e); } } private  Native(); }
Native { static InetSocketAddress address(byte[] addr, int offset, int len) { final int port = decodeInt(addr, offset + len - 4); final InetAddress address; try { switch (len) { case 8: byte[] ipv4 = new byte[4]; System.arraycopy(addr, offset, ipv4, 0, 4); address = InetAddress.getByAddress(ipv4); break; case 24: byte[] ipv6 = new byte[16]; System.arraycopy(addr, offset, ipv6, 0, 16); int scopeId = decodeInt(addr, offset + len - 8); address = Inet6Address.getByAddress(null, ipv6, scopeId); break; default: throw new Error(); } return new InetSocketAddress(address, port); } catch (UnknownHostException e) { throw new Error("Should never happen", e); } } private  Native(); static native int eventFd(); static native void eventFdWrite(int fd, long value); static native void eventFdRead(int fd); static native int epollCreate(); static int epollWait(int efd, EpollEventArray events, int timeout); static native void epollCtlAdd(int efd, final int fd, final int flags); static native void epollCtlMod(int efd, final int fd, final int flags); static native void epollCtlDel(int efd, final int fd); static void close(int fd); static int write(int fd, ByteBuffer buf, int pos, int limit); static int writeAddress(int fd, long address, int pos, int limit); static long writev(int fd, ByteBuffer[] buffers, int offset, int length); static long writevAddresses(int fd, long memoryAddress, int length); static int read(int fd, ByteBuffer buf, int pos, int limit); static int readAddress(int fd, long address, int pos, int limit); static long sendfile(
            int dest, DefaultFileRegion src, long baseOffset, long offset, long length); static int sendTo(
            int fd, ByteBuffer buf, int pos, int limit, InetAddress addr, int port); static int sendToAddress(
            int fd, long memoryAddress, int pos, int limit, InetAddress addr, int port); static int sendToAddresses(
            int fd, long memoryAddress, int length, InetAddress addr, int port); static native EpollDatagramChannel.DatagramSocketAddress recvFrom(
            int fd, ByteBuffer buf, int pos, int limit); static native EpollDatagramChannel.DatagramSocketAddress recvFromAddress(
            int fd, long memoryAddress, int pos, int limit); static int sendmmsg(
            int fd, NativeDatagramPacketArray.NativeDatagramPacket[] msgs, int offset, int len); static int socketStreamFd(); static int socketDgramFd(); static int socketDomainFd(); static void bind(int fd, SocketAddress socketAddress); static void listen(int fd, int backlog); static boolean connect(int fd, SocketAddress socketAddress); static boolean finishConnect(int fd); static InetSocketAddress remoteAddress(int fd); static InetSocketAddress localAddress(int fd); static int accept(int fd, byte[] addr); static int recvFd(int fd); static int sendFd(int socketFd, int fd); static void shutdown(int fd, boolean read, boolean write); static native int getReceiveBufferSize(int fd); static native int getSendBufferSize(int fd); static native int isKeepAlive(int fd); static native int isReuseAddress(int fd); static native int isReusePort(int fd); static native int isTcpNoDelay(int fd); static native int isTcpCork(int fd); static native int getSoLinger(int fd); static native int getTrafficClass(int fd); static native int isBroadcast(int fd); static native int getTcpKeepIdle(int fd); static native int getTcpKeepIntvl(int fd); static native int getTcpKeepCnt(int fd); static native int getSoError(int fd); static native void setKeepAlive(int fd, int keepAlive); static native void setReceiveBufferSize(int fd, int receiveBufferSize); static native void setReuseAddress(int fd, int reuseAddress); static native void setReusePort(int fd, int reuseAddress); static native void setSendBufferSize(int fd, int sendBufferSize); static native void setTcpNoDelay(int fd, int tcpNoDelay); static native void setTcpCork(int fd, int tcpCork); static native void setSoLinger(int fd, int soLinger); static native void setTrafficClass(int fd, int tcpNoDelay); static native void setBroadcast(int fd, int broadcast); static native void setTcpKeepIdle(int fd, int seconds); static native void setTcpKeepIntvl(int fd, int seconds); static native void setTcpKeepCnt(int fd, int probes); static void tcpInfo(int fd, EpollTcpInfo info); static native String kernelVersion(); static native int sizeofEpollEvent(); static native int offsetofEpollData(); }
Native { static InetSocketAddress address(byte[] addr, int offset, int len) { final int port = decodeInt(addr, offset + len - 4); final InetAddress address; try { switch (len) { case 8: byte[] ipv4 = new byte[4]; System.arraycopy(addr, offset, ipv4, 0, 4); address = InetAddress.getByAddress(ipv4); break; case 24: byte[] ipv6 = new byte[16]; System.arraycopy(addr, offset, ipv6, 0, 16); int scopeId = decodeInt(addr, offset + len - 8); address = Inet6Address.getByAddress(null, ipv6, scopeId); break; default: throw new Error(); } return new InetSocketAddress(address, port); } catch (UnknownHostException e) { throw new Error("Should never happen", e); } } private  Native(); static native int eventFd(); static native void eventFdWrite(int fd, long value); static native void eventFdRead(int fd); static native int epollCreate(); static int epollWait(int efd, EpollEventArray events, int timeout); static native void epollCtlAdd(int efd, final int fd, final int flags); static native void epollCtlMod(int efd, final int fd, final int flags); static native void epollCtlDel(int efd, final int fd); static void close(int fd); static int write(int fd, ByteBuffer buf, int pos, int limit); static int writeAddress(int fd, long address, int pos, int limit); static long writev(int fd, ByteBuffer[] buffers, int offset, int length); static long writevAddresses(int fd, long memoryAddress, int length); static int read(int fd, ByteBuffer buf, int pos, int limit); static int readAddress(int fd, long address, int pos, int limit); static long sendfile(
            int dest, DefaultFileRegion src, long baseOffset, long offset, long length); static int sendTo(
            int fd, ByteBuffer buf, int pos, int limit, InetAddress addr, int port); static int sendToAddress(
            int fd, long memoryAddress, int pos, int limit, InetAddress addr, int port); static int sendToAddresses(
            int fd, long memoryAddress, int length, InetAddress addr, int port); static native EpollDatagramChannel.DatagramSocketAddress recvFrom(
            int fd, ByteBuffer buf, int pos, int limit); static native EpollDatagramChannel.DatagramSocketAddress recvFromAddress(
            int fd, long memoryAddress, int pos, int limit); static int sendmmsg(
            int fd, NativeDatagramPacketArray.NativeDatagramPacket[] msgs, int offset, int len); static int socketStreamFd(); static int socketDgramFd(); static int socketDomainFd(); static void bind(int fd, SocketAddress socketAddress); static void listen(int fd, int backlog); static boolean connect(int fd, SocketAddress socketAddress); static boolean finishConnect(int fd); static InetSocketAddress remoteAddress(int fd); static InetSocketAddress localAddress(int fd); static int accept(int fd, byte[] addr); static int recvFd(int fd); static int sendFd(int socketFd, int fd); static void shutdown(int fd, boolean read, boolean write); static native int getReceiveBufferSize(int fd); static native int getSendBufferSize(int fd); static native int isKeepAlive(int fd); static native int isReuseAddress(int fd); static native int isReusePort(int fd); static native int isTcpNoDelay(int fd); static native int isTcpCork(int fd); static native int getSoLinger(int fd); static native int getTrafficClass(int fd); static native int isBroadcast(int fd); static native int getTcpKeepIdle(int fd); static native int getTcpKeepIntvl(int fd); static native int getTcpKeepCnt(int fd); static native int getSoError(int fd); static native void setKeepAlive(int fd, int keepAlive); static native void setReceiveBufferSize(int fd, int receiveBufferSize); static native void setReuseAddress(int fd, int reuseAddress); static native void setReusePort(int fd, int reuseAddress); static native void setSendBufferSize(int fd, int sendBufferSize); static native void setTcpNoDelay(int fd, int tcpNoDelay); static native void setTcpCork(int fd, int tcpCork); static native void setSoLinger(int fd, int soLinger); static native void setTrafficClass(int fd, int tcpNoDelay); static native void setBroadcast(int fd, int broadcast); static native void setTcpKeepIdle(int fd, int seconds); static native void setTcpKeepIntvl(int fd, int seconds); static native void setTcpKeepCnt(int fd, int probes); static void tcpInfo(int fd, EpollTcpInfo info); static native String kernelVersion(); static native int sizeofEpollEvent(); static native int offsetofEpollData(); static final int EPOLLIN; static final int EPOLLOUT; static final int EPOLLRDHUP; static final int EPOLLET; static final int IOV_MAX; static final int UIO_MAX_IOV; static final boolean IS_SUPPORTING_SENDMMSG; }