Class ThreadLocalInputStream

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    class ThreadLocalInputStream
    extends 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.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.io.InputStream defaultInputStream  
      private java.lang.InheritableThreadLocal streams
      The InputStreams for the various threads
    • Constructor Summary

      Constructors 
      Constructor Description
      ThreadLocalInputStream​(java.io.InputStream defaultInputStream)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int available()  
      void close()  
      (package private) java.io.InputStream getInputStream()
      Returns this thread's InputStream
      (package private) void init​(java.io.InputStream streamForCurrentThread)
      Sets the InputStream for the current thread
      void mark​(int readlimit)  
      boolean markSupported()  
      int read()  
      int read​(byte[] b)  
      int read​(byte[] b, int off, int len)  
      void reset()  
      long skip​(long n)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • streams

        private java.lang.InheritableThreadLocal streams
        The InputStreams for the various threads
      • defaultInputStream

        private java.io.InputStream defaultInputStream
    • Constructor Detail

      • ThreadLocalInputStream

        ThreadLocalInputStream​(java.io.InputStream defaultInputStream)
        Parameters:
        defaultInputStream - the InputStream that will be used if the current thread has not called init()
    • Method Detail

      • init

        void init​(java.io.InputStream streamForCurrentThread)
        Sets the InputStream for the current thread
        Parameters:
        streamForCurrentThread - the InputStream for the current thread
      • getInputStream

        java.io.InputStream getInputStream()
        Returns this thread's InputStream
        Returns:
        this thread's InputStream
      • available

        public int available()
                      throws java.io.IOException
        Overrides:
        available in class java.io.InputStream
        Throws:
        java.io.IOException
        See Also:
        InputStream.available()
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.io.InputStream
        Throws:
        java.io.IOException
        See Also:
        InputStream.close()
      • mark

        public void mark​(int readlimit)
        Overrides:
        mark in class java.io.InputStream
        See Also:
        InputStream.mark(int)
      • markSupported

        public boolean markSupported()
        Overrides:
        markSupported in class java.io.InputStream
        See Also:
        InputStream.markSupported()
      • read

        public int read()
                 throws java.io.IOException
        Specified by:
        read in class java.io.InputStream
        Throws:
        java.io.IOException
        See Also:
        InputStream.read()
      • read

        public int read​(byte[] b)
                 throws java.io.IOException
        Overrides:
        read in class java.io.InputStream
        Throws:
        java.io.IOException
        See Also:
        InputStream.read(byte[])
      • read

        public int read​(byte[] b,
                        int off,
                        int len)
                 throws java.io.IOException
        Overrides:
        read in class java.io.InputStream
        Throws:
        java.io.IOException
        See Also:
        InputStream.read(byte[],int,int)
      • reset

        public void reset()
                   throws java.io.IOException
        Overrides:
        reset in class java.io.InputStream
        Throws:
        java.io.IOException
        See Also:
        InputStream.reset()
      • skip

        public long skip​(long n)
                  throws java.io.IOException
        Overrides:
        skip in class java.io.InputStream
        Throws:
        java.io.IOException
        See Also:
        InputStream.skip(long)