tcl.lang
Class ManagedSystemInStream

java.lang.Object
  extended by java.io.InputStream
      extended by tcl.lang.ManagedSystemInStream
All Implemented Interfaces:
Closeable, Runnable

public class ManagedSystemInStream
extends InputStream
implements Runnable

A class for managing the System.in. Avoids blocking the current thread on the non-interruptible System.in.read(), by isolating all System.in.read()'s in a separate thread. Allows other classes to use System.in in a coordinated way, so that one class doesn't block access to System.in. After the initial instance is created, Java objects can read from System.in directly to get the benefits of this class, or can create another instance of this class that can be close()'d to interrupt a blocked read. This class forces standard input to remain unbuffered, so the JVM doesn't steal bytes from stdin that might be useful to subsequent processes that run after this JVM exits.


Constructor Summary
ManagedSystemInStream()
          Create a new ManagedSystemInStream.
 
Method Summary
 int available()
           
 void close()
          Closes this ManagedSystemInStream instance.
 void dispose()
          Signal readThread to stop, restore original stdin
 int read()
           
 int read(byte[] b)
           
 int read(byte[] b, int offset, int length)
           
 void run()
          Reads from System.in directly in a separate thread.
 
Methods inherited from class java.io.InputStream
mark, markSupported, reset, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ManagedSystemInStream

public ManagedSystemInStream()
Create a new ManagedSystemInStream. If this is the first ManagedSystemInStream instance, System.in is set to this instance, so all direct access to System.in goes through this instance. Other objects can either read System.in directly, or create an instance of this class to read from.

Method Detail

dispose

public void dispose()
Signal readThread to stop, restore original stdin


available

public int available()
              throws IOException
Overrides:
available in class InputStream
Throws:
IOException

close

public void close()
           throws IOException
Closes this ManagedSystemInStream instance. Stops any blocked read() on this instance.

Specified by:
close in interface Closeable
Overrides:
close in class InputStream
Throws:
IOException

read

public int read()
         throws IOException
Specified by:
read in class InputStream
Throws:
IOException

read

public int read(byte[] b)
         throws IOException
Overrides:
read in class InputStream
Throws:
IOException

read

public int read(byte[] b,
                int offset,
                int length)
         throws IOException
Overrides:
read in class InputStream
Throws:
IOException

run

public void run()
Reads from System.in directly in a separate thread.

Specified by:
run in interface Runnable


Copyright © 2015. All rights reserved.