diff options
author | Dmitry Neverov <dmitry.neverov@gmail.com> | 2010-05-13 09:50:44 -0700 |
---|---|---|
committer | Shawn O. Pearce <spearce@spearce.org> | 2010-05-13 09:58:18 -0700 |
commit | 3f143b8d6b5c20098e041e22be42216e668a4a0b (patch) | |
tree | 764b921ab0cccfc1c51e8ca9020e04d2329f3757 | |
parent | a54e0bae687d4a3600fab87b1182d7303f097a92 (diff) | |
download | jgit-3f143b8d6b5c20098e041e22be42216e668a4a0b.tar.gz jgit-3f143b8d6b5c20098e041e22be42216e668a4a0b.zip |
Fix missing flush in StreamCopyThread
It is possible to miss flush() invocation in StreamCopyThread.
In this case some data will not be sent to remote host and we will
wait forever (or until timeout) in src.read().
Use a counter to keep track of the flush requests.
Change-Id: Ia818be9b109a1674d9e2a9c78e125ab248cfb75b
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
-rw-r--r-- | org.eclipse.jgit/src/org/eclipse/jgit/util/io/StreamCopyThread.java | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/io/StreamCopyThread.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/io/StreamCopyThread.java index a2b9540170..50f42ad4fc 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/util/io/StreamCopyThread.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/io/StreamCopyThread.java @@ -47,6 +47,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.InterruptedIOException; import java.io.OutputStream; +import java.util.concurrent.atomic.AtomicInteger; /** Thread to copy from an input stream to an output stream. */ public class StreamCopyThread extends Thread { @@ -56,7 +57,7 @@ public class StreamCopyThread extends Thread { private final OutputStream dst; - private volatile boolean doFlush; + private final AtomicInteger flushCounter = new AtomicInteger(0); /** * Create a thread to copy data from an input stream to an output stream. @@ -82,10 +83,8 @@ public class StreamCopyThread extends Thread { * the request. */ public void flush() { - if (!doFlush) { - doFlush = true; - interrupt(); - } + flushCounter.incrementAndGet(); + interrupt(); } @Override @@ -94,10 +93,8 @@ public class StreamCopyThread extends Thread { final byte[] buf = new byte[BUFFER_SIZE]; for (;;) { try { - if (doFlush) { - doFlush = false; + if (needFlush()) dst.flush(); - } final int n; try { @@ -125,4 +122,13 @@ public class StreamCopyThread extends Thread { } } } + + private boolean needFlush() { + int i = flushCounter.get(); + if (i > 0) { + flushCounter.decrementAndGet(); + return true; + } + return false; + } } |