diff options
Diffstat (limited to 'libjava/gnu/java/nio/SocketChannelImpl.java')
-rw-r--r-- | libjava/gnu/java/nio/SocketChannelImpl.java | 98 |
1 files changed, 30 insertions, 68 deletions
diff --git a/libjava/gnu/java/nio/SocketChannelImpl.java b/libjava/gnu/java/nio/SocketChannelImpl.java index 820d62f3b6c..b721c6d7d51 100644 --- a/libjava/gnu/java/nio/SocketChannelImpl.java +++ b/libjava/gnu/java/nio/SocketChannelImpl.java @@ -52,30 +52,13 @@ import gnu.classpath.Configuration; public class SocketChannelImpl extends SocketChannel { Socket socket; - int fd; - int local_port; boolean blocking = true; boolean connected = false; - InetSocketAddress sa; - - static native int SocketCreate(); - static native int SocketConnect(int fd, InetAddress addr, int port); - static native int SocketBind(int fd, InetAddress addr, int port); - static native int SocketListen(int fd, int backlog); - static native int SocketAvailable(int fd); - static native int SocketClose(int fd); - static native int SocketRead(int fd, byte b[], int off, int len); - static native int SocketWrite(int fd, byte b[], int off, int len); - - public SocketChannelImpl(SelectorProvider provider) + + public SocketChannelImpl (SelectorProvider provider) { - super(provider); - fd = SocketCreate(); - - if (fd == -1) - { - System.err.println("failed to create socket:"+fd); - } + super (provider); + socket = new Socket (); } public void finalizer() @@ -95,39 +78,22 @@ public class SocketChannelImpl extends SocketChannel protected void implCloseSelectableChannel () throws IOException { connected = false; - SocketClose(fd); - fd = SocketCreate(); + socket.close(); } protected void implConfigureBlocking (boolean blocking) throws IOException { - if (this.blocking == blocking) - return; + this.blocking = blocking; // FIXME } public boolean connect (SocketAddress remote) throws IOException { if (connected) - { - throw new AlreadyConnectedException (); - } - - // ok, lets connect ! + throw new AlreadyConnectedException(); - sa = (InetSocketAddress) remote; - - InetAddress addr = sa.getAddress(); - int port = sa.getPort(); - int err = SocketConnect(fd, addr, port); - - if (err < 0) - { - throw new IOException("Connection refused:"+err + ", connect="+err); - } - - local_port = err; + socket.connect (remote, 50); connected = true; - return blocking; + return blocking; // FIXME } public boolean finishConnect () @@ -147,30 +113,26 @@ public class SocketChannelImpl extends SocketChannel public Socket socket () { - if (socket != null) - { - //socket.ch = this; - } - return socket; } public int read (ByteBuffer dst) throws IOException { + byte[] data; int bytes = 0; - int len = 1024; - byte[]b = new byte[len]; + int len = dst.remaining (); - bytes = SocketRead(fd, b, 0, len); - dst.put(b, 0, bytes); - - if (bytes == 0) + if (!dst.hasArray ()) { - // we've hit eof ? - return -1; + data = new byte [len]; + dst.get (data, 0, len); } - - return bytes; + else + { + data = dst.array (); + } + + return socket.getInputStream().read (data, 0, len); } public long read (ByteBuffer[] dsts, int offset, int length) @@ -189,22 +151,22 @@ public class SocketChannelImpl extends SocketChannel public int write (ByteBuffer src) throws IOException { + byte[] data; int bytes = 0; - int len = src.position(); - - if (src.hasArray ()) + int len = src.remaining (); + + if (!src.hasArray ()) { - byte[] b = src.array (); - bytes = SocketWrite (fd, b, 0, len); + data = new byte [len]; + src.get (data, 0, len); } else { - byte[] b = new byte [len]; - src.get (b, 0, len); - bytes = SocketWrite (fd, b, 0, len); + data = src.array (); } - - return bytes; + + socket.getOutputStream().write (data, 0, len); + return len; } public long write (ByteBuffer[] srcs, int offset, int length) |