001 /* DatagramChannel.java -- 002 Copyright (C) 2002 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 package java.nio.channels; 039 040 import java.io.IOException; 041 import java.net.DatagramSocket; 042 import java.net.SocketAddress; 043 import java.nio.ByteBuffer; 044 import java.nio.channels.spi.AbstractSelectableChannel; 045 import java.nio.channels.spi.SelectorProvider; 046 047 048 /** 049 * @since 1.4 050 */ 051 public abstract class DatagramChannel extends AbstractSelectableChannel 052 implements ByteChannel, ScatteringByteChannel, GatheringByteChannel 053 { 054 /** 055 * Initializes the channel. 056 */ 057 protected DatagramChannel(SelectorProvider provider) 058 { 059 super(provider); 060 } 061 062 /** 063 * Opens a datagram channel. 064 * 065 * @exception IOException If an error occurs 066 */ 067 public static DatagramChannel open() throws IOException 068 { 069 return SelectorProvider.provider().openDatagramChannel(); 070 } 071 072 /** 073 * Reads data from this channel. 074 */ 075 public final long read(ByteBuffer[] dsts) throws IOException 076 { 077 long b = 0; 078 079 for (int i = 0; i < dsts.length; i++) 080 b += read(dsts[i]); 081 082 return b; 083 } 084 085 /** 086 * Writes data to this channel. 087 * 088 * @exception IOException If an error occurs 089 * @exception NotYetConnectedException The channel's socket is not connected. 090 */ 091 public final long write(ByteBuffer[] srcs) throws IOException 092 { 093 long b = 0; 094 095 for (int i = 0; i < srcs.length; i++) 096 b += write(srcs[i]); 097 098 return b; 099 } 100 101 /** 102 * Connects this channel's socket. 103 * 104 * @exception AsynchronousCloseException If another thread closes this channel 105 * while the connect operation is in progress. 106 * @exception ClosedByInterruptException If another thread interrupts the 107 * current thread while the read operation is in progress, thereby closing the 108 * channel and setting the current thread's interrupt status. 109 * @exception ClosedChannelException If this channel is closed. 110 * @exception IOException If an error occurs. 111 * @exception SecurityException If a security manager has been installed and 112 * it does not permit datagrams to be sent to the given address. 113 */ 114 public abstract DatagramChannel connect(SocketAddress remote) 115 throws IOException; 116 117 /** 118 * Disonnects this channel's socket. 119 * 120 * @exception IOException If an error occurs 121 */ 122 public abstract DatagramChannel disconnect() throws IOException; 123 124 /** 125 * Tells whether or not this channel's socket is connected. 126 * 127 * @exception NotYetConnectedException The channel's socket is not connected. 128 */ 129 public abstract boolean isConnected(); 130 131 /** 132 * Reads data from this channel. 133 */ 134 public abstract int read(ByteBuffer dst) throws IOException; 135 136 /** 137 * Reads data from this channel. 138 * 139 * @exception IOException If an error occurs. 140 * @exception NotYetConnectedException The channel's socket is not connected. 141 */ 142 public abstract long read(ByteBuffer[] dsts, int offset, int length) 143 throws IOException; 144 145 /** 146 * Receives a datagram via this channel. 147 * 148 * @exception AsynchronousCloseException If another thread closes this channel 149 * while the connect operation is in progress. 150 * @exception ClosedByInterruptException If another thread interrupts the 151 * current thread while the read operation is in progress, thereby closing the 152 * channel and setting the current thread's interrupt status. 153 * @exception ClosedChannelException If this channel is closed. 154 * @exception IOException If an error occurs 155 * @exception SecurityException If a security manager has been installed and 156 * it does not permit datagrams to be sent to the given address. 157 */ 158 public abstract SocketAddress receive(ByteBuffer dst) 159 throws IOException; 160 161 /** 162 * Sends a datagram via this channel. 163 * 164 * @exception AsynchronousCloseException If another thread closes this channel 165 * while the connect operation is in progress. 166 * @exception ClosedByInterruptException If another thread interrupts the 167 * current thread while the read operation is in progress, thereby closing the 168 * channel and setting the current thread's interrupt status. 169 * @exception ClosedChannelException If this channel is closed. 170 * @exception IOException If an error occurs 171 * @exception SecurityException If a security manager has been installed and 172 * it does not permit datagrams to be sent to the given address. 173 */ 174 public abstract int send(ByteBuffer src, SocketAddress target) 175 throws IOException; 176 177 /** 178 * Retrieves the channel's socket. 179 */ 180 public abstract DatagramSocket socket(); 181 182 /** 183 * Writes data to this channel. 184 * 185 * @exception IOException If an error occurs. 186 * @exception NotYetConnectedException The channel's socket is not connected. 187 */ 188 public abstract int write(ByteBuffer src) throws IOException; 189 190 /** 191 * Writes data to this channel. 192 * 193 * @exception IOException If an error occurs. 194 * @exception NotYetConnectedException The channel's socket is not connected. 195 */ 196 public abstract long write(ByteBuffer[] srcs, int offset, int length) 197 throws IOException; 198 199 /** 200 * Retrieves the valid operations for this channel. 201 * 202 * @exception NotYetConnectedException The channel's socket is not connected. 203 */ 204 public final int validOps() 205 { 206 return SelectionKey.OP_READ | SelectionKey.OP_WRITE; 207 } 208 }