diff options
Diffstat (limited to 'libjava/java/nio/channels')
-rw-r--r-- | libjava/java/nio/channels/DatagramChannel.java | 105 | ||||
-rw-r--r-- | libjava/java/nio/channels/ServerSocketChannel.java | 37 | ||||
-rw-r--r-- | libjava/java/nio/channels/SocketChannel.java | 121 | ||||
-rw-r--r-- | libjava/java/nio/channels/spi/AbstractChannel.java | 57 | ||||
-rw-r--r-- | libjava/java/nio/channels/spi/AbstractSelectableChannel.java | 163 |
5 files changed, 417 insertions, 66 deletions
diff --git a/libjava/java/nio/channels/DatagramChannel.java b/libjava/java/nio/channels/DatagramChannel.java index 301f33e20a0..eefa443cb29 100644 --- a/libjava/java/nio/channels/DatagramChannel.java +++ b/libjava/java/nio/channels/DatagramChannel.java @@ -37,14 +37,117 @@ exception statement from your version. */ package java.nio.channels; +import java.io.IOException; +import java.net.DatagramSocket; +import java.net.SocketAddress; +import java.nio.ByteBuffer; import java.nio.channels.spi.AbstractSelectableChannel; import java.nio.channels.spi.SelectorProvider; +/** + * @since 1.4 + */ public abstract class DatagramChannel extends AbstractSelectableChannel + implements ByteChannel, ScatteringByteChannel, GatheringByteChannel { - public DatagramChannel (SelectorProvider provider) + /** + * Initializes the channel. + */ + protected DatagramChannel (SelectorProvider provider) { super (provider); } + + /** + * Opens a datagram channel. + */ + public static DatagramChannel open () throws IOException + { + return SelectorProvider.provider ().openDatagramChannel (); + } + + /** + * Reads data from this channel. + */ + public final long read (ByteBuffer[] dsts) throws IOException + { + long b = 0; + + for (int i = 0; i < dsts.length; i++) + b += read (dsts[i]); + + return b; + } + + /** + * Writes data to this channel. + */ + public final long write (ByteBuffer[] srcs) + { + long b = 0; + + for (int i = 0;i < srcs.length; i++) + b += write (srcs[i]); + + return b; + } + + /** + * Connects this channel's socket. + */ + public abstract DatagramChannel connect (SocketAddress remote); + + /** + * Disonnects this channel's socket. + */ + public abstract DatagramChannel disconnect (); + + /** + * Tells whether or not this channel's socket is connected. + */ + public abstract boolean isConnected (); + + /** + * Reads data from this channel. + */ + public abstract int read (ByteBuffer dst); + + /** + * Reads data from this channel. + */ + public abstract long read (ByteBuffer[] dsts, int offset, int length); + + /** + * Receives a datagram via this channel. + */ + public abstract SocketAddress receive (ByteBuffer dst); + + /** + * Sends a datagram via this channel. + */ + public abstract int send (ByteBuffer src, SocketAddress target); + + /** + * Retrieves the channel's socket. + */ + public abstract DatagramSocket socket (); + + /** + * Writes data to this channel. + */ + public abstract int write (ByteBuffer src); + + /** + * Writes data to this channel. + */ + public abstract long write (ByteBuffer[] srcs, int offset, int length); + + /** + * Retrieves the valid operations for this channel. + */ + public final int validOps () + { + return SelectionKey.OP_READ | SelectionKey.OP_WRITE; + } } diff --git a/libjava/java/nio/channels/ServerSocketChannel.java b/libjava/java/nio/channels/ServerSocketChannel.java index e423bd10b43..ed8d39264df 100644 --- a/libjava/java/nio/channels/ServerSocketChannel.java +++ b/libjava/java/nio/channels/ServerSocketChannel.java @@ -39,12 +39,49 @@ package java.nio.channels; import java.nio.channels.spi.AbstractSelectableChannel; import java.nio.channels.spi.SelectorProvider; +import java.nio.ByteOrder; +import java.nio.ByteBuffer; +import java.io.IOException; +import java.net.ServerSocket; +/** + * @author Michael Koch + * @since 1.4 + */ public abstract class ServerSocketChannel extends AbstractSelectableChannel { + /** + * Initializes this channel. + */ public ServerSocketChannel (SelectorProvider provider) { super (provider); } + + /** + * Accepts a connection made to this channel's socket. + */ + public abstract SocketChannel accept (); + + /** + * Retrieves the channels socket. + */ + public abstract ServerSocket socket (); + + /** + * Opens a server socker channel. + */ + public static ServerSocketChannel open () throws IOException + { + return SelectorProvider.provider ().openServerSocketChannel (); + } + + /** + * Retrieves the valid operations for this channel. + */ + public final int validOps () + { + return SelectionKey.OP_ACCEPT; + } } diff --git a/libjava/java/nio/channels/SocketChannel.java b/libjava/java/nio/channels/SocketChannel.java index dc6e1d2d96a..1d93430947e 100644 --- a/libjava/java/nio/channels/SocketChannel.java +++ b/libjava/java/nio/channels/SocketChannel.java @@ -39,12 +39,127 @@ package java.nio.channels; import java.nio.channels.spi.AbstractSelectableChannel; import java.nio.channels.spi.SelectorProvider; +import java.nio.ByteBuffer; +import java.io.IOException; +import java.net.Socket; +import java.net.SocketAddress; -public abstract class SocketChannel - extends AbstractSelectableChannel +/** + * @author Michael Koch + * @since 1.4 + */ +abstract public class SocketChannel extends AbstractSelectableChannel { - public SocketChannel (SelectorProvider provider) + /** + * Initializes this socket. + */ + protected SocketChannel (SelectorProvider provider) { super (provider); } + + /** + * Opens a socket channel. + */ + public static SocketChannel open () throws IOException + { + return SelectorProvider.provider ().openSocketChannel (); + } + + /** + * Opens a channel and connects it to a remote address. + */ + public static SocketChannel open (SocketAddress remote) throws IOException + { + SocketChannel ch = open (); + + if (ch.connect (remote)) + { + } + + return ch; + } + + /** + * Reads data from the channel. + */ + public final long read (ByteBuffer[] dsts) + { + long b = 0; + + for (int i = 0; i < dsts.length; i++) + { + b += read (dsts [i]); + } + + return b; + } + + /** + * Writes data to the channel. + */ + public final long write (ByteBuffer[] dsts) + { + long b = 0; + + for (int i= 0; i < dsts.length; i++) + { + b += write (dsts [i]); + } + + return b; + } + + /** + * Retrieves the valid operations for this channel. + */ + public final int validOps () + { + return SelectionKey.OP_CONNECT | SelectionKey.OP_READ | SelectionKey.OP_WRITE; + } + + /** + * Reads data from the channel. + */ + public abstract int read (ByteBuffer dst); + + /** + * Connects the channel's socket to the remote address. + */ + public abstract boolean connect (SocketAddress remote) throws IOException; + + /** + * Finishes the process of connecting a socket channel. + */ + public abstract boolean finishConnect (); + + /** + * Tells whether or not the channel's socket is connected. + */ + public abstract boolean isConnected (); + + /** + * Tells whether or not a connection operation is in progress on this channel. + */ + public abstract boolean isConnectionPending (); + + /** + * Reads data from the channel. + */ + public abstract long read (ByteBuffer[] dsts, int offset, int length); + + /** + * Retrieves the channel's socket. + */ + public abstract Socket socket (); + + /** + * Writes data to the channel. + */ + public abstract int write (ByteBuffer src); + + /** + * Writes data to the channel. + */ + public abstract long write (ByteBuffer[] srcs, int offset, int length); } diff --git a/libjava/java/nio/channels/spi/AbstractChannel.java b/libjava/java/nio/channels/spi/AbstractChannel.java deleted file mode 100644 index 3d7fe7ac799..00000000000 --- a/libjava/java/nio/channels/spi/AbstractChannel.java +++ /dev/null @@ -1,57 +0,0 @@ -/* AbstractChannel.java -- - Copyright (C) 2002 Free Software Foundation, Inc. - -This file is part of GNU Classpath. - -GNU Classpath is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2, or (at your option) -any later version. - -GNU Classpath is distributed in the hope that it will be useful, but -WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -General Public License for more details. - -You should have received a copy of the GNU General Public License -along with GNU Classpath; see the file COPYING. If not, write to the -Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA -02111-1307 USA. - -Linking this library statically or dynamically with other modules is -making a combined work based on this library. Thus, the terms and -conditions of the GNU General Public License cover the whole -combination. - -As a special exception, the copyright holders of this library give you -permission to link this library with independent modules to produce an -executable, regardless of the license terms of these independent -modules, and to copy and distribute the resulting executable under -terms of your choice, provided that you also meet, for each linked -independent module, the terms and conditions of the license of that -module. An independent module is a module which is not derived from -or based on this library. If you modify this library, you may extend -this exception to your version of the library, but you are not -obligated to do so. If you do not wish to do so, delete this -exception statement from your version. */ - -package java.nio.channels.spi; - -import java.io.IOException; -import java.nio.channels.Channel; - -public abstract class AbstractChannel implements Channel -{ - boolean opened; - - public boolean isOpen() - { - return opened; - } - - public void close() throws IOException - { - if (! isOpen()) - return; - } -} diff --git a/libjava/java/nio/channels/spi/AbstractSelectableChannel.java b/libjava/java/nio/channels/spi/AbstractSelectableChannel.java index 42bb1f24bb4..433b7294902 100644 --- a/libjava/java/nio/channels/spi/AbstractSelectableChannel.java +++ b/libjava/java/nio/channels/spi/AbstractSelectableChannel.java @@ -1,4 +1,4 @@ -/* AbstractSelectableChannel.java -- +/* AbstractSelectableChannel.java Copyright (C) 2002 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -37,18 +37,171 @@ exception statement from your version. */ package java.nio.channels.spi; +import java.io.IOException; +import java.nio.channels.ClosedChannelException; import java.nio.channels.SelectableChannel; +import java.nio.channels.SelectionKey; +import java.nio.channels.Selector; +import java.util.LinkedList; +import java.util.List; +import java.util.ListIterator; -public abstract class AbstractSelectableChannel - extends SelectableChannel +public abstract class AbstractSelectableChannel extends SelectableChannel { + int registered; + boolean blocking = true; + Object LOCK = new Object (); + SelectorProvider provider; + List keys; + + /** + * Initializes the channel + */ protected AbstractSelectableChannel (SelectorProvider provider) { + this.provider = provider; + } + + /** + * Retrieves the object upon which the configureBlocking and register + * methods synchronize. + */ + public final Object blockingLock () + { + return LOCK; + } + + /** + * Adjusts this channel's blocking mode. + */ + public final SelectableChannel configureBlocking (boolean block) + { + synchronized (LOCK) + { + blocking = true; + implConfigureBlocking (block); + } + + return this; + } + + /** + * Closes this channel. + */ + protected final void implCloseChannel () + { + implCloseSelectableChannel (); } + + /** + * Closes this selectable channel. + */ + protected abstract void implCloseSelectableChannel (); + /** + * Adjusts this channel's blocking mode. + */ + protected abstract void implConfigureBlocking (boolean block); + + /** + * Tells whether or not every I/O operation on this channel will block + * until it completes. + */ public final boolean isBlocking() { - return true; + return blocking; + } + + /** + * Tells whether or not this channel is currently registered with + * any selectors. + */ + public final boolean isRegistered() + { + return registered > 0; + } + + /** + * Retrieves the key representing the channel's registration with the + * given selector. + */ + public final SelectionKey keyFor(Selector selector) + { + try + { + return register (selector, 0, null); + } + catch (Exception e) + { + return null; + } } -} + /** + * Returns the provider that created this channel. + */ + public final SelectorProvider provider () + { + return provider; + } + + private SelectionKey locate (Selector selector) + { + if (keys == null) + return null; + + SelectionKey k = null; + ListIterator it = keys.listIterator (); + + while (it.hasNext ()) + { + k = (SelectionKey) it.next (); + if (k.selector () == selector) + { + return k; + } + } + + return k; + } + + private void add (SelectionKey key) + { + if (keys == null) + keys = new LinkedList (); + + keys.add (key); + } + + /** + * Registers this channel with the given selector, returning a selection key. + */ + public final SelectionKey register (Selector selin, int ops, Object att) + throws ClosedChannelException + { + if (!isOpen ()) + throw new ClosedChannelException(); + + SelectionKey k = null; + AbstractSelector selector = (AbstractSelector) selin; + + synchronized (LOCK) + { + k = locate (selector); + + if (k != null) + { + k.attach (att); + } + else + { + k = selector.register (this, ops, att); + + if (k != null) + add (k); + } + } + + return k; + } +} |