/* AbstractSelectableChannel.java
- Copyright (C) 2002 Free Software Foundation, Inc.
+ Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath.
public abstract class AbstractSelectableChannel extends SelectableChannel
{
- int registered;
- boolean blocking = true;
- Object LOCK = new Object ();
- SelectorProvider provider;
- List keys;
+ private boolean blocking = true;
+ private Object LOCK = new Object();
+ private SelectorProvider provider;
+ private LinkedList keys = new LinkedList();
/**
* Initializes the channel
/**
* Adjusts this channel's blocking mode.
*/
- public final SelectableChannel configureBlocking (boolean block)
+ public final SelectableChannel configureBlocking (boolean blocking)
throws IOException
{
- synchronized (LOCK)
+ synchronized (blockingLock())
{
- blocking = true;
- implConfigureBlocking (block);
+ if (this.blocking != blocking)
+ {
+ implConfigureBlocking(blocking);
+ this.blocking = blocking;
+ }
}
return this;
*/
public final boolean isRegistered()
{
- return registered > 0;
+ return !keys.isEmpty();
}
/**
*/
public final SelectionKey keyFor(Selector selector)
{
+ if (! isOpen())
+ return null;
+
try
{
- return register (selector, 0, null);
+ synchronized(blockingLock())
+ {
+ return locate (selector);
+ }
}
catch (Exception e)
{
private SelectionKey locate (Selector selector)
{
- if (keys == null)
- return null;
-
- SelectionKey k = null;
ListIterator it = keys.listIterator ();
while (it.hasNext ())
{
- k = (SelectionKey) it.next ();
- if (k.selector () == selector)
- {
- return k;
- }
- }
-
- return k;
- }
-
- private void add (SelectionKey key)
- {
- if (keys == null)
- {
- keys = new LinkedList ();
+ SelectionKey key = (SelectionKey) it.next();
+
+ if (key.selector() == selector)
+ return key;
}
- keys.add (key);
+ return null;
}
/**
if (!isOpen ())
throw new ClosedChannelException();
- SelectionKey k = null;
+ SelectionKey key = null;
AbstractSelector selector = (AbstractSelector) selin;
- synchronized (LOCK)
+ synchronized (blockingLock())
{
- k = locate (selector);
+ key = locate (selector);
- if (k != null)
+ if (key != null)
{
- k.attach (att);
+ if (att != null)
+ key.attach (att);
}
else
{
- k = selector.register (this, ops, att);
+ key = selector.register (this, ops, att);
- if (k != null)
- add (k);
+ if (key != null)
+ addSelectionKey (key);
}
}
- return k;
+ return key;
+ }
+
+ void addSelectionKey(SelectionKey key)
+ {
+ keys.add(key);
+ }
+
+ // This method gets called by AbstractSelector.deregister().
+ void removeSelectionKey(SelectionKey key)
+ {
+ keys.remove(key);
}
}