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 a2b954017..50f42ad4f 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.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 StreamCopyThread(final InputStream i, final OutputStream o) { * the request. */ public void flush() { - if (!doFlush) { - doFlush = true; - interrupt(); - } + flushCounter.incrementAndGet(); + interrupt(); } @Override @@ -94,10 +93,8 @@ public void run() { 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 void run() { } } } + + private boolean needFlush() { + int i = flushCounter.get(); + if (i > 0) { + flushCounter.decrementAndGet(); + return true; + } + return false; + } }