Logo Search packages:      
Sourcecode: nailgun version File versions  Download package

ThreadLocalInputStream.java

/*   

  Copyright 2004, Martian Software, Inc.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

*/

package com.martiansoftware.nailgun;

import java.io.IOException;
import java.io.InputStream;

/**
 * The class name is pretty descriptive.  This creates an InputStream
 * much like a FilterInputStream, but with the wrapped InputStream
 * being local to the current Thread.  By setting System.in to a
 * ThreadLocalInputStream, different Threads can read from different
 * InputStreams simply by using System.in.  Of course, the init()
 * method must be called by the Thread that wishes to use the 
 * wrapped stream.
 *  
 * @author <a href="http://www.martiansoftware.com/contact.html">Marty Lamb</a>
 */
00035 class ThreadLocalInputStream extends InputStream {

    /**
     * The InputStreams for the various threads
     */
00040     private InheritableThreadLocal streams = null;

    private InputStream defaultInputStream = null;
    
    /**
     * @param defaultInputStream the InputStream that will be used if the
     * current thread has not called init()
     */
00048     ThreadLocalInputStream(InputStream defaultInputStream) {
        super();
        streams = new InheritableThreadLocal();
        this.defaultInputStream = defaultInputStream;
        init(null);
    }

    /**
     * Sets the InputStream for the current thread
     * @param streamForCurrentThread the InputStream for the current thread
     */
00059     void init(InputStream streamForCurrentThread) {
        streams.set(streamForCurrentThread);
    }

    /**
     * Returns this thread's InputStream
     * @return this thread's InputStream
     */
00067     InputStream getInputStream() {
      InputStream result = (InputStream) streams.get();
      return ((result == null) ? defaultInputStream : result);
    }

//  BEGIN delegated java.io.InputStream methods

    /**
     * @see java.io.InputStream#available()
     */
00077     public int available() throws IOException {
        return (getInputStream().available());
    }

    /**
     * @see java.io.InputStream#close()
     */
00084     public void close() throws IOException {
        getInputStream().close();
    }

    /**
     * @see java.io.InputStream#mark(int)
     */
00091     public void mark(int readlimit) {
        getInputStream().mark(readlimit);
    }

    /**
     * @see java.io.InputStream#markSupported()
     */
00098     public boolean markSupported() {
        return (getInputStream().markSupported());
    }

    /**
     * @see java.io.InputStream#read()
     */
00105     public int read() throws IOException {
        return (getInputStream().read());
    }

    /**
     * @see java.io.InputStream#read(byte[])
     */
00112     public int read(byte[] b) throws IOException {
        return (getInputStream().read(b));
    }

    /**
     * @see java.io.InputStream#read(byte[],int,int)
     */
00119     public int read(byte[] b, int off, int len) throws IOException {
        return (getInputStream().read(b, off, len));
    }

    /**
     * @see java.io.InputStream#reset()
     */
00126     public void reset() throws IOException {
        getInputStream().reset();
    }

    /**
     * @see java.io.InputStream#skip(long)
     */
00133     public long skip(long n) throws IOException {
        return (getInputStream().skip(n));
    }

//  BEGIN delegated java.io.InputStream methods

//  Note: Should java.lang.Object methods be delegated? If not, and
//  someone synchronizes on this stream, processes might be blocked
//  that shouldn't be.  It would certainly be stupid to delegate
//  finalize().  Not so clear are hashcode(), equals(), notify(), and
//  the wait() methods.
}

Generated by  Doxygen 1.6.0   Back to index