001 /* SocketChannel.java --
002 Copyright (C) 2002, 2004 Free Software Foundation, Inc.
003
004 This file is part of GNU Classpath.
005
006 GNU Classpath is free software; you can redistribute it and/or modify
007 it under the terms of the GNU General Public License as published by
008 the Free Software Foundation; either version 2, or (at your option)
009 any later version.
010
011 GNU Classpath is distributed in the hope that it will be useful, but
012 WITHOUT ANY WARRANTY; without even the implied warranty of
013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 General Public License for more details.
015
016 You should have received a copy of the GNU General Public License
017 along with GNU Classpath; see the file COPYING. If not, write to the
018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019 02110-1301 USA.
020
021 Linking this library statically or dynamically with other modules is
022 making a combined work based on this library. Thus, the terms and
023 conditions of the GNU General Public License cover the whole
024 combination.
025
026 As a special exception, the copyright holders of this library give you
027 permission to link this library with independent modules to produce an
028 executable, regardless of the license terms of these independent
029 modules, and to copy and distribute the resulting executable under
030 terms of your choice, provided that you also meet, for each linked
031 independent module, the terms and conditions of the license of that
032 module. An independent module is a module which is not derived from
033 or based on this library. If you modify this library, you may extend
034 this exception to your version of the library, but you are not
035 obligated to do so. If you do not wish to do so, delete this
036 exception statement from your version. */
037
038
039 package java.nio.channels;
040
041 import java.io.IOException;
042 import java.net.Socket;
043 import java.net.SocketAddress;
044 import java.nio.ByteBuffer;
045 import java.nio.channels.spi.AbstractSelectableChannel;
046 import java.nio.channels.spi.SelectorProvider;
047
048 /**
049 * @author Michael Koch (konqueror@gmx.de)
050 * @since 1.4
051 */
052 public abstract class SocketChannel extends AbstractSelectableChannel
053 implements ByteChannel, ScatteringByteChannel, GatheringByteChannel
054 {
055 /**
056 * Initializes this socket channel.
057 */
058 protected SocketChannel(SelectorProvider provider)
059 {
060 super(provider);
061 }
062
063 /**
064 * Opens a socket channel.
065 *
066 * @return the new <code>SocketChannel</code> object
067 *
068 * @exception IOException If an error occurs
069 */
070 public static SocketChannel open() throws IOException
071 {
072 return SelectorProvider.provider().openSocketChannel();
073 }
074
075 /**
076 * Opens a channel and connects it to a remote address.
077 *
078 * @return the new <code>SocketChannel</code> object
079 *
080 * @exception AsynchronousCloseException If this channel is already connected.
081 * @exception ClosedByInterruptException If another thread interrupts the
082 * current thread while the connect operation is in progress, thereby closing
083 * the channel and setting the current thread's interrupt status.
084 * @exception IOException If an error occurs
085 * @exception SecurityException If a security manager has been installed and
086 * it does not permit access to the given remote endpoint.
087 * @exception UnresolvedAddressException If the given remote address is not
088 * fully resolved.
089 * @exception UnsupportedAddressTypeException If the type of the given remote
090 * address is not supported.
091 */
092 public static SocketChannel open(SocketAddress remote)
093 throws IOException
094 {
095 SocketChannel ch = open();
096 ch.connect(remote);
097 return ch;
098 }
099
100 /**
101 * Reads data from the channel.
102 *
103 * @return the number of bytes read, zero is valid too, -1 if end of stream
104 * is reached
105 *
106 * @exception IOException If an error occurs
107 * @exception NotYetConnectedException If this channel is not yet connected.
108 */
109 public final long read(ByteBuffer[] dsts) throws IOException
110 {
111 long b = 0;
112
113 for (int i = 0; i < dsts.length; i++)
114 b += read(dsts[i]);
115
116 return b;
117 }
118
119 /**
120 * Writes data to the channel.
121 *
122 * @return the number of bytes written, zero is valid too
123 *
124 * @exception IOException If an error occurs
125 * @exception NotYetConnectedException If this channel is not yet connected.
126 */
127 public final long write(ByteBuffer[] dsts) throws IOException
128 {
129 long b = 0;
130
131 for (int i = 0; i < dsts.length; i++)
132 b += write(dsts[i]);
133
134 return b;
135 }
136
137 /**
138 * Retrieves the valid operations for this channel.
139 *
140 * @return the valid operations
141 */
142 public final int validOps()
143 {
144 return SelectionKey.OP_CONNECT | SelectionKey.OP_READ
145 | SelectionKey.OP_WRITE;
146 }
147
148 /**
149 * Reads data from the channel.
150 *
151 * @return the number of bytes read, zero is valid too, -1 if end of stream
152 * is reached
153 *
154 * @exception IOException If an error occurs
155 * @exception NotYetConnectedException If this channel is not yet connected.
156 */
157 public abstract int read(ByteBuffer dst) throws IOException;
158
159 /**
160 * Connects the channel's socket to the remote address.
161 *
162 * @return <code>true</code> if the channel got successfully connected,
163 * <code>false</code> if the channel is in non-blocking mode and connection
164 * operation is still in progress.
165 *
166 * @exception AlreadyConnectedException If this channel is already connected.
167 * @exception AsynchronousCloseException If this channel is already connected.
168 * @exception ClosedByInterruptException If another thread interrupts the
169 * current thread while the connect operation is in progress, thereby closing
170 * the channel and setting the current thread's interrupt status.
171 * @exception ClosedChannelException If this channel is closed.
172 * @exception ConnectionPendingException If a non-blocking connection
173 * operation is already in progress on this channel.
174 * @exception IOException If an error occurs
175 * @exception SecurityException If a security manager has been installed and
176 * it does not permit access to the given remote endpoint.
177 * @exception UnresolvedAddressException If the given remote address is not
178 * fully resolved.
179 * @exception UnsupportedAddressTypeException If the type of the given remote
180 * address is not supported.
181 */
182 public abstract boolean connect(SocketAddress remote)
183 throws IOException;
184
185 /**
186 * Finishes the process of connecting a socket channel.
187 *
188 * @exception AsynchronousCloseException If this channel is already connected.
189 * @exception ClosedByInterruptException If another thread interrupts the
190 * current thread while the connect operation is in progress, thereby closing
191 * the channel and setting the current thread's interrupt status.
192 * @exception ClosedChannelException If this channel is closed.
193 * @exception IOException If an error occurs
194 * @exception NoConnectionPendingException If this channel is not connected
195 * and a connection operation has not been initiated.
196 */
197 public abstract boolean finishConnect() throws IOException;
198
199 /**
200 * Tells whether or not the channel's socket is connected.
201 */
202 public abstract boolean isConnected();
203
204 /**
205 * Tells whether or not a connection operation is in progress on this channel.
206 */
207 public abstract boolean isConnectionPending();
208
209 /**
210 * Reads data from the channel.
211 *
212 * @return the number of bytes read, zero is valid too, -1 if end of stream
213 * is reached
214 *
215 * @exception IOException If an error occurs
216 * @exception NotYetConnectedException If this channel is not yet connected.
217 */
218 public abstract long read(ByteBuffer[] dsts, int offset, int length)
219 throws IOException;
220
221 /**
222 * Retrieves the channel's socket.
223 *
224 * @return the socket
225 */
226 public abstract Socket socket();
227
228 /**
229 * Writes data to the channel.
230 *
231 * @return the number of bytes written, zero is valid too
232 *
233 * @exception IOException If an error occurs
234 * @exception NotYetConnectedException If this channel is not yet connected.
235 */
236 public abstract int write(ByteBuffer src) throws IOException;
237
238 /**
239 * Writes data to the channel.
240 *
241 * @return the number of bytes written, zero is valid too
242 *
243 * @exception IOException If an error occurs
244 * @exception NotYetConnectedException If this channel is not yet connected.
245 */
246 public abstract long write(ByteBuffer[] srcs, int offset, int length)
247 throws IOException;
248 }