Filter:   InfoImg
download OutputStreamHub.java
Language: Java
LOC: 121
Project Info
Enhydra Server(enhydra)
Server: ObjectWeb Forge
Type: cvs
...s\Core\src\com\lutris\util\
   Base64Encoder.java
   BMByteSearch.java
   BMByteSearchStream.java
   BytesToString.java
   ChainedError.java
   ChainedException.java
   ...edRuntimeException.java
   ChainedThrowable.java
   ChainedThrowableUtil.java
   CircularQueue.java
   Convert.java
   Currency.java
   ExceptionUtils.java
   FilePersistentStore.java
   HexEncoder.java
   HtmlEncoder.java
   JavaScriptEncoder.java
   JavaVersion.java
   LRUCache.java
   ...utStreamEventQueue.java
   ...eamEventQueueEntry.java
   OutputStreamHub.java
   PersistentStore.java
   ...tentStoreException.java
   QuotedString.java
   StringEnum.java
   TmpDir.java

/*
 * Enhydra Java Application Server Project
 * 
 * The contents of this file are subject to the Enhydra Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License on
 * the Enhydra web site ( http://www.enhydra.org/ ).
 * 
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 
 * the License for the specific terms governing rights and limitations
 * under the License.
 * 
 * The Initial Developer of the Enhydra Application Server is Lutris
 * Technologies, Inc. The Enhydra Application Server and portions created
 * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
 * All Rights Reserved.
 * 
 * Contributor(s):
 * 
 * $Id: OutputStreamHub.java,v 1.15 2005/06/13 09:26:06 draganr Exp $
 */





package com.lutris.util;

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


/**
    This class implements a "hub", or redistribution center.
    Like a "Y" connector for garden hoses, but with an arbitrary
    number of outputs, not just 2. <P>

    Instances of this class maintain teir own set of OutputStreams. 
    When you create an instance of this class, the set is empty.
    Writes to the hub have no effect. call <CODE>add()</CODE> to 
    register OutputStreams with the hub. All OutputStream calls are
    routed to all the current members of the set.

    @see java.io.OutputStream
    @author Andy John
*/
public class OutputStreamHub extends OutputStream {
    
    private Vector      myStreams;
    private boolean     closed;

    /**
        Create a new hub with no members.
    */
    public OutputStreamHub() {
        myStreams = new Vector();
        closed = false;
    }

    
    /**
        Add a new OutputStream to the set. When <code>write()</code>s
        happen, the will be coppied to all the current members of the set.
    
        @param newMember The OutputStream to add to the set.
        @see java.io.OutputStream
    */
    public void add(OutputStream newMember) {
        synchronized (myStreams) {
            myStreams.addElement(newMember);
        }
    }


    /**
        Remove an OutputStream from the set. 

        @param member The OutputStream to remove.
        @see java.io.OutputStream
    */
    public void remove(OutputStream member) {
        synchronized (myStreams) {
            myStreams.removeElement(member);
        }
    }


    /**
        Is the given OutputStream currently in the set?

        @param stream The OutputStream to search for.
        @see java.io.OutputStream
    */
    public boolean contains(OutputStream stream) {
        synchronized (myStreams) {
            return myStreams.contains(stream);
        }
    }

    //----------------------------------------


    /**
        Write an integer to all the members. Writes are attempted on all
        members. The last exception thrown, if there are more than
        one, will be thown from this method.
    
        @param b The int to write.  
        @see java.io.OutputStream
    */
    public void write(int b) throws IOException {
        synchronized (myStreams) {
            if (closed)
                throw new IOException("write() called on closed OutputStream");
            IOException err = null;
            Enumeration e = myStreams.elements();
            while (e.hasMoreElements()) {
                OutputStream o = (OutputStream) e.nextElement();
                try {
                    o.write(b);
                } catch (IOException ioe) {
                    err = ioe;
                }
            }
            if (err != null)
                throw err;
        }
    }


    /**
        Write an array of bytes to all the members. 
        Writes are attempted on all
        members. The last exception thrown, if there are more than
        one, will be thown from this method.
    
        @param b The byte array to write.   
        @see java.io.OutputStream
    */
    public void write(byte b[]) throws IOException {
        synchronized (myStreams) {
            if (closed)
                throw new IOException("write() called on closed OutputStream");
            IOException err = null;
            Enumeration e = myStreams.elements();
            while (e.hasMoreElements()) {
                OutputStream o = (OutputStream) e.nextElement();
                try {
                    o.write(b);
                } catch (IOException ioe) {
                    err = ioe;
                }
            }
            if (err != null)
                throw err;
        }
    }


    /**
        Write part of an array of bytes to all the members. 
        Bytes b[off]..b[off+len-1] will be written.
        Writes are attempted on all
        members. The last exception thrown, if there are more than
        one, will be thown from this method.
    
        @param b The byte array to write part of.
        @param off The offset.
        @param len How many bytes to write.
        @see java.io.OutputStream
    */
    public void write(byte b[],
                   int off,
                   int len) throws IOException {
        synchronized (myStreams) {
            if (closed)
                throw new IOException("write() called on closed OutputStream");
            IOException err = null;
            Enumeration e = myStreams.elements();
            while (e.hasMoreElements()) {
                OutputStream o = (OutputStream) e.nextElement();
                try {
                    o.write(b, off, len);
                } catch (IOException ioe) {
                    err = ioe;
                }
            }
            if (err != null)
                throw err;
        }
    }


    /**
        Flushes are attempted on all members. 
        The last exception thrown, if there are more than
        one, will be thown from this method.
        
        @see java.io.OutputStream
    */
    public void flush() throws IOException {
        synchronized (myStreams) {
            if (closed)
                throw new IOException("flush() called on closed OutputStream");
            IOException err = null;
            Enumeration e = myStreams.elements();
            while (e.hasMoreElements()) {
                OutputStream o = (OutputStream) e.nextElement();
                try {
                    o.flush();
                } catch (IOException ioe) {
                    err = ioe;
                }
            }
            if (err != null)
                throw err;
        }
    }


    /**
        Closes the stream. Do not call <CODE>write()</CODE> after this.
        Calls <CODE>close()</CODE> on all member OutputStreams.
        The last exception thrown, if there are more than
        one, will be thown from this method.
        
        @see java.io.OutputStream
    */
    public void close() throws IOException {
        synchronized (myStreams) {
            if (closed)
                throw new IOException("OutputStream already closed.");
            closed = true;
            IOException err = null;
            Enumeration e = myStreams.elements();
            while (e.hasMoreElements()) {
                OutputStream o = (OutputStream) e.nextElement();
                try {
                    o.close();
                } catch (IOException ioe) {
                    err = ioe;
                }
            }
            if (err != null)
                throw err;
        }
    }

}