From 3668a9e1126f618b32a862ce08d422dac7c15a67 Mon Sep 17 00:00:00 2001 From: Jonathan Bernard Date: Sat, 10 Aug 2013 01:21:13 -0500 Subject: [PATCH] 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. --- project.properties | 6 +-- .../io/NonBlockingInputStreamReader.groovy | 21 ---------- .../io/NonBlockingInputStreamReader.java | 41 +++++++++++++++++++ 3 files changed, 44 insertions(+), 24 deletions(-) delete mode 100644 src/main/com/jdbernard/io/NonBlockingInputStreamReader.groovy create mode 100644 src/main/com/jdbernard/io/NonBlockingInputStreamReader.java diff --git a/project.properties b/project.properties index 29d5193..6730b47 100644 --- a/project.properties +++ b/project.properties @@ -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 diff --git a/src/main/com/jdbernard/io/NonBlockingInputStreamReader.groovy b/src/main/com/jdbernard/io/NonBlockingInputStreamReader.groovy deleted file mode 100644 index 7f6c642..0000000 --- a/src/main/com/jdbernard/io/NonBlockingInputStreamReader.groovy +++ /dev/null @@ -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) } -} diff --git a/src/main/com/jdbernard/io/NonBlockingInputStreamReader.java b/src/main/com/jdbernard/io/NonBlockingInputStreamReader.java new file mode 100644 index 0000000..76fbb88 --- /dev/null +++ b/src/main/com/jdbernard/io/NonBlockingInputStreamReader.java @@ -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 buffer = new LinkedList(); + + 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)); } +}