NonBlockingInputStreamReader is now pausable.

* NonBlockingInputStreamReader now has pause() and resume(). The reader will
  stop listening to input when pause() is invoked and will skip all available
  input once resume() is invoked, ignoring any buffered input entered while it
  was paused.
* Bumped the version to 2.0. This version change should have been made instead
  of 1.11, as that was a breaking change, altering the package name of several
  classes.
This commit is contained in:
Jonathan Bernard 2013-08-10 01:21:13 -05:00
parent b47ca10660
commit 3668a9e112
3 changed files with 44 additions and 24 deletions

View File

@ -1,6 +1,6 @@
#Thu, 08 Aug 2013 20:37:37 -0500
#Sat, 10 Aug 2013 00:00:58 -0500
name=jdb-util
version=1.11
version=2.0
lib.local=true
build.number=3
build.number=2

View File

@ -1,21 +0,0 @@
package com.jdbernard.io;
public class NonBlockingInputStreamReader implements Runnable {
private Reader rin
private LinkedList buffer = []
public void run() {
String line = null
try {
while((line = rin.readLine()) != null &&
!Thread.currentThread().isInterrupted())
storeLine(line) }
catch (InterruptedException ie) { Thread.currentThread().interrupt() } }
public synchronized String readLine() { return buffer.poll() }
private synchronized void storeLine(String line) { buffer << line }
public NonBlockingInputStreamReader(def sin) {
this.rin = new InputStreamReader(sin) }
}

View File

@ -0,0 +1,41 @@
package com.jdbernard.io;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.LinkedList;
public class NonBlockingInputStreamReader implements Runnable {
private InputStream streamIn;
private BufferedReader reader;
private volatile boolean paused = false;
private volatile boolean stopped = false;
private LinkedList<String> buffer = new LinkedList<String>();
public void run() {
String line = null;
while (!stopped && !Thread.currentThread().isInterrupted()) {
try {
if (paused) Thread.sleep(200);
else {
line = reader.readLine();
if (line == null) stopped = true;
else storeLine(line); } }
catch (IOException ioe) { stopped = true; }
catch (InterruptedException ie) {
Thread.currentThread().interrupt(); } } }
public synchronized String readLine() { return buffer.poll(); }
private synchronized void storeLine(String line) { buffer.add(line); }
public synchronized void pause() throws IOException { paused = true; }
public synchronized void resume() throws IOException {
reader.skip(streamIn.available());
paused = false; }
public NonBlockingInputStreamReader(InputStream streamIn) {
this.streamIn = streamIn;
this.reader = new BufferedReader(new InputStreamReader(streamIn)); }
}