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)); } +}