summaryrefslogtreecommitdiff
path: root/gnu
diff options
context:
space:
mode:
authorRonald Veldema <rveldema@cs.vu.nl>2002-03-11 16:24:12 +0000
committerRonald Veldema <rveldema@cs.vu.nl>2002-03-11 16:24:12 +0000
commit4dd1666fff1bd101b979357e9fd7f50a430884b0 (patch)
tree12cafc867be5431a42a75ffddf7881b2ac0749dc /gnu
parent5158e5cd020ca88753de8793a40dcda66f3ea8ae (diff)
downloadclasspath-4dd1666fff1bd101b979357e9fd7f50a430884b0.tar.gz
*** empty log message ***
Diffstat (limited to 'gnu')
-rw-r--r--gnu/java/nio/DatagramChannelImpl.java76
-rw-r--r--gnu/java/nio/FileChannelImpl.java70
-rw-r--r--gnu/java/nio/GenericBuffer.cpp233
-rw-r--r--gnu/java/nio/GenericMappedByteFileBuffer.cpp138
-rw-r--r--gnu/java/nio/PipeImpl.java21
-rw-r--r--gnu/java/nio/SelectionKeyImpl.java48
-rw-r--r--gnu/java/nio/SelectorImpl.java155
-rw-r--r--gnu/java/nio/SelectorProviderImpl.java33
-rw-r--r--gnu/java/nio/ServerSocketChannelImpl.java63
-rw-r--r--gnu/java/nio/SocketChannelImpl.java194
10 files changed, 1031 insertions, 0 deletions
diff --git a/gnu/java/nio/DatagramChannelImpl.java b/gnu/java/nio/DatagramChannelImpl.java
new file mode 100644
index 000000000..98119a3f5
--- /dev/null
+++ b/gnu/java/nio/DatagramChannelImpl.java
@@ -0,0 +1,76 @@
+package manta.runtime;
+
+
+import java.nio.channels.*;
+import java.nio.channels.spi.*;
+import java.io.*;
+import java.net.*;
+import java.nio.*;
+
+class DatagramChannelImpl extends DatagramChannel
+{
+
+ protected DatagramChannelImpl(SelectorProvider provider)
+ {
+ super(provider);
+ }
+
+
+ protected void implCloseSelectableChannel()
+ {
+ }
+
+ protected void implConfigureBlocking(boolean block)
+ {
+ }
+
+ public int write(java.nio.ByteBuffer src)
+ {
+ return 0;
+ }
+
+ public long write(java.nio.ByteBuffer[] srcs, int offset, int length)
+ {
+ return 0;
+ }
+
+ public int read(java.nio.ByteBuffer dst)
+ {
+ return 0;
+ }
+
+ DatagramChannel connect(java.net.SocketAddress remote)
+ {
+ return null;
+ }
+
+ DatagramChannel disconnect()
+ {
+ return null;
+ }
+
+ boolean isConnected()
+ {
+ return false;
+ }
+
+ long read(ByteBuffer[] dsts, int offset, int length)
+ {
+ return 0;
+ }
+
+ SocketAddress receive(java.nio.ByteBuffer dst)
+ {
+ return null;
+ }
+
+ int send(java.nio.ByteBuffer src, java.net.SocketAddress target)
+ {
+ return 0;
+ }
+
+ DatagramSocket socket()
+ {
+ return null;
+ }
+}
diff --git a/gnu/java/nio/FileChannelImpl.java b/gnu/java/nio/FileChannelImpl.java
new file mode 100644
index 000000000..4e4505916
--- /dev/null
+++ b/gnu/java/nio/FileChannelImpl.java
@@ -0,0 +1,70 @@
+package manta.runtime;
+
+import java.io.*;
+import java.nio.*;
+import java.nio.channels.*;
+
+
+public class FileChannelImpl extends FileChannel
+{
+ long address;
+ int fd;
+ MappedByteBuffer buf;
+ Object file_obj; // just to keep it live...
+
+ FileChannelImpl(int fd,
+ Object obj)
+ {
+ this.fd = fd;
+ this.file_obj = obj;
+ }
+
+ FileChannelImpl(FileDescriptor fd,
+ Object obj)
+ {
+ this(fd.file_des, obj);
+ }
+
+
+ public int read(java.nio.ByteBuffer dst) throws IOException
+ {
+ System.out.println("in here-1");
+ return 0;
+ }
+
+ public int write(java.nio.ByteBuffer src) throws IOException
+ {
+ System.out.println("in here-2");
+ return 0;
+ }
+
+ public int write(java.nio.ByteBuffer[] srcs,
+ int offset,
+ int length) throws IOException
+ {
+ System.out.println("in here-3");
+ return 0;
+ }
+
+ static MantaNative long nio_mmap_file(int fd,
+ long pos,
+ int size,
+ int mode);
+
+ static MantaNative void nio_unmmap_file(int fd,
+ long pos,
+ int size);
+
+ public MappedByteBuffer map(java.nio.channels.FileChannel.MapMode mode,
+ long position,
+ int size) throws IOException
+ {
+ int cmode = 0;
+
+ address = nio_mmap_file(fd, position, size, cmode);
+
+ buf = new MappedByteFileBuffer(this);
+ return buf;
+ }
+}
+
diff --git a/gnu/java/nio/GenericBuffer.cpp b/gnu/java/nio/GenericBuffer.cpp
new file mode 100644
index 000000000..07e540052
--- /dev/null
+++ b/gnu/java/nio/GenericBuffer.cpp
@@ -0,0 +1,233 @@
+package manta.runtime;
+
+import java.nio.*;
+
+// these are wrappers around byte[]'s
+
+
+#include "temp.h"
+
+public final class BUFFERImpl extends java.nio. BUFFER
+{
+ private int array_offset;
+ ELT [] backing_buffer;
+ private boolean ro;
+
+ BUFFERImpl(int cap, int off, int lim)
+ {
+ this.backing_buffer = new ELT[cap];
+ this.cap = cap;
+ this.pos = off;
+ this.limit = lim;
+ }
+
+ BUFFERImpl(ELT[] array, int off, int lim)
+ {
+ this.backing_buffer = array;
+ this.cap = array.length;
+ this.pos = off;
+ this.limit = lim;
+ }
+
+ BUFFERImpl(BUFFERImpl copy)
+ {
+ backing_buffer = copy.backing_buffer;
+ ro = copy.ro;
+ pos = copy.pos;
+ limit = copy.limit;
+ }
+
+ void inc_pos(int a)
+ {
+ pos += a;
+ }
+
+ private static MantaNative ELT[] nio_cast(byte[]copy);
+ private static MantaNative ELT[] nio_cast(char[]copy);
+ private static MantaNative ELT[] nio_cast(short[]copy);
+ private static MantaNative ELT[] nio_cast(long[]copy);
+ private static MantaNative ELT[] nio_cast(int[]copy);
+ private static MantaNative ELT[] nio_cast(float[]copy);
+ private static MantaNative ELT[] nio_cast(double[]copy);
+
+#define CAST_CTOR(ELT,TO_TYPE) \
+ BUFFERImpl(ELT[] copy) \
+ { \
+ this.backing_buffer = copy != null ? nio_cast(copy) : null; \
+ } \
+\
+\
+\
+ private static MantaNative ELT nio_get_ ## TO_TYPE(BUFFERImpl b, int index); \
+\
+\
+ private static MantaNative void nio_put_ ## TO_TYPE(BUFFERImpl b, int index, ELT value);\
+\
+\
+ public java.nio. TO_TYPE ## Buffer as ## TO_TYPE ## Buffer() \
+ { \
+ return new manta.runtime. TO_TYPE ## BufferImpl(backing_buffer); \
+ }
+
+
+ CAST_CTOR(byte,Byte)
+ CAST_CTOR(char,Char)
+ CAST_CTOR(short,Short)
+ CAST_CTOR(int,Int)
+ CAST_CTOR(long,Long)
+ CAST_CTOR(float,Float)
+ CAST_CTOR(double,Double)
+
+
+ public boolean isReadOnly()
+ {
+ return ro;
+ }
+
+ public java.nio. BUFFER slice()
+ {
+ BUFFERImpl A = new BUFFERImpl(this);
+ A.array_offset = pos;
+ return A;
+ }
+
+ public java.nio. BUFFER duplicate()
+ {
+ return new BUFFERImpl(this);
+ }
+
+ public java.nio. BUFFER asReadOnlyBuffer()
+ {
+ BUFFERImpl a = new BUFFERImpl(this);
+ a.ro = true;
+ return a;
+ }
+
+ public java.nio. BUFFER compact()
+ {
+ return this;
+ }
+
+ public boolean isDirect()
+ {
+ return backing_buffer != null;
+ }
+
+ public ELT get()
+ {
+ ELT e = backing_buffer[pos];
+ pos++;
+ return e;
+ }
+
+ public java.nio. BUFFER put(ELT b)
+ {
+ backing_buffer[pos] = b;
+ pos++;
+ return this;
+ }
+ public ELT get(int index)
+ {
+ return backing_buffer[index];
+ }
+ public java.nio. BUFFER put(int index, ELT b)
+ {
+ backing_buffer[index] = b;
+ return this;
+ }
+
+
+#define NATIVE_GET_PUT(TYPE,SIZE,ELT) \
+ public ELT get ## TYPE() \
+ { \
+ ELT a = nio_get_ ## TYPE(this, pos); \
+ inc_pos(SIZE); \
+ return a; \
+ } \
+ public java.nio. BUFFER put ## TYPE(ELT value) \
+ { \
+ nio_put_ ## TYPE(this, pos, value); \
+ inc_pos(SIZE); \
+ return this; \
+ } \
+ public ELT get ## TYPE(int index) \
+ { \
+ ELT a = nio_get_ ## TYPE(this, index); \
+ inc_pos(SIZE); \
+ return a; \
+ } \
+ public java.nio. BUFFER put ## TYPE(int index, ELT value) \
+ { \
+ nio_put_ ## TYPE(this, index, value); \
+ inc_pos(SIZE); \
+ return this; \
+ }
+
+#define INLINE_GET_PUT(TYPE,ELT) \
+ public ELT get ## TYPE() \
+ { \
+ return get(); \
+ } \
+ public java.nio. BUFFER put ## TYPE(ELT value) \
+ { \
+ return put(value); \
+ } \
+ public ELT get ## TYPE(int index) \
+ { \
+ return get(index); \
+ } \
+ public java.nio. BUFFER put ## TYPE(int index, ELT value) \
+ { \
+ return put(index, value); \
+ }
+
+
+#if defined(CHAR)
+ INLINE_GET_PUT(Char, char);
+#else
+ NATIVE_GET_PUT(Char,2,char);
+#endif
+
+
+#if defined(SHORT)
+ INLINE_GET_PUT(Short, short);
+#else
+ NATIVE_GET_PUT(Short,2,short);
+#endif
+
+#if defined(INT)
+ INLINE_GET_PUT(Int,int);
+#else
+ NATIVE_GET_PUT(Int,4,int);
+#endif
+
+#if defined(LONG)
+ INLINE_GET_PUT(Long,long);
+#else
+ NATIVE_GET_PUT(Long,8,long);
+#endif
+
+#if defined(FLOAT)
+ INLINE_GET_PUT(Float,float);
+#else
+ NATIVE_GET_PUT(Float,4,float);
+#endif
+
+#if defined(DOUBLE)
+ INLINE_GET_PUT(Double,double);
+#else
+ NATIVE_GET_PUT(Double,8,double);
+#endif
+
+
+#if defined(CHAR)
+ public String toString()
+ {
+ if (backing_buffer != null)
+ {
+ return new String(backing_buffer, position(), limit());
+ }
+ return super.toString();
+ }
+#endif
+}
diff --git a/gnu/java/nio/GenericMappedByteFileBuffer.cpp b/gnu/java/nio/GenericMappedByteFileBuffer.cpp
new file mode 100644
index 000000000..fa826cdb4
--- /dev/null
+++ b/gnu/java/nio/GenericMappedByteFileBuffer.cpp
@@ -0,0 +1,138 @@
+package manta.runtime;
+
+import java.nio.*;
+
+#include "temp.h"
+
+final public class MappedTYPEFileBuffer
+#if SIZE == 1
+ extends MappedByteBuffer
+#else
+ extends TYPEBuffer
+#endif
+{
+ boolean ro;
+ boolean direct;
+ FileChannelImpl ch;
+
+ MappedTYPEFileBuffer(FileChannelImpl ch)
+ {
+ this.ch = ch;
+ }
+
+ MappedTYPEFileBuffer(MappedTYPEFileBuffer b)
+ {
+ this.ro = b.ro;
+ this.ch = b.ch;
+ }
+
+ boolean isReadOnly()
+ {
+ return ro;
+ }
+
+#if SIZE == 1
+#define GO(TYPE,ELT) \
+ static MantaNative ELT nio_read_ ## TYPE ## _file_channel(FileChannelImpl ch, int index); \
+ static MantaNative void nio_write_ ## TYPE ## _file_channel(FileChannelImpl ch, int index, ELT value)
+
+ GO(Byte,byte);
+ GO(Short,short);
+ GO(Char,char);
+ GO(Int,int);
+ GO(Long,long);
+ GO(Float,float);
+ GO(Double,double);
+#endif
+
+public ELT get()
+ {
+ ELT a = MappedByteFileBuffer.nio_read_TYPE_file_channel(ch, pos);
+ pos += SIZE;
+ return a;
+ }
+
+public TYPEBuffer put(ELT b)
+ {
+ MappedByteFileBuffer.nio_write_TYPE_file_channel(ch, pos, b);
+ pos += SIZE;
+ return this;
+ }
+
+public ELT get(int index)
+ {
+ ELT a = MappedByteFileBuffer.nio_read_TYPE_file_channel(ch, index);
+ return a;
+ }
+
+public TYPEBuffer put(int index, ELT b)
+ {
+ MappedByteFileBuffer.nio_write_TYPE_file_channel(ch, index, b);
+ return this;
+ }
+
+public TYPEBuffer compact()
+ {
+ return this;
+ }
+
+public boolean isDirect()
+ {
+ return direct;
+ }
+
+public TYPEBuffer slice()
+ {
+ MappedTYPEFileBuffer A = new MappedTYPEFileBuffer(this);
+ return A;
+ }
+public TYPEBuffer duplicate()
+ {
+ return new MappedTYPEFileBuffer(this);
+ }
+
+public TYPEBuffer asReadOnlyBuffer()
+ {
+ MappedTYPEFileBuffer b = new MappedTYPEFileBuffer(this);
+ b.ro = true;
+ return b;
+ }
+
+#define CONVERT(TYPE,STYPE) \
+ public TYPE ## Buffer as ## TYPE ## Buffer() \
+ { \
+ return new Mapped ## TYPE ## FileBuffer(ch); \
+ } \
+public STYPE get ## TYPE() \
+ { \
+ STYPE a = MappedByteFileBuffer.nio_read_ ## TYPE ## _file_channel(ch, pos); \
+ pos += SIZE; \
+ return a; \
+ } \
+public TYPEBuffer put ## TYPE(STYPE value) \
+ { \
+ MappedByteFileBuffer.nio_write_ ## TYPE ## _file_channel(ch, pos, value); \
+ pos += SIZE; \
+ return this; \
+ } \
+public STYPE get ## TYPE(int index) \
+ { \
+ STYPE a = MappedByteFileBuffer.nio_read_ ## TYPE ## _file_channel(ch, index); \
+ return a; \
+ } \
+public TYPEBuffer put ## TYPE(int index, STYPE value) \
+ { \
+ MappedByteFileBuffer.nio_write_ ## TYPE ## _file_channel(ch, index, value); \
+ return this; \
+ }
+
+ CONVERT(Byte,byte);
+ CONVERT(Char,char);
+ CONVERT(Short,short);
+ CONVERT(Int,int);
+ CONVERT(Long,long);
+ CONVERT(Float,float);
+ CONVERT(Double,double);
+
+}
+
diff --git a/gnu/java/nio/PipeImpl.java b/gnu/java/nio/PipeImpl.java
new file mode 100644
index 000000000..e24254d2d
--- /dev/null
+++ b/gnu/java/nio/PipeImpl.java
@@ -0,0 +1,21 @@
+package manta.runtime;
+
+import java.nio.channels.*;
+
+class PipeImpl extends Pipe
+{
+ PipeImpl()
+ {
+ }
+
+
+ java.nio.channels.Pipe.SinkChannel sink()
+ {
+ return null;
+ }
+
+ java.nio.channels.Pipe.SourceChannel source()
+ {
+ return null;
+ }
+}
diff --git a/gnu/java/nio/SelectionKeyImpl.java b/gnu/java/nio/SelectionKeyImpl.java
new file mode 100644
index 000000000..a05791bb8
--- /dev/null
+++ b/gnu/java/nio/SelectionKeyImpl.java
@@ -0,0 +1,48 @@
+package manta.runtime;
+
+import java.nio.channels.*;
+import java.nio.channels.spi.*;
+
+class SelectionKeyImpl extends AbstractSelectionKey
+{
+ int fd, ops;
+ SelectorImpl impl;
+ SelectableChannel ch;
+
+ SelectionKeyImpl(SelectableChannel ch,
+ SelectorImpl impl,
+ int fd)
+ {
+ this.ch = ch;
+ this.impl = impl;
+ this.fd = fd;
+ }
+
+
+ SelectableChannel channel()
+ {
+ return ch;
+ }
+
+
+ int readyOps()
+ {
+ return 0;
+ }
+
+ int interestOps()
+ {
+ return ops;
+ }
+ SelectionKey interestOps(int ops)
+ {
+ this.ops = ops;
+ return this;
+ }
+
+
+ Selector selector()
+ {
+ return impl;
+ }
+}
diff --git a/gnu/java/nio/SelectorImpl.java b/gnu/java/nio/SelectorImpl.java
new file mode 100644
index 000000000..19bb67b85
--- /dev/null
+++ b/gnu/java/nio/SelectorImpl.java
@@ -0,0 +1,155 @@
+package manta.runtime;
+
+import java.util.*;
+import java.nio.channels.*;
+import java.nio.channels.spi.*;
+
+
+public class SelectorImpl extends AbstractSelector
+{
+ Set keys, selected, canceled;
+
+ SelectorImpl(SelectorProvider provider)
+ {
+ super(provider);
+ }
+
+ Set keys()
+ {
+ return keys;
+ }
+
+ int selectNow() { return select(1); }
+ int select() { return select(Long.MAX_VALUE); }
+
+ private static MantaNative int java_do_select(int []read,
+ int []write,
+ int []except,
+ long timeout);
+
+ int select(long timeout)
+ {
+ if (keys == null)
+ {
+ return 0;
+ }
+
+ int [] read = new int[keys.size()];
+ int [] write = new int[keys.size()];
+ int [] except = new int[keys.size()];
+ int i = 0;
+
+ Iterator it = keys.iterator();
+ while (it.hasNext())
+ {
+ SelectionKeyImpl k = (SelectionKeyImpl) it.next();
+
+ read[i] = k.fd;
+ write[i] = k.fd;
+ except[i] = k.fd;
+
+ i++;
+ }
+
+ int ret = java_do_select(read,
+ write,
+ except,
+ timeout);
+ it = keys.iterator();
+ while (it.hasNext())
+ {
+ SelectionKeyImpl k = (SelectionKeyImpl) it.next();
+
+ if (read[i] != -1 ||
+ write[i] != -1 ||
+ except[i] != -1)
+ {
+ add_selected(k);
+ }
+
+ i++;
+ }
+ return ret;
+ }
+
+
+ Set selectedKeys() { return selected; }
+ Set cancelledKeys() { return canceled; }
+
+
+
+ Selector wakeup()
+ {
+ return null;
+ }
+
+ void add(SelectionKeyImpl k)
+ {
+ if (keys == null)
+ keys = new HashSet();
+
+ keys.add(k);
+ }
+ void add_selected(SelectionKeyImpl k)
+ {
+ if (selected == null)
+ selected = new HashSet();
+
+ selected.add(k);
+ }
+
+
+ protected void implCloseSelector()
+ {
+ }
+
+ protected SelectionKey register(SelectableChannel ch,
+ int ops,
+ Object att)
+ {
+ return register((AbstractSelectableChannel) ch,
+ ops,
+ att);
+ }
+
+ protected SelectionKey register(AbstractSelectableChannel ch,
+ int ops,
+ Object att)
+ {
+ /*
+ // filechannel is not selectable ?
+ if (ch instanceof manta.runtime.FileChannelImpl)
+ {
+ FileChannelImpl fc = (FileChannelImpl) ch;
+
+ SelectionKeyImpl impl = new SelectionKeyImpl(ch,
+ this,
+ fc.fd);
+
+ keys.add(impl);
+
+ return impl;
+ }
+ else
+ */
+
+ if (ch instanceof manta.runtime.SocketChannelImpl)
+ {
+ SocketChannelImpl fc = (SocketChannelImpl) ch;
+
+ SelectionKeyImpl impl = new SelectionKeyImpl(ch,
+ this,
+ fc.fd);
+ add(impl);
+
+ return impl;
+ }
+ else
+ {
+ System.err.println("INTERNAL ERROR, no known channel type");
+ }
+
+ return null;
+ }
+
+}
diff --git a/gnu/java/nio/SelectorProviderImpl.java b/gnu/java/nio/SelectorProviderImpl.java
new file mode 100644
index 000000000..9153dd1f8
--- /dev/null
+++ b/gnu/java/nio/SelectorProviderImpl.java
@@ -0,0 +1,33 @@
+package manta.runtime;
+
+import java.nio.channels.spi.*;
+import java.nio.channels.*;
+
+
+public class SelectorProviderImpl extends SelectorProvider
+{
+ DatagramChannel openDatagramChannel()
+ {
+ return new DatagramChannelImpl(this);
+ }
+
+ Pipe openPipe()
+ {
+ return new PipeImpl();
+ }
+
+ AbstractSelector openSelector()
+ {
+ return new SelectorImpl(this);
+ }
+
+ ServerSocketChannel openServerSocketChannel()
+ {
+ return new ServerSocketChannelImpl(this);
+ }
+
+ SocketChannel openSocketChannel()
+ {
+ return new SocketChannelImpl(this);
+ }
+}
diff --git a/gnu/java/nio/ServerSocketChannelImpl.java b/gnu/java/nio/ServerSocketChannelImpl.java
new file mode 100644
index 000000000..4c0e8fec2
--- /dev/null
+++ b/gnu/java/nio/ServerSocketChannelImpl.java
@@ -0,0 +1,63 @@
+package manta.runtime;
+
+import java.io.*;
+import java.nio.channels.spi.*;
+import java.nio.channels.*;
+import java.net.*;
+
+class ServerSocketChannelImpl extends ServerSocketChannel
+{
+ ServerSocket sock_object;
+ int fd;
+ int local_port;
+ boolean blocking = true;
+ boolean connected = false;
+ InetSocketAddress sa;
+
+ private static native int NioSocketAccept(ServerSocketChannelImpl server,
+ SocketChannelImpl s);
+
+ protected ServerSocketChannelImpl(SelectorProvider provider)
+ {
+ super(provider);
+ fd = SocketChannelImpl.SocketCreate();
+ }
+
+ public void finalizer()
+ {
+ if (connected)
+ {
+ try {
+ close();
+ } catch (Exception e) {
+ }
+ }
+ }
+
+ protected void implCloseSelectableChannel()
+ {
+ connected = false;
+ SocketChannelImpl.SocketClose(fd);
+ fd = SocketChannelImpl.SocketCreate();
+ }
+
+ protected void implConfigureBlocking(boolean block)
+ {
+ }
+
+ SocketChannel accept()
+ {
+ SocketChannelImpl result = new SocketChannelImpl(provider());
+
+ result.sa = new InetSocketAddress(0);
+
+ int res = NioSocketAccept(this, result);
+
+ return result;
+ }
+
+ ServerSocket socket()
+ {
+ return null;
+ }
+}
diff --git a/gnu/java/nio/SocketChannelImpl.java b/gnu/java/nio/SocketChannelImpl.java
new file mode 100644
index 000000000..b4da0a9a5
--- /dev/null
+++ b/gnu/java/nio/SocketChannelImpl.java
@@ -0,0 +1,194 @@
+package manta.runtime;
+
+import java.net.*;
+import java.io.*;
+import java.nio.*;
+import java.nio.channels.*;
+import java.nio.channels.spi.*;
+
+public class SocketChannelImpl extends SocketChannel
+{
+ Socket sock_object;
+ int fd;
+ int local_port;
+ boolean blocking = true;
+ boolean connected = false;
+ InetSocketAddress sa;
+
+ static native int SocketCreate();
+ static native int SocketConnect(int fd, InetAddress a, int port);
+ static native int SocketBind(int fd, InetAddress host, 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);
+
+
+ SocketChannelImpl(SelectorProvider provider)
+ {
+ super(provider);
+
+ fd = SocketCreate();
+
+ //System.out.println("socket-channel:"+fd);
+ }
+
+ public void finalizer()
+ {
+ if (connected)
+ {
+ try {
+ close();
+ } catch (Exception e) {
+ }
+ }
+ }
+
+
+ int validOps()
+ {
+ return SelectionKey.OP_READ | SelectionKey.OP_WRITE | SelectionKey.OP_CONNECT;
+ }
+
+ protected void implCloseSelectableChannel()
+ {
+ connected = false;
+ SocketClose(fd);
+ fd = SocketCreate();
+ }
+
+ protected void implConfigureBlocking(boolean block)
+ {
+ if (blocking == block)
+ return;
+ }
+
+ boolean connect(SocketAddress remote)
+ throws IOException
+ {
+ if (connected)
+ {
+ throw new AlreadyConnectedException();
+ }
+
+ // ok, lets connect !
+
+ sa = (InetSocketAddress) remote;
+
+ InetAddress addr = sa.getAddress();
+ int port = sa.getPort();
+
+ // System.out.println("CONNECT: " + addr + ","+port);
+
+ int err = SocketConnect(fd, addr, port);
+
+ if (err < 0)
+ {
+ throw new IOException("Connection refused:"+err + ", connect="+err);
+ }
+
+ local_port = err;
+
+ connected = true;
+
+ return blocking;
+ }
+
+ boolean finishConnect()
+ {
+ return false;
+ }
+
+ boolean isConnected()
+ {
+ return connected;
+ }
+
+ boolean isConnectionPending()
+ {
+ if (blocking)
+ return false;
+ return false;
+ }
+ Socket socket()
+ {
+ if (sock_object != null)
+ {
+ sock_object.ch = this;
+ }
+ return sock_object;
+ }
+
+
+ int read(ByteBuffer dst)
+ {
+ int bytes = 0;
+
+ int len = 1024;
+ byte[]b = new byte[len];
+
+ bytes = SocketRead(fd, b, 0, len);
+ //System.out.println("readbytes:"+bytes +",len" +len);
+
+ dst.put(b, 0, bytes);
+
+ if (bytes == 0)
+ {
+ // we've hit eof ?
+ return -1;
+ }
+
+ return bytes;
+ }
+
+
+ long read(ByteBuffer[] dsts, int offset, int length)
+ {
+ long bytes = 0;
+ for (int i=offset; i<length; i++)
+ {
+ bytes += read(dsts[i]);
+ }
+ return bytes;
+ }
+
+
+ int write(ByteBuffer src)
+ {
+ int bytes = 0;
+
+ int len = src.position();
+
+ if (src instanceof ByteBufferImpl)
+ {
+ ByteBufferImpl bi = (ByteBufferImpl) src;
+ byte[]b = bi.backing_buffer;
+ bytes = SocketWrite(fd, b, 0, len);
+
+ //System.out.println("reused memory buffer....");
+ }
+ else
+ {
+ byte[]b = new byte[len];
+ src.get(b, 0, len);
+ bytes = SocketWrite(fd, b, 0, len);
+ }
+
+
+
+ //System.out.println("WRITEN #bytes="+bytes +",fd=" +fd+","+(char)b[0]+(char)b[1]+(char)b[2]);
+
+ return bytes;
+ }
+
+ long write(ByteBuffer[] srcs, int offset, int length)
+ {
+ long bytes = 0;
+ for (int i=offset; i<length; i++)
+ {
+ bytes += write(srcs[i]);
+ }
+ return bytes;
+ }
+}