diff options
author | Michael Koch <konqueror@gmx.de> | 2002-11-16 15:48:25 +0000 |
---|---|---|
committer | Michael Koch <konqueror@gmx.de> | 2002-11-16 15:48:25 +0000 |
commit | a2c24a0bd9eb4b186c5d8d11516b789b514ff828 (patch) | |
tree | aa160d08aa13382b12caf81e800d99a5be94eea5 | |
parent | a9033eae89f91de683e47b93db5d3e307ea4df2e (diff) | |
download | classpath-a2c24a0bd9eb4b186c5d8d11516b789b514ff828.tar.gz |
2002-11-16 Michael Koch <konqueror@gmx.de>
* gnu/java/nio/DatagramChannelImpl.java,
gnu/java/nio/FileChannelImpl.java,
gnu/java/nio/PipeImpl.java,
gnu/java/nio/SelectionKeyImpl.java,
gnu/java/nio/SelectorImpl.java,
gnu/java/nio/SelectorProviderImpl.java,
gnu/java/nio/ServerSocketChannelImpl.java,
gnu/java/nio/SocketChannelImpl.java:
Reindented.
-rw-r--r-- | ChangeLog | 12 | ||||
-rw-r--r-- | gnu/java/nio/DatagramChannelImpl.java | 112 | ||||
-rw-r--r-- | gnu/java/nio/FileChannelImpl.java | 236 | ||||
-rw-r--r-- | gnu/java/nio/PipeImpl.java | 27 | ||||
-rw-r--r-- | gnu/java/nio/SelectionKeyImpl.java | 82 | ||||
-rw-r--r-- | gnu/java/nio/SelectorImpl.java | 245 | ||||
-rw-r--r-- | gnu/java/nio/SelectorProviderImpl.java | 55 | ||||
-rw-r--r-- | gnu/java/nio/ServerSocketChannelImpl.java | 118 | ||||
-rw-r--r-- | gnu/java/nio/SocketChannelImpl.java | 285 |
9 files changed, 600 insertions, 572 deletions
@@ -1,5 +1,17 @@ 2002-11-16 Michael Koch <konqueror@gmx.de> + * gnu/java/nio/DatagramChannelImpl.java, + gnu/java/nio/FileChannelImpl.java, + gnu/java/nio/PipeImpl.java, + gnu/java/nio/SelectionKeyImpl.java, + gnu/java/nio/SelectorImpl.java, + gnu/java/nio/SelectorProviderImpl.java, + gnu/java/nio/ServerSocketChannelImpl.java, + gnu/java/nio/SocketChannelImpl.java: + Reindented. + +2002-11-16 Michael Koch <konqueror@gmx.de> + * gnu/java/nio/FileChannelImpl.java (map): Last argument must be of type long, made it a dummy method for now. diff --git a/gnu/java/nio/DatagramChannelImpl.java b/gnu/java/nio/DatagramChannelImpl.java index 423b9b1f0..b192ef236 100644 --- a/gnu/java/nio/DatagramChannelImpl.java +++ b/gnu/java/nio/DatagramChannelImpl.java @@ -37,76 +37,74 @@ exception statement from your version. */ package gnu.java.nio; -import java.nio.channels.*; -import java.nio.channels.spi.*; -import java.io.*; -import java.net.*; -import java.nio.*; +import java.net.DatagramSocket; +import java.net.SocketAddress; +import java.nio.ByteBuffer; +import java.nio.channels.DatagramChannel; +import java.nio.channels.spi.SelectorProvider; public class DatagramChannelImpl extends DatagramChannel { + protected DatagramChannelImpl (SelectorProvider provider) + { + super (provider); + } - protected DatagramChannelImpl(SelectorProvider provider) - { - super(provider); - } + protected void implCloseSelectableChannel () + { + } + protected void implConfigureBlocking (boolean block) + { + } - protected void implCloseSelectableChannel() - { - } - - protected void implConfigureBlocking(boolean block) - { - } - - public int write(java.nio.ByteBuffer src) - { - return 0; - } + public int write (ByteBuffer src) + { + return 0; + } - public long write(java.nio.ByteBuffer[] srcs, int offset, int length) - { - return 0; - } + public long write (ByteBuffer[] srcs, int offset, int length) + { + return 0; + } - public int read(java.nio.ByteBuffer dst) - { - return 0; - } + public int read (ByteBuffer dst) + { + return 0; + } - public DatagramChannel connect(java.net.SocketAddress remote) - { - return null; - } + public DatagramChannel connect (SocketAddress remote) + { + return null; + } - public DatagramChannel disconnect() - { - return null; - } + public DatagramChannel disconnect () + { + return null; + } - public boolean isConnected() - { - return false; - } + public boolean isConnected () + { + return false; + } - public long read(ByteBuffer[] dsts, int offset, int length) - { - return 0; - } + public long read (ByteBuffer[] dsts, int offset, int length) + { + return 0; + } - public SocketAddress receive(java.nio.ByteBuffer dst) - { - return null; - } + public SocketAddress receive (ByteBuffer dst) + { + return null; + } - public int send(java.nio.ByteBuffer src, java.net.SocketAddress target) - { - return 0; - } + public int send (ByteBuffer src, SocketAddress target) + { + return 0; + } - public DatagramSocket socket() - { - return null; - } + public DatagramSocket socket () + { + return null; + } } diff --git a/gnu/java/nio/FileChannelImpl.java b/gnu/java/nio/FileChannelImpl.java index 8db961c44..6b602e1fd 100644 --- a/gnu/java/nio/FileChannelImpl.java +++ b/gnu/java/nio/FileChannelImpl.java @@ -37,12 +37,18 @@ exception statement from your version. */ package gnu.java.nio; -import java.io.*; -import java.nio.*; -import java.nio.channels.*; - -/** This file is not user visible ! - * But alas, Java does not have a concept of frieldly packages +import java.io.EOFException; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.nio.ByteBuffer; +import java.nio.MappedByteBuffer; +import java.nio.channels.FileChannel; + +/** + * This file is not user visible ! + * But alas, Java does not have a concept of friendly packages * so this class is public. * Instances of this class are created by invoking getChannel * Upon a Input/Output/RandomAccessFile object. @@ -50,112 +56,118 @@ import java.nio.channels.*; public class FileChannelImpl extends FileChannel { - public long address; - public int length; - public int fd; - public MappedByteBuffer buf; - public Object file_obj; // just to keep it live... - - /** - * This method came from java.io.RandomAccessFile - * It is private there so we will repeat it here. - */ - private native long lengthInternal(int native_fd) throws IOException; - - public FileChannelImpl(int fd, - Object obj) - { - this.fd = fd; - this.file_obj = obj; - - // System.out.println("file channel: " + fd); - } - - public long size() throws IOException - { - return lengthInternal(fd); + public long address; + public int length; + public int fd; + public MappedByteBuffer buf; + public Object file_obj; // just to keep it live... + + /** + * This method came from java.io.RandomAccessFile + * It is private there so we will repeat it here. + */ + private native long lengthInternal (int native_fd) throws IOException; + + public FileChannelImpl (int fd, Object obj) + { + this.fd = fd; + this.file_obj = obj; + } - } + public long size () throws IOException + { + return lengthInternal (fd); + } - protected void implCloseChannel() throws IOException - { - //System.out.println("length in Java ="+length); - - if (address != 0) + protected void implCloseChannel() throws IOException + { + if (address != 0) { - nio_unmmap_file(fd, - address, - (int)length); + nio_unmmap_file (fd, address, (int) length); } - // FIXME - fd = 0; + // FIXME + fd = 0; - if (file_obj instanceof RandomAccessFile) + if (file_obj instanceof RandomAccessFile) { - RandomAccessFile o = (RandomAccessFile) file_obj; - o.close(); - //System.out.println("closing stream too"); + RandomAccessFile o = (RandomAccessFile) file_obj; + o.close(); } - else if (file_obj instanceof FileInputStream) + else if (file_obj instanceof FileInputStream) { - FileInputStream o = (FileInputStream) file_obj; - o.close(); + FileInputStream o = (FileInputStream) file_obj; + o.close(); } - else if (file_obj instanceof FileOutputStream) + else if (file_obj instanceof FileOutputStream) { - FileOutputStream o = (FileOutputStream) file_obj; - o.close(); + FileOutputStream o = (FileOutputStream) file_obj; + o.close(); } - } + } - public int read(java.nio.ByteBuffer dst) throws IOException - { - //System.out.println("unimplemented: in here-1"); - - int w = 0; - - int s = (int)size(); + public int read (ByteBuffer dst) throws IOException + { + int w = 0; + int s = (int)size(); - if (buf == null) + if (buf == null) { - throw new EOFException("file not mapped"); + throw new EOFException("file not mapped"); } - for (int i=0; i<s; i++) + for (int i=0; i<s; i++) { - dst.put( buf.get() ); + dst.put( buf.get() ); } - return s; - } + return s; + } + + public long read (ByteBuffer[] dsts) throws IOException + { + return read (dsts, 0, dsts.length); + } + + public long read (ByteBuffer[] dsts, int offset, int length) + throws IOException + { + long result = 0; + + for (int i = offset; i < offset + length; i++) + { + result += write (dsts[i]); + } + + return result; + } - public int write(java.nio.ByteBuffer src) throws IOException - { - int w = 0; + public int write (ByteBuffer src) throws IOException + { + int w = 0; - if (buf == null) + if (buf == null) { - throw new EOFException("file not mapped"); + throw new EOFException ("file not mapped"); } - while (src.hasRemaining()) + while (src.hasRemaining ()) { - buf.put(src.get()); - w++; + buf.put (src.get ()); + w++; } - return w; - } + + return w; + } - public long write(java.nio.ByteBuffer[] srcs, - int offset, - int length) throws IOException - { - long res = 0; + public long write(ByteBuffer[] srcs, int offset, int length) + throws IOException + { + long res = 0; - for (int i=offset;i<offset+length;i++) + for (int i = offset;i < offset + length;i++) { - res += write(srcs[i]); + res += write (srcs[i]); } return res; } @@ -172,43 +184,33 @@ public class FileChannelImpl extends FileChannel return null; } - static MappedByteBuffer create_direct_mapped_buffer(long address, - long length) - { - FileChannelImpl ch = new FileChannelImpl(-1, null); - - ch.address = address; - ch.length = (int)length; - - ch.buf = new MappedByteFileBuffer(ch); - return ch.buf; - } + static MappedByteBuffer create_direct_mapped_buffer (long address, + long length) + { + FileChannelImpl ch = new FileChannelImpl (-1, null); + ch.address = address; + ch.length = (int) length; + ch.buf = new MappedByteFileBuffer (ch); + return ch.buf; + } - /* msync with the disk */ - public void force(boolean metaData) - { - nio_msync(fd, address, length); - } + public long write (ByteBuffer[] srcs) + throws IOException + { + return write (srcs, 0, srcs.length); + } + + /** + * msync with the disk + */ + public void force (boolean metaData) + { + nio_msync (fd, address, length); + } - static native long nio_mmap_file(int fd, - long pos, - int size, - int mode); + static native long nio_mmap_file (int fd, long pos, int size, int mode); - static native void nio_unmmap_file(int fd, - long address, - int size); - static native void nio_msync(int fd, - long address, - int length); + static native void nio_unmmap_file (int fd, long address, int size); - public long write(ByteBuffer[] srcs) throws IOException { - throw new Error("not implemented"); - } - public long read(ByteBuffer[] srcs, int offset, int length) throws IOException { - throw new Error("not implemented"); - } - public long read(ByteBuffer[] srcs) throws IOException { - throw new Error("not implemented"); - } + static native void nio_msync (int fd, long address, int length); } diff --git a/gnu/java/nio/PipeImpl.java b/gnu/java/nio/PipeImpl.java index 7bd7e719e..77341e7f4 100644 --- a/gnu/java/nio/PipeImpl.java +++ b/gnu/java/nio/PipeImpl.java @@ -37,22 +37,21 @@ exception statement from your version. */ package gnu.java.nio; -import java.nio.channels.*; +import java.nio.channels.Pipe; class PipeImpl extends Pipe { - public PipeImpl() - { - } - + public PipeImpl() + { + } - public java.nio.channels.Pipe.SinkChannel sink() - { - return null; - } - - public java.nio.channels.Pipe.SourceChannel source() - { - return null; - } + public Pipe.SinkChannel sink() + { + return null; + } + + public Pipe.SourceChannel source() + { + return null; + } } diff --git a/gnu/java/nio/SelectionKeyImpl.java b/gnu/java/nio/SelectionKeyImpl.java index 30c1db8fd..8ef6f7f27 100644 --- a/gnu/java/nio/SelectionKeyImpl.java +++ b/gnu/java/nio/SelectionKeyImpl.java @@ -37,49 +37,47 @@ exception statement from your version. */ package gnu.java.nio; -import java.nio.channels.*; -import java.nio.channels.spi.*; +import java.nio.channels.SelectableChannel; +import java.nio.channels.SelectionKey; +import java.nio.channels.Selector; +import java.nio.channels.spi.AbstractSelectionKey; -public class SelectionKeyImpl extends AbstractSelectionKey +public class SelectionKeyImpl extends AbstractSelectionKey { - int fd, ops; - SelectorImpl impl; - SelectableChannel ch; - - public SelectionKeyImpl(SelectableChannel ch, - SelectorImpl impl, - int fd) - { - this.ch = ch; - this.impl = impl; - this.fd = fd; - } - - - public SelectableChannel channel() - { - return ch; - } - - - public int readyOps() - { - return 0; - } - - public int interestOps() - { - return ops; - } - - public SelectionKey interestOps(int ops) - { - this.ops = ops; - return this; - } + int fd, ops; + SelectorImpl impl; + SelectableChannel ch; + + public SelectionKeyImpl(SelectableChannel ch, SelectorImpl impl, int fd) + { + this.ch = ch; + this.impl = impl; + this.fd = fd; + } + + public SelectableChannel channel () + { + return ch; + } + + public int readyOps () + { + return 0; + } + + public int interestOps () + { + return ops; + } + + public SelectionKey interestOps (int ops) + { + this.ops = ops; + return this; + } - public Selector selector() - { - return impl; - } + public Selector selector () + { + return impl; + } } diff --git a/gnu/java/nio/SelectorImpl.java b/gnu/java/nio/SelectorImpl.java index 3e78f6abe..0df173bea 100644 --- a/gnu/java/nio/SelectorImpl.java +++ b/gnu/java/nio/SelectorImpl.java @@ -37,153 +37,152 @@ exception statement from your version. */ package gnu.java.nio; -import java.util.*; -import java.nio.channels.*; -import java.nio.channels.spi.*; - +import java.nio.channels.SelectableChannel; +import java.nio.channels.SelectionKey; +import java.nio.channels.Selector; +import java.nio.channels.spi.AbstractSelectableChannel; +import java.nio.channels.spi.AbstractSelector; +import java.nio.channels.spi.SelectorProvider; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; public class SelectorImpl extends AbstractSelector { - Set keys, selected, canceled; + Set keys, selected, canceled; - public SelectorImpl(SelectorProvider provider) - { - super(provider); - } + public SelectorImpl (SelectorProvider provider) + { + super (provider); + } - public Set keys() - { - return keys; - } + public Set keys () + { + return keys; + } - public int selectNow() { return select(1); } - public int select() { return select(Long.MAX_VALUE); } - - private static native int java_do_select(int []read, - int []write, - int []except, - long timeout); - - public int select(long timeout) - { - if (keys == null) + public int selectNow () + { + return select (1); + } + public int select () + { + return select (Long.MAX_VALUE); + } + +// private static native int java_do_select(int[] read, int[] write, +// int[] except, long timeout); + + private static int java_do_select (int[] read, int[] write, + int[] except, long timeout) + { + return 0; + } + + public int select (long timeout) + { + if (keys == null) { - return 0; + return 0; } - int [] read = new int[keys.size()]; - int [] write = new int[keys.size()]; - int [] except = new int[keys.size()]; - int i = 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 (); - Iterator it = keys.iterator(); - while (it.hasNext()) + while (it.hasNext ()) { - SelectionKeyImpl k = (SelectionKeyImpl) it.next(); - - read[i] = k.fd; - write[i] = k.fd; - except[i] = k.fd; - - i++; + 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()) + int ret = java_do_select (read, write, except, timeout); + it = keys.iterator (); + + while (it.hasNext ()) { - SelectionKeyImpl k = (SelectionKeyImpl) it.next(); + SelectionKeyImpl k = (SelectionKeyImpl) it.next (); - if (read[i] != -1 || - write[i] != -1 || - except[i] != -1) - { - add_selected(k); - } + if (read[i] != -1 || + write[i] != -1 || + except[i] != -1) + { + add_selected (k); + } - i++; + i++; } - return ret; - } - - - public Set selectedKeys() { return selected; } - - public Selector wakeup() - { - return null; - } - - public 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() - { - } + return ret; + } + + public Set selectedKeys () + { + return selected; + } + + public Selector wakeup () + { + return null; + } + + public 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) - { - /* + 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 gnu.java.nio.FileChannelImpl) - { - FileChannelImpl fc = (FileChannelImpl) ch; - - SelectionKeyImpl impl = new SelectionKeyImpl(ch, - this, - fc.fd); - - keys.add(impl); - - return impl; - } - else - */ + if (ch instanceof FileChannelImpl) + { + FileChannelImpl fc = (FileChannelImpl) ch; + SelectionKeyImpl impl = new SelectionKeyImpl (ch, this, fc.fd); + keys.add (impl); + return impl; + } + else + */ - if (ch instanceof gnu.java.nio.SocketChannelImpl) + if (ch instanceof SocketChannelImpl) { - SocketChannelImpl fc = (SocketChannelImpl) ch; - - SelectionKeyImpl impl = new SelectionKeyImpl(ch, - this, - fc.fd); - add(impl); - - return impl; + SocketChannelImpl fc = (SocketChannelImpl) ch; + SelectionKeyImpl impl = new SelectionKeyImpl (ch, this, fc.fd); + add (impl); + return impl; } - else + else { - System.err.println("INTERNAL ERROR, no known channel type"); + System.err.println ("INTERNAL ERROR, no known channel type"); } - return null; - } - + return null; + } } diff --git a/gnu/java/nio/SelectorProviderImpl.java b/gnu/java/nio/SelectorProviderImpl.java index b88081052..3fedfc735 100644 --- a/gnu/java/nio/SelectorProviderImpl.java +++ b/gnu/java/nio/SelectorProviderImpl.java @@ -37,38 +37,41 @@ exception statement from your version. */ package gnu.java.nio; -import java.nio.channels.spi.*; -import java.nio.channels.*; - +import java.nio.channels.DatagramChannel; +import java.nio.channels.Pipe; +import java.nio.channels.ServerSocketChannel; +import java.nio.channels.SocketChannel; +import java.nio.channels.spi.SelectorProvider; +import java.nio.channels.spi.AbstractSelector; public class SelectorProviderImpl extends SelectorProvider { - public SelectorProviderImpl() - { - } + public SelectorProviderImpl () + { + } - public DatagramChannel openDatagramChannel() - { - return new DatagramChannelImpl(this); - } + public DatagramChannel openDatagramChannel () + { + return new DatagramChannelImpl (this); + } - public Pipe openPipe() - { - return new PipeImpl(); - } + public Pipe openPipe () + { + return new PipeImpl (); + } - public AbstractSelector openSelector() - { - return new SelectorImpl(this); - } + public AbstractSelector openSelector () + { + return new SelectorImpl (this); + } - public ServerSocketChannel openServerSocketChannel() - { - return new ServerSocketChannelImpl(this); - } + public ServerSocketChannel openServerSocketChannel () + { + return new ServerSocketChannelImpl (this); + } - public SocketChannel openSocketChannel() - { - return new SocketChannelImpl(this); - } + public SocketChannel openSocketChannel () + { + return new SocketChannelImpl (this); + } } diff --git a/gnu/java/nio/ServerSocketChannelImpl.java b/gnu/java/nio/ServerSocketChannelImpl.java index d71262f9a..975a6fc1a 100644 --- a/gnu/java/nio/ServerSocketChannelImpl.java +++ b/gnu/java/nio/ServerSocketChannelImpl.java @@ -37,64 +37,74 @@ exception statement from your version. */ package gnu.java.nio; -import java.io.*; -import java.nio.channels.spi.*; -import java.nio.channels.*; -import java.net.*; +import java.net.InetSocketAddress; +import java.net.ServerSocket; +import java.net.SocketAddress; +import java.nio.channels.ServerSocketChannel; +import java.nio.channels.SocketChannel; +import java.nio.channels.spi.SelectorProvider; 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(); - } + 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); +*/ + + private static int NioSocketAccept (ServerSocketChannelImpl server, + SocketChannelImpl s) + { + return 0; + } + + protected ServerSocketChannelImpl (SelectorProvider provider) + { + super (provider); + fd = SocketChannelImpl.SocketCreate (); + } - public void finalizer() - { - if (connected) + public void finalizer() + { + if (connected) { - try { - close(); - } catch (Exception e) { - } + try + { + close(); + } + catch (Exception e) + { + } } - } - - protected void implCloseSelectableChannel() - { - connected = false; - SocketChannelImpl.SocketClose(fd); - fd = SocketChannelImpl.SocketCreate(); - } - - protected void implConfigureBlocking(boolean block) - { - } - - public SocketChannel accept() - { - SocketChannelImpl result = new SocketChannelImpl(provider()); - - result.sa = new InetSocketAddress(0); - - int res = NioSocketAccept(this, result); - - return result; - } - - public ServerSocket socket() - { - return null; - } + } + + protected void implCloseSelectableChannel () + { + connected = false; + SocketChannelImpl.SocketClose (fd); + fd = SocketChannelImpl.SocketCreate (); + } + + protected void implConfigureBlocking (boolean block) + { + } + + public SocketChannel accept () + { + SocketChannelImpl result = new SocketChannelImpl (provider ()); + result.sa = new InetSocketAddress (0); + int res = NioSocketAccept (this, result); + return result; + } + + public ServerSocket socket () + { + return sock_object; + } } diff --git a/gnu/java/nio/SocketChannelImpl.java b/gnu/java/nio/SocketChannelImpl.java index 970ef559e..dd24b28c8 100644 --- a/gnu/java/nio/SocketChannelImpl.java +++ b/gnu/java/nio/SocketChannelImpl.java @@ -37,195 +37,202 @@ exception statement from your version. */ package gnu.java.nio; -import java.net.*; -import java.io.*; -import java.nio.*; -import java.nio.channels.*; -import java.nio.channels.spi.*; +import java.io.IOException; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.Socket; +import java.net.SocketAddress; +import java.nio.ByteBuffer; +import java.nio.channels.AlreadyConnectedException; +import java.nio.channels.SocketChannel; +import java.nio.channels.spi.SelectorProvider; import gnu.classpath.Configuration; 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); - - public SocketChannelImpl(SelectorProvider provider) - { - super(provider); - - fd = SocketCreate(); + 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); +*/ + + static int SocketCreate() { return 0; }; + static int SocketConnect(int fd, InetAddress a, int port) { return 0; }; + static int SocketBind(int fd, InetAddress host, int port) { return 0; }; + static int SocketListen(int fd, int backlog) { return 0; }; + static int SocketAvailable(int fd) { return 0; }; + static int SocketClose(int fd) { return 0; }; + static int SocketRead(int fd, byte b[], int off, int len) { return 0; }; + static int SocketWrite(int fd, byte b[], int off, int len) { return 0; }; + + public SocketChannelImpl(SelectorProvider provider) + { + super(provider); + + fd = SocketCreate(); - if (fd == -1) + if (fd == -1) { - System.err.println("failed to create socket:"+fd); + System.err.println("failed to create socket:"+fd); } - - //System.out.println("socket-channel:"+fd); - } + } - public void finalizer() - { - if (connected) + public void finalizer() + { + if (connected) { - try { - close(); - } catch (Exception e) { - } + try + { + close(); + } + catch (Exception e) + { + } } - } - - protected void implCloseSelectableChannel() - { - connected = false; - SocketClose(fd); - fd = SocketCreate(); - } - - protected void implConfigureBlocking(boolean block) - { - if (blocking == block) + } + + protected void implCloseSelectableChannel() + { + connected = false; + SocketClose(fd); + fd = SocketCreate(); + } + + protected void implConfigureBlocking(boolean block) + { + if (blocking == block) return; - } + } - public boolean connect(SocketAddress remote) - throws IOException - { - if (connected) + public boolean connect(SocketAddress remote) + throws IOException + { + if (connected) { - throw new AlreadyConnectedException(); + throw new AlreadyConnectedException(); } - // ok, lets connect ! + // ok, lets connect ! - sa = (InetSocketAddress) remote; - - InetAddress addr = sa.getAddress(); - int port = sa.getPort(); - - // System.out.println("CONNECT: " + addr + ","+port); + sa = (InetSocketAddress) remote; - int err = SocketConnect(fd, addr, port); + InetAddress addr = sa.getAddress(); + int port = sa.getPort(); + int err = SocketConnect(fd, addr, port); - if (err < 0) + if (err < 0) { - throw new IOException("Connection refused:"+err + ", connect="+err); + throw new IOException("Connection refused:"+err + ", connect="+err); } - local_port = err; + local_port = err; - connected = true; + connected = true; - return blocking; - } + return blocking; + } - public boolean finishConnect() - { - return false; - } - - public boolean isConnected() - { - return connected; - } + public boolean finishConnect() + { + return false; + } + + public boolean isConnected() + { + return connected; + } - public boolean isConnectionPending() - { - if (blocking) + public boolean isConnectionPending() + { + if (blocking) return false; - return false; - } + + return false; + } - public Socket socket() - { - if (sock_object != null) + public Socket socket() + { + if (sock_object != null) { - //sock_object.ch = this; + //sock_object.ch = this; } - return sock_object; - } + return sock_object; + } - public int read(ByteBuffer dst) - { - int bytes = 0; - - int len = 1024; - byte[]b = new byte[len]; + public 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); + bytes = SocketRead(fd, b, 0, len); - dst.put(b, 0, bytes); + dst.put(b, 0, bytes); - if (bytes == 0) + if (bytes == 0) { - // we've hit eof ? - return -1; + // we've hit eof ? + return -1; } - return bytes; - } + return bytes; + } - public long read(ByteBuffer[] dsts, int offset, int length) - { - long bytes = 0; - for (int i=offset; i<length; i++) + public long read(ByteBuffer[] dsts, int offset, int length) + { + long bytes = 0; + + for (int i=offset; i<length; i++) { - bytes += read(dsts[i]); + bytes += read(dsts[i]); } - return bytes; - } + + return bytes; + } - public int write(ByteBuffer src) - { - int bytes = 0; - - int len = src.position(); + public int write(ByteBuffer src) + { + int bytes = 0; + int len = src.position(); - if (src instanceof ByteBufferImpl) + if (src instanceof ByteBufferImpl) { - ByteBufferImpl bi = (ByteBufferImpl) src; - byte[]b = bi.array(); - bytes = SocketWrite(fd, b, 0, len); - - //System.out.println("reused memory buffer...."); + ByteBufferImpl bi = (ByteBufferImpl) src; + byte[]b = bi.array(); + bytes = SocketWrite(fd, b, 0, len); } - else + else { - byte[]b = new byte[len]; - src.get(b, 0, len); - bytes = SocketWrite(fd, b, 0, len); + 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; - } + return bytes; + } - public long write(ByteBuffer[] srcs, int offset, int length) - { - long bytes = 0; - for (int i=offset; i<length; i++) + public long write (ByteBuffer[] srcs, int offset, int length) + { + long bytes = 0; + for (int i=offset; i<length; i++) { - bytes += write(srcs[i]); + bytes += write(srcs[i]); } - return bytes; - } + return bytes; + } } |