* gnu/java/nio/DatagramChannelImpl.java
authorMohan Embar <gnustuff@thisiscool.com>
Tue, 3 Feb 2004 16:07:24 +0000 (16:07 +0000)
committerMohan Embar <membar@gcc.gnu.org>
Tue, 3 Feb 2004 16:07:24 +0000 (16:07 +0000)
(inChannelOperation): New field.
(isInChannelOperation): New accessor.
(setInChannelOperation): New modifier.
(receive): Use capacity() - position() of destination
buffer instead of remaining(). Set and reset our "in
channel operation indicator" before and after delegating
the receive to our datagram socket. Removed testing code.
Update destination buffer's current position if it is
backed by a byte array (hasArray() is true).
(send): Set and reset our "in channel operation indicator"
before and after delegating the send to our datagram socket.
Removed testing code. Update source buffer's current position
if it is backed by a byte array (hasArray() is true).
* gnu/java/nio/SocketChannelImpl.java (read(ByteBuffer)):
Use capacity() - position() of destination buffer instead
of remaining().
* java/net/DatagramSocket.java (receive): Don't throw an
IllegalBlockingModeException if we have a non-blocking
channel which initiated this operation.
(send): Likewise.

From-SVN: r77173

libjava/ChangeLog
libjava/gnu/java/nio/DatagramChannelImpl.java
libjava/gnu/java/nio/SocketChannelImpl.java
libjava/java/net/DatagramSocket.java

index 454b7486a70d56f1a45561a3e12851768646d5c9..3392b3b491381d2fed172c7d10534e3e7bb9af0c 100644 (file)
@@ -1,3 +1,27 @@
+2004-02-03  Mohan Embar  <gnustuff@thisiscool.com>
+
+       * gnu/java/nio/DatagramChannelImpl.java
+       (inChannelOperation): New field.
+       (isInChannelOperation): New accessor.
+       (setInChannelOperation): New modifier.
+       (receive): Use capacity() - position() of destination
+       buffer instead of remaining(). Set and reset our "in
+       channel operation indicator" before and after delegating
+       the receive to our datagram socket. Removed testing code.
+       Update destination buffer's current position if it is
+       backed by a byte array (hasArray() is true).
+       (send): Set and reset our "in channel operation indicator"
+       before and after delegating the send to our datagram socket.
+       Removed testing code. Update source buffer's current position
+       if it is backed by a byte array (hasArray() is true).
+       * gnu/java/nio/SocketChannelImpl.java (read(ByteBuffer)):
+       Use capacity() - position() of destination buffer instead
+       of remaining().
+       * java/net/DatagramSocket.java (receive): Don't throw an
+       IllegalBlockingModeException if we have a non-blocking
+       channel which initiated this operation.
+       (send): Likewise.
+
 2004-02-01  Thomas Fitzsimmons  <fitzsim@redhat.com>
 
        * configure.in: Add pkgconfig check for glib and gthread.
index 3531803900aa999c5540c59f7ea002ec5b90e482..baeac19deb641fd169de2bb04ec4ca2ef7457c5f 100644 (file)
@@ -57,6 +57,33 @@ public final class DatagramChannelImpl extends DatagramChannel
 {
   private NIODatagramSocket socket;
   
+  /**
+   * Indicates whether this channel initiated whatever operation
+   * is being invoked on our datagram socket.
+   */
+  private boolean inChannelOperation;
+
+  /**
+   * Indicates whether our datagram socket should ignore whether
+   * we are set to non-blocking mode. Certain operations on our
+   * socket throw an <code>IllegalBlockingModeException</code> if
+   * we are in non-blocking mode, <i>except</i> if the operation
+   * is initiated by us.
+   */
+  public final boolean isInChannelOperation()
+  {
+    return inChannelOperation;
+  }
+  
+  /**
+   * Sets our indicator of whether we are initiating an I/O operation
+   * on our socket.
+   */
+  public final void setInChannelOperation(boolean b)
+  {
+    inChannelOperation = b;
+  }
   protected DatagramChannelImpl (SelectorProvider provider)
     throws IOException
   {
@@ -178,7 +205,7 @@ public final class DatagramChannelImpl extends DatagramChannel
     try
       {
         DatagramPacket packet;
-        int len = dst.remaining();
+        int len = dst.capacity() - dst.position();
         
         if (dst.hasArray())
           {
@@ -196,23 +223,23 @@ public final class DatagramChannelImpl extends DatagramChannel
         try
           {
             begin();
+            setInChannelOperation(true);
             socket.receive (packet);
             completed = true;
           }
         finally
           {
             end (completed);
+            setInChannelOperation(false);
           }
 
         if (!dst.hasArray())
           {
             dst.put (packet.getData(), packet.getOffset(), packet.getLength());
           }
-
-        // FIMXE: remove this testing code.
-        for (int i = 0; i < packet.getLength(); i++)
+        else
           {
-            System.out.println ("Byte " + i + " has value " + packet.getData() [packet.getOffset() + i]);
+            dst.position (dst.position() + packet.getLength());
           }
 
         return packet.getSocketAddress();
@@ -246,13 +273,25 @@ public final class DatagramChannelImpl extends DatagramChannel
 
     DatagramPacket packet = new DatagramPacket (buffer, offset, len, target);
 
-    // FIMXE: remove this testing code.
-    for (int i = 0; i < packet.getLength(); i++)
+    boolean completed = false;
+    try
+      {
+        begin();
+        setInChannelOperation(true);
+        socket.send(packet);
+        completed = true;
+      }
+    finally
+      {
+        end (completed);
+        setInChannelOperation(false);
+      }
+      
+    if (src.hasArray())
       {
-        System.out.println ("Byte " + i + " has value " + packet.getData() [packet.getOffset() + i]);
+       src.position (src.position() + len);
       }
 
-    socket.send (packet);
     return len;
   }
 }
index 4df40b481a6df0a9915ab2d7a2dc1811030de422..d490529f87396e443ffe0d1442a576318bfb72a4 100644 (file)
@@ -226,7 +226,7 @@ public final class SocketChannelImpl extends SocketChannel
     int offset = 0;
     InputStream input = socket.getInputStream();
     int available = input.available();
-    int len = dst.remaining();
+    int len = dst.capacity() - dst.position();
        
     if (available == 0)
       return 0;
index c9c0f5d0f03ddd33d7ee0e0196872d04c7098cbc..c5920a0942460e70f2c87b4f9f06b05c7f27d0ea 100644 (file)
@@ -39,6 +39,7 @@ exception statement from your version. */
 package java.net;
 
 import gnu.java.net.PlainDatagramSocketImpl;
+import gnu.java.nio.DatagramChannelImpl;
 import java.io.IOException;
 import java.nio.channels.DatagramChannel;
 import java.nio.channels.IllegalBlockingModeException;
@@ -565,7 +566,8 @@ public class DatagramSocket
         ("Socket connected to a multicast address my not receive");
 
     if (getChannel() != null
-        && !getChannel().isBlocking ())
+        && !getChannel().isBlocking ()
+        && !((DatagramChannelImpl) getChannel()).isInChannelOperation())
       throw new IllegalBlockingModeException ();
 
     getImpl().receive(p);
@@ -618,7 +620,8 @@ public class DatagramSocket
     // use getTimeToLive for TTL val.
 
     if (getChannel() != null
-        && !getChannel().isBlocking ())
+        && !getChannel().isBlocking ()
+        && !((DatagramChannelImpl) getChannel()).isInChannelOperation())
       throw new IllegalBlockingModeException ();
 
     getImpl().send(p);