| 1 | /* SocketChannel.java --
|
|---|
| 2 | Copyright (C) 2002 Free Software Foundation, Inc.
|
|---|
| 3 |
|
|---|
| 4 | This file is part of GNU Classpath.
|
|---|
| 5 |
|
|---|
| 6 | GNU Classpath is free software; you can redistribute it and/or modify
|
|---|
| 7 | it under the terms of the GNU General Public License as published by
|
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option)
|
|---|
| 9 | any later version.
|
|---|
| 10 |
|
|---|
| 11 | GNU Classpath is distributed in the hope that it will be useful, but
|
|---|
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 14 | General Public License for more details.
|
|---|
| 15 |
|
|---|
| 16 | You should have received a copy of the GNU General Public License
|
|---|
| 17 | along with GNU Classpath; see the file COPYING. If not, write to the
|
|---|
| 18 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|---|
| 19 | 02111-1307 USA.
|
|---|
| 20 |
|
|---|
| 21 | Linking this library statically or dynamically with other modules is
|
|---|
| 22 | making a combined work based on this library. Thus, the terms and
|
|---|
| 23 | conditions of the GNU General Public License cover the whole
|
|---|
| 24 | combination.
|
|---|
| 25 |
|
|---|
| 26 | As a special exception, the copyright holders of this library give you
|
|---|
| 27 | permission to link this library with independent modules to produce an
|
|---|
| 28 | executable, regardless of the license terms of these independent
|
|---|
| 29 | modules, and to copy and distribute the resulting executable under
|
|---|
| 30 | terms of your choice, provided that you also meet, for each linked
|
|---|
| 31 | independent module, the terms and conditions of the license of that
|
|---|
| 32 | module. An independent module is a module which is not derived from
|
|---|
| 33 | or based on this library. If you modify this library, you may extend
|
|---|
| 34 | this exception to your version of the library, but you are not
|
|---|
| 35 | obligated to do so. If you do not wish to do so, delete this
|
|---|
| 36 | exception statement from your version. */
|
|---|
| 37 |
|
|---|
| 38 | package java.nio.channels;
|
|---|
| 39 |
|
|---|
| 40 | import java.nio.channels.spi.AbstractSelectableChannel;
|
|---|
| 41 | import java.nio.channels.spi.SelectorProvider;
|
|---|
| 42 | import java.nio.ByteBuffer;
|
|---|
| 43 | import java.io.IOException;
|
|---|
| 44 | import java.net.Socket;
|
|---|
| 45 | import java.net.SocketAddress;
|
|---|
| 46 |
|
|---|
| 47 | /**
|
|---|
| 48 | * @author Michael Koch
|
|---|
| 49 | * @since 1.4
|
|---|
| 50 | */
|
|---|
| 51 | abstract public class SocketChannel extends AbstractSelectableChannel
|
|---|
| 52 | implements ByteChannel, ScatteringByteChannel, GatheringByteChannel
|
|---|
| 53 | {
|
|---|
| 54 | /**
|
|---|
| 55 | * Initializes this socket.
|
|---|
| 56 | */
|
|---|
| 57 | protected SocketChannel (SelectorProvider provider)
|
|---|
| 58 | {
|
|---|
| 59 | super (provider);
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | /**
|
|---|
| 63 | * Opens a socket channel.
|
|---|
| 64 | *
|
|---|
| 65 | * @exception IOException If an error occurs
|
|---|
| 66 | */
|
|---|
| 67 | public static SocketChannel open () throws IOException
|
|---|
| 68 | {
|
|---|
| 69 | return SelectorProvider.provider ().openSocketChannel ();
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | /**
|
|---|
| 73 | * Opens a channel and connects it to a remote address.
|
|---|
| 74 | *
|
|---|
| 75 | * @exception AsynchronousCloseException If this channel is already connected.
|
|---|
| 76 | * @exception ClosedByInterruptException If another thread interrupts the
|
|---|
| 77 | * current thread while the connect operation is in progress, thereby closing
|
|---|
| 78 | * the channel and setting the current thread's interrupt status.
|
|---|
| 79 | * @exception IOException If an error occurs
|
|---|
| 80 | * @exception SecurityException If a security manager has been installed and
|
|---|
| 81 | * it does not permit access to the given remote endpoint.
|
|---|
| 82 | * @exception UnresolvedAddressException If the given remote address is not
|
|---|
| 83 | * fully resolved.
|
|---|
| 84 | * @exception UnsupportedAddressTypeException If the type of the given remote
|
|---|
| 85 | * address is not supported.
|
|---|
| 86 | */
|
|---|
| 87 | public static SocketChannel open (SocketAddress remote) throws IOException
|
|---|
| 88 | {
|
|---|
| 89 | SocketChannel ch = open ();
|
|---|
| 90 |
|
|---|
| 91 | if (ch.connect (remote))
|
|---|
| 92 | {
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | return ch;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | /**
|
|---|
| 99 | * Reads data from the channel.
|
|---|
| 100 | *
|
|---|
| 101 | * @exception IOException If an error occurs
|
|---|
| 102 | * @exception NotYetConnectedException If this channel is not yet connected.
|
|---|
| 103 | */
|
|---|
| 104 | public final long read (ByteBuffer[] dsts) throws IOException
|
|---|
| 105 | {
|
|---|
| 106 | long b = 0;
|
|---|
| 107 |
|
|---|
| 108 | for (int i = 0; i < dsts.length; i++)
|
|---|
| 109 | {
|
|---|
| 110 | b += read (dsts [i]);
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | return b;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | /**
|
|---|
| 117 | * Writes data to the channel.
|
|---|
| 118 | *
|
|---|
| 119 | * @exception IOException If an error occurs
|
|---|
| 120 | * @exception NotYetConnectedException If this channel is not yet connected.
|
|---|
| 121 | */
|
|---|
| 122 | public final long write (ByteBuffer[] dsts) throws IOException
|
|---|
| 123 | {
|
|---|
| 124 | long b = 0;
|
|---|
| 125 |
|
|---|
| 126 | for (int i= 0; i < dsts.length; i++)
|
|---|
| 127 | {
|
|---|
| 128 | b += write (dsts [i]);
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | return b;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | /**
|
|---|
| 135 | * Retrieves the valid operations for this channel.
|
|---|
| 136 | */
|
|---|
| 137 | public final int validOps ()
|
|---|
| 138 | {
|
|---|
| 139 | return SelectionKey.OP_CONNECT | SelectionKey.OP_READ | SelectionKey.OP_WRITE;
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | /**
|
|---|
| 143 | * Reads data from the channel.
|
|---|
| 144 | *
|
|---|
| 145 | * @exception IOException If an error occurs
|
|---|
| 146 | * @exception NotYetConnectedException If this channel is not yet connected.
|
|---|
| 147 | */
|
|---|
| 148 | public abstract int read (ByteBuffer dst) throws IOException;
|
|---|
| 149 |
|
|---|
| 150 | /**
|
|---|
| 151 | * Connects the channel's socket to the remote address.
|
|---|
| 152 | *
|
|---|
| 153 | * @exception AlreadyConnectedException If this channel is already connected.
|
|---|
| 154 | * @exception AsynchronousCloseException If this channel is already connected.
|
|---|
| 155 | * @exception ClosedByInterruptException If another thread interrupts the
|
|---|
| 156 | * current thread while the connect operation is in progress, thereby closing
|
|---|
| 157 | * the channel and setting the current thread's interrupt status.
|
|---|
| 158 | * @exception ClosedChannelException If this channel is closed.
|
|---|
| 159 | * @exception ConnectionPendingException If a non-blocking connection
|
|---|
| 160 | * operation is already in progress on this channel.
|
|---|
| 161 | * @exception IOException If an error occurs
|
|---|
| 162 | * @exception SecurityException If a security manager has been installed and
|
|---|
| 163 | * it does not permit access to the given remote endpoint.
|
|---|
| 164 | * @exception UnresolvedAddressException If the given remote address is not
|
|---|
| 165 | * fully resolved.
|
|---|
| 166 | * @exception UnsupportedAddressTypeException If the type of the given remote
|
|---|
| 167 | * address is not supported.
|
|---|
| 168 | */
|
|---|
| 169 | public abstract boolean connect (SocketAddress remote) throws IOException;
|
|---|
| 170 |
|
|---|
| 171 | /**
|
|---|
| 172 | * Finishes the process of connecting a socket channel.
|
|---|
| 173 | *
|
|---|
| 174 | * @exception AsynchronousCloseException If this channel is already connected.
|
|---|
| 175 | * @exception ClosedByInterruptException If another thread interrupts the
|
|---|
| 176 | * current thread while the connect operation is in progress, thereby closing
|
|---|
| 177 | * the channel and setting the current thread's interrupt status.
|
|---|
| 178 | * @exception ClosedChannelException If this channel is closed.
|
|---|
| 179 | * @exception IOException If an error occurs
|
|---|
| 180 | * @exception NoConnectionPendingException If this channel is not connected
|
|---|
| 181 | * and a connection operation has not been initiated.
|
|---|
| 182 | */
|
|---|
| 183 | public abstract boolean finishConnect () throws IOException;
|
|---|
| 184 |
|
|---|
| 185 | /**
|
|---|
| 186 | * Tells whether or not the channel's socket is connected.
|
|---|
| 187 | */
|
|---|
| 188 | public abstract boolean isConnected ();
|
|---|
| 189 |
|
|---|
| 190 | /**
|
|---|
| 191 | * Tells whether or not a connection operation is in progress on this channel.
|
|---|
| 192 | */
|
|---|
| 193 | public abstract boolean isConnectionPending ();
|
|---|
| 194 |
|
|---|
| 195 | /**
|
|---|
| 196 | * Reads data from the channel.
|
|---|
| 197 | *
|
|---|
| 198 | * @exception IOException If an error occurs
|
|---|
| 199 | * @exception NotYetConnectedException If this channel is not yet connected.
|
|---|
| 200 | */
|
|---|
| 201 | public abstract long read (ByteBuffer[] dsts, int offset, int length)
|
|---|
| 202 | throws IOException;
|
|---|
| 203 |
|
|---|
| 204 | /**
|
|---|
| 205 | * Retrieves the channel's socket.
|
|---|
| 206 | */
|
|---|
| 207 | public abstract Socket socket ();
|
|---|
| 208 |
|
|---|
| 209 | /**
|
|---|
| 210 | * Writes data to the channel.
|
|---|
| 211 | *
|
|---|
| 212 | * @exception IOException If an error occurs
|
|---|
| 213 | * @exception NotYetConnectedException If this channel is not yet connected.
|
|---|
| 214 | */
|
|---|
| 215 | public abstract int write (ByteBuffer src) throws IOException;
|
|---|
| 216 |
|
|---|
| 217 | /**
|
|---|
| 218 | * Writes data to the channel.
|
|---|
| 219 | *
|
|---|
| 220 | * @exception IOException If an error occurs
|
|---|
| 221 | * @exception NotYetConnectedException If this channel is not yet connected.
|
|---|
| 222 | */
|
|---|
| 223 | public abstract long write (ByteBuffer[] srcs, int offset, int length)
|
|---|
| 224 | throws IOException;
|
|---|
| 225 | }
|
|---|