Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2006, JBoss Inc., and individual contributors as indicated
   * by the @authors tag. See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 
 package org.jboss.remoting.transport.multiplex.utility;
 
 import java.util.Map;
 import java.util.Set;
 
 import  org.jboss.logging.Logger;


Author(s):
Ron Sigal
Version:
$$Revision$$

Copyright (c) April 8, 2006

 

VirtualSelector is a simple version of java.nio.channels.Selector. It allows a thread to register for InputStreams to notify it that they have bytes ready to read.
 
 public class VirtualSelector
 {  
    protected static final Logger log = Logger.getLogger(VirtualSelector.class);
    private Map attachmentMap    = new HashMap();
    private Map readyMap         = new HashMap();
    private Map readyMapCopy     = new HashMap();
    private Set removableStreams = new HashSet();
    
    private boolean open = true;
    private boolean closed;
    private boolean inUse;
   
   
Allows an InputStream to register itself and an attachment.

Parameters:
inputStream
attachment
 
    public synchronized void register(GrowablePipedInputStream inputStreamObject attachment)
    {
       .put(inputStreamattachment);
    }
   
   
Allows an InputStream to unregister itself.

Parameters:
inputStream
 
    public synchronized void unregister(InputStream inputStream)
    {
       .remove(inputStream);
       .remove(inputStream);
       .add(inputStream);
    }
   
   
Allows a Thread to wait to be informed of InputStreams that have bytes ready to read.

Returns:
a Map from a set of InputStreams with ready bytes to their attachments. If close() is called while a Thread is waiting in select(), and if there are no ready InputStreams, select() will return null.
 
    public synchronized Map select()
    {
       if ()
          return null;
       
       // Remove any InputStreams declared to be removable.
       Iterator it = .iterator();
       while (it.hasNext())
      {
         .remove(it.next());
      }
      .clear();
      
      // Add any newly ready InputStreams.
      .clear();
      
      // If there are any ready InputStreams, we're done.
      if (!.isEmpty())
         return ;
         
      // Otherwise, wait for some InputStream to become ready.
      while (.isEmpty() && )
      {
         try
         {
             = true;
            wait();
         }
         catch (InterruptedException ignored) {}
         finally
         {
             = false;
         }
      }
      
      // If readyMap is empty, then we're here because someone called close().
      if (.isEmpty())
      {
         .debug("returning null");
         finishClose();
         return null;
      }
      
      // Transfer all ready InputStreams to readyMapCopy.
      .clear();
      return ;
   }
   
   
Returns true if and only if this VirtualSelector is open.

Returns:
true if and only if this VirtualSelector is open
   public boolean isOpen()
   {
      return ;
   }
   
   
Marks this VirtualSelector as preparing to close. If any Thread is blocked in select(), select() returns null.
   public synchronized void close()
   {
       = false;
      
      if ()
         notifyAll();
      else
         finishClose();
   }
   
   
Allows an InputStream to inform a listening Thread that it has bytes ready to read.

Parameters:
inputStream
   public synchronized void addToReadyInputStreams(InputStream inputStream)
   throws IOException
   {
      if (!)
         throw new IOException("This VirtualSelector is closed.");
      
      .put(inputStream.get(inputStream));
      notifyAll();
   }
   
   
Indicates that an InputStream has been processed. If InputStream has no available bytes, it will be removed from the Set of InputStreams that will be returned by the next call to select(). If InputStream has available bytes, its status will not be changed.

Parameters:
inputStream
Throws:
IOException
   public synchronized void remove(InputStream inputStreamthrows IOException
   {
      .add(inputStream);
   }
   
   
   public synchronized void waitUntilEmpty()
   {
      while (!.isEmpty())
      {
         try
         {
            .debug("waiting until empty");
            wait();
         }
         catch (InterruptedException ignored) {}
      }
      .debug("empty");
   }
   
   
Finishes the process of closing this VirtualSelector, releasing all resources.
   protected void finishClose()
   {
      .clear();
      .clear();
      .clear();
      .clear();
       = true;
   }
New to GrepCode? Check out our FAQ X