Source for gnu.java.nio.DatagramChannelImpl

   1: /* DatagramChannelImpl.java -- 
   2:    Copyright (C) 2002, 2003, 2004, 2006  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., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 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: 
  39: package gnu.java.nio;
  40: 
  41: import gnu.java.net.PlainDatagramSocketImpl;
  42: import java.io.IOException;
  43: import java.net.DatagramPacket;
  44: import java.net.DatagramSocket;
  45: import java.net.InetSocketAddress;
  46: import java.net.SocketAddress;
  47: import java.net.SocketTimeoutException;
  48: import java.nio.ByteBuffer;
  49: import java.nio.channels.ClosedChannelException;
  50: import java.nio.channels.DatagramChannel;
  51: import java.nio.channels.NotYetConnectedException;
  52: import java.nio.channels.spi.SelectorProvider;
  53: 
  54: /**
  55:  * @author Michael Koch
  56:  */
  57: public final class DatagramChannelImpl extends DatagramChannel
  58: {
  59:   private NIODatagramSocket socket;
  60:   
  61:   /**
  62:    * Indicates whether this channel initiated whatever operation
  63:    * is being invoked on our datagram socket.
  64:    */
  65:   private boolean inChannelOperation;
  66: 
  67:   /**
  68:    * Indicates whether our datagram socket should ignore whether
  69:    * we are set to non-blocking mode. Certain operations on our
  70:    * socket throw an <code>IllegalBlockingModeException</code> if
  71:    * we are in non-blocking mode, <i>except</i> if the operation
  72:    * is initiated by us.
  73:    */
  74:   public final boolean isInChannelOperation()
  75:   {
  76:     return inChannelOperation;
  77:   }
  78:   
  79:   /**
  80:    * Sets our indicator of whether we are initiating an I/O operation
  81:    * on our socket.
  82:    */
  83:   public final void setInChannelOperation(boolean b)
  84:   {
  85:     inChannelOperation = b;
  86:   }
  87:  
  88:   protected DatagramChannelImpl (SelectorProvider provider)
  89:     throws IOException
  90:   {
  91:     super (provider);
  92:     socket = new NIODatagramSocket (new PlainDatagramSocketImpl(), this);
  93:     configureBlocking(true);
  94:   }
  95: 
  96:   public int getNativeFD()
  97:   {
  98:     return socket.getPlainDatagramSocketImpl().getNativeFD();
  99:   }
 100:     
 101:   public DatagramSocket socket ()
 102:   {
 103:     return socket;
 104:   }
 105:     
 106:   protected void implCloseSelectableChannel ()
 107:     throws IOException
 108:   {
 109:     socket.close ();
 110:   }
 111:     
 112:   protected void implConfigureBlocking (boolean blocking)
 113:     throws IOException
 114:   {
 115:     socket.setSoTimeout (blocking ? 0 : NIOConstants.DEFAULT_TIMEOUT);
 116:   }
 117: 
 118:   public DatagramChannel connect (SocketAddress remote)
 119:     throws IOException
 120:   {
 121:     if (!isOpen())
 122:       throw new ClosedChannelException();
 123:     
 124:     socket.connect (remote);
 125:     return this;
 126:   }
 127:     
 128:   public DatagramChannel disconnect ()
 129:     throws IOException
 130:   {
 131:     socket.disconnect ();
 132:     return this;
 133:   }
 134:     
 135:   public boolean isConnected ()
 136:   {
 137:     return socket.isConnected ();
 138:   }
 139:     
 140:   public int write (ByteBuffer src)
 141:     throws IOException
 142:   {
 143:     if (!isConnected ())
 144:       throw new NotYetConnectedException ();
 145:     
 146:     return send (src, socket.getRemoteSocketAddress());
 147:   }
 148: 
 149:   public long write (ByteBuffer[] srcs, int offset, int length)
 150:     throws IOException
 151:   {
 152:     if (!isConnected())
 153:       throw new NotYetConnectedException();
 154: 
 155:     if ((offset < 0)
 156:         || (offset > srcs.length)
 157:         || (length < 0)
 158:         || (length > (srcs.length - offset)))
 159:       throw new IndexOutOfBoundsException();
 160:       
 161:     long result = 0;
 162: 
 163:     for (int index = offset; index < offset + length; index++)
 164:       result += write (srcs [index]);
 165: 
 166:     return result;
 167:   }
 168: 
 169:   public int read (ByteBuffer dst)
 170:     throws IOException
 171:   {
 172:     if (!isConnected ())
 173:       throw new NotYetConnectedException ();
 174:     
 175:     int remaining = dst.remaining();
 176:     receive (dst);
 177:     return remaining - dst.remaining();
 178:   }
 179:     
 180:   public long read (ByteBuffer[] dsts, int offset, int length)
 181:     throws IOException
 182:   {
 183:     if (!isConnected())
 184:       throw new NotYetConnectedException();
 185:     
 186:     if ((offset < 0)
 187:         || (offset > dsts.length)
 188:         || (length < 0)
 189:         || (length > (dsts.length - offset)))
 190:       throw new IndexOutOfBoundsException();
 191:       
 192:     long result = 0;
 193: 
 194:     for (int index = offset; index < offset + length; index++)
 195:       result += read (dsts [index]);
 196: 
 197:     return result;
 198:   }
 199:     
 200:   public SocketAddress receive (ByteBuffer dst)
 201:     throws IOException
 202:   {
 203:     if (!isOpen())
 204:       throw new ClosedChannelException();
 205:     
 206:     try
 207:       {
 208:         DatagramPacket packet;
 209:         int len = dst.remaining();
 210:         
 211:         if (dst.hasArray())
 212:           {
 213:             packet = new DatagramPacket (dst.array(),
 214:                                          dst.arrayOffset() + dst.position(),
 215:                                          len);
 216:           }
 217:         else
 218:           {
 219:             packet = new DatagramPacket (new byte [len], len);
 220:           }
 221: 
 222:         boolean completed = false;
 223: 
 224:         try
 225:           {
 226:             begin();
 227:             setInChannelOperation(true);
 228:             socket.receive (packet);
 229:             completed = true;
 230:           }
 231:         finally
 232:           {
 233:             end (completed);
 234:             setInChannelOperation(false);
 235:           }
 236: 
 237:         if (!dst.hasArray())
 238:           {
 239:             dst.put (packet.getData(), packet.getOffset(), packet.getLength());
 240:           }
 241:         else
 242:           {
 243:             dst.position (dst.position() + packet.getLength());
 244:           }
 245: 
 246:         return packet.getSocketAddress();
 247:       }
 248:     catch (SocketTimeoutException e)
 249:       {
 250:         return null;
 251:       }
 252:   }
 253:     
 254:   public int send (ByteBuffer src, SocketAddress target)
 255:     throws IOException
 256:   {
 257:     if (!isOpen())
 258:       throw new ClosedChannelException();
 259:     
 260:     if (target instanceof InetSocketAddress
 261:     && ((InetSocketAddress) target).isUnresolved())
 262:       throw new IOException("Target address not resolved");
 263: 
 264:     byte[] buffer;
 265:     int offset = 0;
 266:     int len = src.remaining();
 267:     
 268:     if (src.hasArray())
 269:       {
 270:         buffer = src.array();
 271:         offset = src.arrayOffset() + src.position();
 272:       }
 273:     else
 274:       {
 275:         buffer = new byte [len];
 276:         src.get (buffer);
 277:       }
 278: 
 279:     DatagramPacket packet = new DatagramPacket (buffer, offset, len, target);
 280: 
 281:     boolean completed = false;
 282:     try
 283:       {
 284:         begin();
 285:         setInChannelOperation(true);
 286:         socket.send(packet);
 287:         completed = true;
 288:       }
 289:     finally
 290:       {
 291:         end (completed);
 292:         setInChannelOperation(false);
 293:       }
 294:       
 295:     if (src.hasArray())
 296:       {
 297:     src.position (src.position() + len);
 298:       }
 299: 
 300:     return len;
 301:   }
 302: }