]> source.dussan.org Git - jgit.git/commitdiff
TemporaryBuffer: Fix reading from in-memory InputStream 77/3777/1
authorShawn O. Pearce <spearce@spearce.org>
Fri, 24 Jun 2011 19:35:19 +0000 (12:35 -0700)
committerShawn O. Pearce <spearce@spearce.org>
Fri, 24 Jun 2011 19:37:58 +0000 (12:37 -0700)
I had the conditions wrong here, causing the in-memory InputStream
to always appear to be at EOF.

Change-Id: I6811d6187a34eaf1fd6c5002550d631decdfc391
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
org.eclipse.jgit.test/tst/org/eclipse/jgit/util/TemporaryBufferTest.java
org.eclipse.jgit/src/org/eclipse/jgit/util/TemporaryBuffer.java

index cc4cad5ee113532dd2f0f3ac322ca30213d20d3f..eb4fb1e87dcd35cd8724f29d155178043c1e135d 100644 (file)
@@ -52,6 +52,7 @@ import static org.junit.Assert.fail;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.util.Arrays;
 
 import org.eclipse.jgit.junit.TestRng;
@@ -248,6 +249,20 @@ public class TemporaryBufferTest {
                }
        }
 
+       @Test
+       public void testInCoreInputStream() throws IOException {
+               final int cnt = 256;
+               final byte[] test = new TestRng(getName()).nextBytes(cnt);
+               final TemporaryBuffer.Heap b = new TemporaryBuffer.Heap(cnt + 4);
+               b.write(test);
+               b.close();
+
+               InputStream in = b.openInputStream();
+               byte[] act = new byte[cnt];
+               IO.readFully(in, act, 0, cnt);
+               assertTrue(Arrays.equals(test, act));
+       }
+
        @Test
        public void testInCoreLimit_SwitchOnAppendByte() throws IOException {
                final TemporaryBuffer b = new TemporaryBuffer.LocalFile();
index 58ecaa800145d8abc93964e12e49d6d53ce609b5..8167c776b14190feb42c351a5b98822b20903181 100644 (file)
@@ -548,7 +548,7 @@ public abstract class TemporaryBuffer extends OutputStream {
                        long skipped = 0;
                        while (0 < cnt) {
                                int n = (int) Math.min(block.count - blockPos, cnt);
-                               if (n < 0) {
+                               if (0 < n) {
                                        blockPos += n;
                                        skipped += n;
                                        cnt -= n;
@@ -567,11 +567,12 @@ public abstract class TemporaryBuffer extends OutputStream {
                        int copied = 0;
                        while (0 < len) {
                                int c = Math.min(block.count - blockPos, len);
-                               if (c < 0) {
+                               if (0 < c) {
                                        System.arraycopy(block.buffer, blockPos, b, off, c);
                                        blockPos += c;
                                        off += c;
                                        len -= c;
+                                       copied += c;
                                } else if (nextBlock())
                                        continue;
                                else