diff --git a/classpath/java/net/InetSocketAddress.java b/classpath/java/net/InetSocketAddress.java index f8fd1c313b..e85aab19d7 100644 --- a/classpath/java/net/InetSocketAddress.java +++ b/classpath/java/net/InetSocketAddress.java @@ -10,7 +10,7 @@ package java.net; -public class InetSocketAddress { +public class InetSocketAddress extends SocketAddress { private final String host; private final int port; diff --git a/classpath/java/net/ServerSocket.java b/classpath/java/net/ServerSocket.java new file mode 100644 index 0000000000..191bc85c1e --- /dev/null +++ b/classpath/java/net/ServerSocket.java @@ -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; +} diff --git a/classpath/java/net/SocketAddress.java b/classpath/java/net/SocketAddress.java new file mode 100644 index 0000000000..6b096590cc --- /dev/null +++ b/classpath/java/net/SocketAddress.java @@ -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 { } diff --git a/classpath/java/nio/Buffer.java b/classpath/java/nio/Buffer.java new file mode 100644 index 0000000000..3de26da72f --- /dev/null +++ b/classpath/java/nio/Buffer.java @@ -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; + } +} diff --git a/classpath/java/nio/ByteBuffer.java b/classpath/java/nio/ByteBuffer.java index be6b838100..858c0eb23d 100644 --- a/classpath/java/nio/ByteBuffer.java +++ b/classpath/java/nio/ByteBuffer.java @@ -10,12 +10,9 @@ package java.nio; -public class ByteBuffer implements Comparable { +public class ByteBuffer extends Buffer implements Comparable { 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 { 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 { 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 { 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 { 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 { return array[arrayOffset+position]; } - public ByteBuffer flip() { - limit = position; - position = 0; - return this; - } - public int getInt() { checkGet(4); int i = get() << 24; diff --git a/classpath/java/nio/channels/SelectableChannel.java b/classpath/java/nio/channels/SelectableChannel.java index 5ed88904e1..286337a40d 100644 --- a/classpath/java/nio/channels/SelectableChannel.java +++ b/classpath/java/nio/channels/SelectableChannel.java @@ -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); diff --git a/classpath/java/nio/channels/Selector.java b/classpath/java/nio/channels/Selector.java index faf7125252..2e24fa69b9 100644 --- a/classpath/java/nio/channels/Selector.java +++ b/classpath/java/nio/channels/Selector.java @@ -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(); } diff --git a/classpath/java/nio/channels/ServerSocketChannel.java b/classpath/java/nio/channels/ServerSocketChannel.java index 178060b200..c1311b5116 100644 --- a/classpath/java/nio/channels/ServerSocketChannel.java +++ b/classpath/java/nio/channels/ServerSocketChannel.java @@ -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; } diff --git a/classpath/java/nio/channels/SocketChannel.java b/classpath/java/nio/channels/SocketChannel.java index 932085b8ea..79bcccf2ad 100644 --- a/classpath/java/nio/channels/SocketChannel.java +++ b/classpath/java/nio/channels/SocketChannel.java @@ -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) { diff --git a/classpath/java/nio/channels/SocketSelector.java b/classpath/java/nio/channels/SocketSelector.java index 3e63e42feb..c32bc9be32 100644 --- a/classpath/java/nio/channels/SocketSelector.java +++ b/classpath/java/nio/channels/SocketSelector.java @@ -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 it = keys.iterator(); it.hasNext();) @@ -87,6 +88,8 @@ class SocketSelector extends Selector { } } clearWoken(); + + return selectedKeys.size(); } public void close() { diff --git a/classpath/java/nio/channels/UnsupportedAddressTypeException.java b/classpath/java/nio/channels/UnsupportedAddressTypeException.java new file mode 100644 index 0000000000..13659fd7ac --- /dev/null +++ b/classpath/java/nio/channels/UnsupportedAddressTypeException.java @@ -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); + } +}