2004-03-18 Michael Koch <konqueror@gmx.de>
[gcc.git] / libjava / java / nio / channels / spi / AbstractSelectableChannel.java
index 17d6a2eaea1f90361d2d8130111434b8a4468e45..a07485df85f1f61bf346b2469af0a50c83bf9e51 100644 (file)
@@ -1,5 +1,5 @@
 /* AbstractSelectableChannel.java
-   Copyright (C) 2002, 2003 Free Software Foundation, Inc.
+   Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
 
@@ -48,11 +48,10 @@ import java.util.ListIterator;
 
 public abstract class AbstractSelectableChannel extends SelectableChannel
 {
-  private int registered;
   private boolean blocking = true;
   private Object LOCK = new Object();
   private SelectorProvider provider;
-  private LinkedList keys;
+  private LinkedList keys = new LinkedList();
 
   /**
    * Initializes the channel
@@ -60,7 +59,6 @@ public abstract class AbstractSelectableChannel extends SelectableChannel
   protected AbstractSelectableChannel (SelectorProvider provider)
   {
     this.provider = provider;
-    this.keys = new LinkedList();
   }
 
   /**
@@ -80,8 +78,11 @@ public abstract class AbstractSelectableChannel extends SelectableChannel
   {
     synchronized (blockingLock())
       {
-        implConfigureBlocking(blocking);
-        this.blocking = blocking;
+        if (this.blocking != blocking)
+          {
+            implConfigureBlocking(blocking);
+            this.blocking = blocking;
+          }
       }
     
     return this;
@@ -132,9 +133,15 @@ public abstract class AbstractSelectableChannel extends SelectableChannel
    */
   public final SelectionKey keyFor(Selector selector)
   {
+    if (! isOpen())
+      return null;
+    
     try
       {
-        return register (selector, 0, null);
+        synchronized(blockingLock())
+         {
+           return locate (selector);
+         }
       }
     catch (Exception e)
       {
@@ -152,9 +159,6 @@ public abstract class AbstractSelectableChannel extends SelectableChannel
 
   private SelectionKey locate (Selector selector)
   {
-    if (keys == null)
-      return null;
-    
     ListIterator it = keys.listIterator ();
     
     while (it.hasNext ())
@@ -168,11 +172,6 @@ public abstract class AbstractSelectableChannel extends SelectableChannel
     return null;
   }
 
-  private void add (SelectionKey key)
-  {
-    keys.add (key);
-  }
-
   /**
    * Registers this channel with the given selector, returning a selection key.
    *
@@ -193,17 +192,29 @@ public abstract class AbstractSelectableChannel extends SelectableChannel
 
         if (key != null)
           {
-            key.attach (att);
+           if (att != null)
+             key.attach (att);
           }
         else
           {
             key = selector.register (this, ops, att);
                
             if (key != null)
-              add (key);
+              addSelectionKey (key);
           }
       }
 
     return key;
   }
+
+  void addSelectionKey(SelectionKey key)
+  {
+    keys.add(key);
+  }
+
+  // This method gets called by AbstractSelector.deregister().
+  void removeSelectionKey(SelectionKey key)
+  {
+    keys.remove(key);
+  }
 }