TemporaryBuffer: Fix reading from in-memory InputStream
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>
This commit is contained in:
parent
2cebb7dbc7
commit
826fb260a3
|
@ -52,6 +52,7 @@
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
import org.eclipse.jgit.junit.TestRng;
|
import org.eclipse.jgit.junit.TestRng;
|
||||||
|
@ -248,6 +249,20 @@ public void testLarge_SingleWrite() throws IOException {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@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
|
@Test
|
||||||
public void testInCoreLimit_SwitchOnAppendByte() throws IOException {
|
public void testInCoreLimit_SwitchOnAppendByte() throws IOException {
|
||||||
final TemporaryBuffer b = new TemporaryBuffer.LocalFile();
|
final TemporaryBuffer b = new TemporaryBuffer.LocalFile();
|
||||||
|
|
|
@ -548,7 +548,7 @@ public long skip(long cnt) throws IOException {
|
||||||
long skipped = 0;
|
long skipped = 0;
|
||||||
while (0 < cnt) {
|
while (0 < cnt) {
|
||||||
int n = (int) Math.min(block.count - blockPos, cnt);
|
int n = (int) Math.min(block.count - blockPos, cnt);
|
||||||
if (n < 0) {
|
if (0 < n) {
|
||||||
blockPos += n;
|
blockPos += n;
|
||||||
skipped += n;
|
skipped += n;
|
||||||
cnt -= n;
|
cnt -= n;
|
||||||
|
@ -567,11 +567,12 @@ public int read(byte[] b, int off, int len) throws IOException {
|
||||||
int copied = 0;
|
int copied = 0;
|
||||||
while (0 < len) {
|
while (0 < len) {
|
||||||
int c = Math.min(block.count - blockPos, len);
|
int c = Math.min(block.count - blockPos, len);
|
||||||
if (c < 0) {
|
if (0 < c) {
|
||||||
System.arraycopy(block.buffer, blockPos, b, off, c);
|
System.arraycopy(block.buffer, blockPos, b, off, c);
|
||||||
blockPos += c;
|
blockPos += c;
|
||||||
off += c;
|
off += c;
|
||||||
len -= c;
|
len -= c;
|
||||||
|
copied += c;
|
||||||
} else if (nextBlock())
|
} else if (nextBlock())
|
||||||
continue;
|
continue;
|
||||||
else
|
else
|
||||||
|
|
Loading…
Reference in New Issue