UPMT-master/java/upmt/src/local/conference/SplitterLine.java
/*
 * Copyright (C) 2005 Luca Veltri - University of Parma - Italy
 * 
 * This source code is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This source code 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this source code; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * Author(s):
 * Luca Veltri (luca.veltri@unipr.it)
 */

package local.conference;


import java.io.IOException;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Hashtable;


/** SplitterLine is a simple splitter with one input line (the SplitterLine itself)
  * and N output lines (OutputStreams). 
  * <p/>
  * Each output line has an identifier (Object) used as key when adding or
  * removing the line.
  */
public class SplitterLine extends OutputStream
{
   /** Whether working in debug mode. */
   public static boolean DEBUG=false;

   /** SplitterLine identifier. */
   protected Object splitter_id;

   /** The output lines, as table of (Object)id-->(OutputStream)line. */
   protected Hashtable output_lines;
 
   
   /** Creates a new SplitterLine. */
   public SplitterLine(Object splitter_id)
   {  this.splitter_id=splitter_id;
      output_lines=new Hashtable();
      if (DEBUG) System.err.println("SplitterLine-"+splitter_id+": new");
   }

   /** Creates a new SplitterLine. */
   public SplitterLine(Object splitter_id, Hashtable output_lines)
   {  this.splitter_id=splitter_id;
      this.output_lines=output_lines;
      if (DEBUG) System.err.println("SplitterLine-"+splitter_id+": new");
   }


   /** Adds a new output line. */
   public void addLine(Object id, OutputStream os)
   {  if (DEBUG) System.err.println("SplitterLine-"+splitter_id+": add: "+id+" "+os);
      output_lines.put(id,os);
   }

   /** Removes an output line. */
   public void removeLine(Object id)
   {  if (DEBUG) System.err.println("SplitterLine-"+splitter_id+": remove: "+id);
      output_lines.remove(id);
   }


   /** Gets all output lines. */
   /*protected Enumeration getLines()
   {  return output_lines.elements();
   }*/


   /** Closes this output stream and releases any system resources associated with this stream. */
   public void close() throws IOException
   {  for (Enumeration e=output_lines.elements(); e.hasMoreElements(); )
      {  ((OutputStream)e.nextElement()).close();
      }
      output_lines=null;
   }
   
   /** Flushes this output stream and forces any buffered output bytes to be written out. */
   public void flush() throws IOException
   {  for (Enumeration e=output_lines.elements(); e.hasMoreElements(); )
      {  ((OutputStream)e.nextElement()).flush();
      }
   }
   
   /** Writes b.length bytes from the specified byte array to this output stream. */
   public void write(byte[] b) throws IOException
   {  //System.err.print("*");
      super.write(b);
      //System.err.print("@");
   }
   
   /** Writes len bytes from the specified byte array starting at offset off to this output stream. */
   public void write(byte[] b, int off, int len) throws IOException
   {  //System.err.print("*");
      super.write(b,off,len);
      //System.err.print("@");
   }
   
   /** Writes the specified byte to this output stream. */
   public void write(int b) throws IOException
   {  /*for (Enumeration i=output_lines.elements(); i.hasMoreElements(); )
      {  ((OutputStream)i.nextElement()).write(b);
      }*/
      for (Enumeration i=output_lines.keys(); i.hasMoreElements(); )
      {  Object line_id=i.nextElement();
         try
         {  ((OutputStream)output_lines.get(line_id)).write(b);
         }
         catch (IOException e)
         {  if (DEBUG) System.err.println("SplitterLine-"+splitter_id+": ERROR while writing on line "+line_id);
            e.printStackTrace();
            throw new IOException("SplitterLine error");
         }
      }
   }

   /** Gets a String representation of the Object */
   public String toString()
   {  return "SplitterLine-"+splitter_id;
   }
}
