mirror of
https://github.com/corda/corda.git
synced 2025-01-22 12:28:11 +00:00
update NIO code to be more compatible with Java
This commit is contained in:
parent
9cb21a29a6
commit
3a208edbbc
@ -10,7 +10,7 @@
|
||||
|
||||
package java.net;
|
||||
|
||||
public class InetSocketAddress {
|
||||
public class InetSocketAddress extends SocketAddress {
|
||||
private final String host;
|
||||
private final int port;
|
||||
|
||||
|
17
classpath/java/net/ServerSocket.java
Normal file
17
classpath/java/net/ServerSocket.java
Normal file
@ -0,0 +1,17 @@
|
||||
/* Copyright (c) 2008, Avian Contributors
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software
|
||||
for any purpose with or without fee is hereby granted, provided
|
||||
that the above copyright notice and this permission notice appear
|
||||
in all copies.
|
||||
|
||||
There is NO WARRANTY for this software. See license.txt for
|
||||
details. */
|
||||
|
||||
package java.net;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public abstract class ServerSocket {
|
||||
public abstract void bind(SocketAddress address) throws IOException;
|
||||
}
|
13
classpath/java/net/SocketAddress.java
Normal file
13
classpath/java/net/SocketAddress.java
Normal file
@ -0,0 +1,13 @@
|
||||
/* Copyright (c) 2008, Avian Contributors
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software
|
||||
for any purpose with or without fee is hereby granted, provided
|
||||
that the above copyright notice and this permission notice appear
|
||||
in all copies.
|
||||
|
||||
There is NO WARRANTY for this software. See license.txt for
|
||||
details. */
|
||||
|
||||
package java.net;
|
||||
|
||||
public abstract class SocketAddress { }
|
59
classpath/java/nio/Buffer.java
Normal file
59
classpath/java/nio/Buffer.java
Normal file
@ -0,0 +1,59 @@
|
||||
/* Copyright (c) 2008, Avian Contributors
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software
|
||||
for any purpose with or without fee is hereby granted, provided
|
||||
that the above copyright notice and this permission notice appear
|
||||
in all copies.
|
||||
|
||||
There is NO WARRANTY for this software. See license.txt for
|
||||
details. */
|
||||
|
||||
package java.nio;
|
||||
|
||||
public abstract class Buffer {
|
||||
protected int capacity;
|
||||
protected int position;
|
||||
protected int limit;
|
||||
|
||||
public int limit() {
|
||||
return limit;
|
||||
}
|
||||
|
||||
public int remaining() {
|
||||
return limit-position;
|
||||
}
|
||||
|
||||
public int position() {
|
||||
return position;
|
||||
}
|
||||
|
||||
public int capacity() {
|
||||
return capacity;
|
||||
}
|
||||
|
||||
public Buffer limit(int newLimit) {
|
||||
limit = newLimit;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Buffer position(int newPosition) {
|
||||
position = newPosition;
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean hasRemaining() {
|
||||
return remaining() > 0;
|
||||
}
|
||||
|
||||
public Buffer clear() {
|
||||
position = 0;
|
||||
limit = capacity;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Buffer flip() {
|
||||
limit = position;
|
||||
position = 0;
|
||||
return this;
|
||||
}
|
||||
}
|
@ -10,12 +10,9 @@
|
||||
|
||||
package java.nio;
|
||||
|
||||
public class ByteBuffer implements Comparable<ByteBuffer> {
|
||||
public class ByteBuffer extends Buffer implements Comparable<ByteBuffer> {
|
||||
private final byte[] array;
|
||||
private int arrayOffset;
|
||||
private int capacity;
|
||||
private int position;
|
||||
private int limit;
|
||||
private final boolean readOnly;
|
||||
|
||||
public static ByteBuffer allocate(int capacity) {
|
||||
@ -55,12 +52,6 @@ public class ByteBuffer implements Comparable<ByteBuffer> {
|
||||
return array;
|
||||
}
|
||||
|
||||
public ByteBuffer clear() {
|
||||
position = 0;
|
||||
limit = capacity;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ByteBuffer slice() {
|
||||
ByteBuffer buf = new ByteBuffer(array, true);
|
||||
buf.arrayOffset = arrayOffset + position;
|
||||
@ -70,22 +61,6 @@ public class ByteBuffer implements Comparable<ByteBuffer> {
|
||||
return buf;
|
||||
}
|
||||
|
||||
public int limit() {
|
||||
return limit;
|
||||
}
|
||||
|
||||
public int remaining() {
|
||||
return limit-position;
|
||||
}
|
||||
|
||||
public int position() {
|
||||
return position;
|
||||
}
|
||||
|
||||
public int capacity() {
|
||||
return capacity;
|
||||
}
|
||||
|
||||
public int arrayOffset() {
|
||||
return arrayOffset;
|
||||
}
|
||||
@ -100,16 +75,6 @@ public class ByteBuffer implements Comparable<ByteBuffer> {
|
||||
return this;
|
||||
}
|
||||
|
||||
public ByteBuffer limit(int newLimit) {
|
||||
limit = newLimit;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ByteBuffer position(int newPosition) {
|
||||
position = newPosition;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ByteBuffer put(byte val) {
|
||||
checkPut(1);
|
||||
array[arrayOffset+(position++)] = val;
|
||||
@ -164,10 +129,6 @@ public class ByteBuffer implements Comparable<ByteBuffer> {
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean hasRemaining() {
|
||||
return remaining() > 0;
|
||||
}
|
||||
|
||||
public byte get() {
|
||||
checkGet(1);
|
||||
return array[arrayOffset+(position++)];
|
||||
@ -189,12 +150,6 @@ public class ByteBuffer implements Comparable<ByteBuffer> {
|
||||
return array[arrayOffset+position];
|
||||
}
|
||||
|
||||
public ByteBuffer flip() {
|
||||
limit = position;
|
||||
position = 0;
|
||||
return this;
|
||||
}
|
||||
|
||||
public int getInt() {
|
||||
checkGet(4);
|
||||
int i = get() << 24;
|
||||
|
@ -16,9 +16,8 @@ import java.nio.ByteBuffer;
|
||||
public abstract class SelectableChannel implements Channel {
|
||||
private SelectionKey key;
|
||||
|
||||
public abstract int read(ByteBuffer b) throws Exception;
|
||||
public abstract int write(ByteBuffer b) throws Exception;
|
||||
public abstract boolean isOpen();
|
||||
public abstract SelectableChannel configureBlocking(boolean v)
|
||||
throws IOException;
|
||||
|
||||
public SelectionKey register(Selector selector, int interestOps,
|
||||
Object attachment)
|
||||
@ -29,6 +28,10 @@ public abstract class SelectableChannel implements Channel {
|
||||
return key;
|
||||
}
|
||||
|
||||
public boolean isOpen() {
|
||||
return key != null;
|
||||
}
|
||||
|
||||
public void close() throws IOException {
|
||||
if (key != null) {
|
||||
key.selector().remove(key);
|
||||
|
@ -42,7 +42,7 @@ public abstract class Selector {
|
||||
|
||||
public abstract void wakeup();
|
||||
|
||||
public abstract void select(long interval) throws IOException;
|
||||
public abstract int select(long interval) throws IOException;
|
||||
|
||||
public abstract void close();
|
||||
}
|
||||
|
@ -10,7 +10,11 @@
|
||||
|
||||
package java.nio.channels;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.SocketAddress;
|
||||
import java.net.ServerSocket;
|
||||
|
||||
public class ServerSocketChannel extends SocketChannel {
|
||||
public static ServerSocketChannel open() {
|
||||
@ -24,26 +28,32 @@ public class ServerSocketChannel extends SocketChannel {
|
||||
return c;
|
||||
}
|
||||
|
||||
public Handle socket() {
|
||||
public ServerSocket socket() {
|
||||
return new Handle();
|
||||
}
|
||||
|
||||
private int doAccept() throws Exception {
|
||||
private int doAccept() throws IOException {
|
||||
return natDoAccept(socket);
|
||||
}
|
||||
|
||||
private int doListen(String host, int port) throws Exception {
|
||||
private int doListen(String host, int port) throws IOException {
|
||||
return natDoListen(host, port);
|
||||
}
|
||||
|
||||
public class Handle {
|
||||
public void bind(InetSocketAddress address)
|
||||
throws Exception
|
||||
public class Handle extends ServerSocket {
|
||||
public void bind(SocketAddress address)
|
||||
throws IOException
|
||||
{
|
||||
socket = doListen(address.getHostName(), address.getPort());
|
||||
InetSocketAddress a;
|
||||
try {
|
||||
a = (InetSocketAddress) address;
|
||||
} catch (ClassCastException e) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
socket = doListen(a.getHostName(), a.getPort());
|
||||
}
|
||||
}
|
||||
|
||||
private static native int natDoAccept(int socket) throws Exception;
|
||||
private static native int natDoListen(String host, int port) throws Exception;
|
||||
private static native int natDoAccept(int socket) throws IOException;
|
||||
private static native int natDoListen(String host, int port) throws IOException;
|
||||
}
|
||||
|
@ -11,6 +11,7 @@
|
||||
package java.nio.channels;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.SocketAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
@ -20,30 +21,33 @@ public class SocketChannel extends SelectableChannel
|
||||
public static final int InvalidSocket = -1;
|
||||
|
||||
protected int socket = InvalidSocket;
|
||||
protected boolean open = true;
|
||||
protected boolean connected = false;
|
||||
|
||||
public static SocketChannel open() {
|
||||
return new SocketChannel();
|
||||
}
|
||||
|
||||
public void configureBlocking(boolean v) {
|
||||
public SelectableChannel configureBlocking(boolean v) {
|
||||
if (v) throw new IllegalArgumentException();
|
||||
return this;
|
||||
}
|
||||
|
||||
public void connect(InetSocketAddress address) throws Exception {
|
||||
socket = doConnect(address.getHostName(), address.getPort());
|
||||
public boolean connect(SocketAddress address) throws Exception {
|
||||
InetSocketAddress a;
|
||||
try {
|
||||
a = (InetSocketAddress) address;
|
||||
} catch (ClassCastException e) {
|
||||
throw new UnsupportedAddressTypeException();
|
||||
}
|
||||
socket = doConnect(a.getHostName(), a.getPort());
|
||||
return connected;
|
||||
}
|
||||
|
||||
public void close() throws IOException {
|
||||
super.close();
|
||||
if (! open) return;
|
||||
closeSocket();
|
||||
open = false;
|
||||
}
|
||||
|
||||
public boolean isOpen() {
|
||||
return open;
|
||||
if (! isOpen()) {
|
||||
super.close();
|
||||
closeSocket();
|
||||
}
|
||||
}
|
||||
|
||||
private int doConnect(String host, int port) throws Exception {
|
||||
@ -54,7 +58,7 @@ public class SocketChannel extends SelectableChannel
|
||||
}
|
||||
|
||||
public int read(ByteBuffer b) throws IOException {
|
||||
if (! open) return -1;
|
||||
if (! isOpen()) return -1;
|
||||
if (b.remaining() == 0) return 0;
|
||||
int r = natRead(socket, b.array(), b.arrayOffset() + b.position(), b.remaining());
|
||||
if (r > 0) {
|
||||
|
@ -52,10 +52,11 @@ class SocketSelector extends Selector {
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void select(long interval) throws IOException {
|
||||
public synchronized int select(long interval) throws IOException {
|
||||
selectedKeys.clear();
|
||||
|
||||
if (clearWoken()) return;
|
||||
if (clearWoken()) return 0;
|
||||
|
||||
int max=0;
|
||||
for (Iterator<SelectionKey> it = keys.iterator();
|
||||
it.hasNext();)
|
||||
@ -87,6 +88,8 @@ class SocketSelector extends Selector {
|
||||
}
|
||||
}
|
||||
clearWoken();
|
||||
|
||||
return selectedKeys.size();
|
||||
}
|
||||
|
||||
public void close() {
|
||||
|
@ -0,0 +1,17 @@
|
||||
/* Copyright (c) 2008, Avian Contributors
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software
|
||||
for any purpose with or without fee is hereby granted, provided
|
||||
that the above copyright notice and this permission notice appear
|
||||
in all copies.
|
||||
|
||||
There is NO WARRANTY for this software. See license.txt for
|
||||
details. */
|
||||
|
||||
package java.nio.channels;
|
||||
|
||||
public class UnsupportedAddressTypeException extends IllegalArgumentException {
|
||||
public UnsupportedAddressTypeException() {
|
||||
super(null, null);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user