-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Updates SourceBufferingInputStream to conform to InputStream contract #1891
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,8 @@ | |
import okio.Buffer; | ||
import okio.BufferedSource; | ||
|
||
import static com.squareup.picasso3.Utils.checkNotNull; | ||
|
||
/** | ||
* An {@link InputStream} that fills the buffer of an {@link BufferedSource} as reads are requested | ||
* and copies its bytes into the byte arrays of the caller. This allows you to read as much of the | ||
|
@@ -33,33 +35,49 @@ final class SourceBufferingInputStream extends InputStream { | |
private final Buffer buffer; | ||
private long position; | ||
private long mark = -1; | ||
private long markLimit = -1; | ||
|
||
SourceBufferingInputStream(BufferedSource source) { | ||
this.source = source; | ||
this.buffer = source.buffer(); | ||
} | ||
|
||
private final Buffer temp = new Buffer(); | ||
// offset is the write offset in the dest array | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you move this comment inside the method so it doesn't get lost if shuffling occurs. Also please capitalize "offset" and add a period. Comments should be sentences. |
||
private int copyTo(byte[] sink, int offset, int byteCount) { | ||
// TODO replace this with https://github.com/square/okio/issues/362 | ||
buffer.copyTo(temp, offset, byteCount); | ||
buffer.copyTo(temp, position, byteCount); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I meant to circle back and fix this in Picasso, but I noticed this was a bug when trying to add |
||
return temp.read(sink, offset, byteCount); | ||
} | ||
|
||
@Override public int read() throws IOException { | ||
source.require(position + 1); | ||
if (!source.request(position + 1)) { | ||
return -1; | ||
} | ||
byte value = buffer.getByte(position++); | ||
if (position > mark) { | ||
if (position > markLimit) { | ||
mark = -1; | ||
} | ||
return value; | ||
} | ||
|
||
@Override public int read(@NonNull byte[] b, int off, int len) throws IOException { | ||
source.require(position + len); | ||
int copied = /*buffer.*/copyTo(b, off, len); | ||
checkNotNull(b, "b is null"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Although I'm not sure we really need this explicit check. |
||
if (off < 0 || len < 0 || len > b.length - off) { | ||
throw new IndexOutOfBoundsException(); | ||
} else if (len == 0) { | ||
return 0; | ||
} | ||
|
||
int count = len; | ||
if (!source.request(position + count)) { | ||
count = available(); | ||
} | ||
if (count == 0) return -1; | ||
|
||
int copied = /*buffer.*/copyTo(b, off, count); | ||
position += copied; | ||
if (position > mark) { | ||
if (position > markLimit) { | ||
mark = -1; | ||
} | ||
return copied; | ||
|
@@ -68,7 +86,7 @@ private int copyTo(byte[] sink, int offset, int byteCount) { | |
@Override public long skip(long n) throws IOException { | ||
source.require(position + n); | ||
position += n; | ||
if (position > mark) { | ||
if (position > markLimit) { | ||
mark = -1; | ||
} | ||
return n; | ||
|
@@ -79,7 +97,8 @@ private int copyTo(byte[] sink, int offset, int byteCount) { | |
} | ||
|
||
@Override public void mark(int readlimit) { | ||
mark = position + readlimit; | ||
mark = position; | ||
markLimit = position + readlimit; | ||
} | ||
|
||
@Override public void reset() throws IOException { | ||
|
@@ -88,6 +107,7 @@ private int copyTo(byte[] sink, int offset, int byteCount) { | |
} | ||
position = mark; | ||
mark = -1; | ||
markLimit = -1; | ||
} | ||
|
||
@Override public int available() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,9 @@ | |
import okio.Timeout; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertArrayEquals; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.fail; | ||
|
||
public final class SourceBufferingInputStreamTest { | ||
@Test public void replay() throws IOException { | ||
|
@@ -45,6 +47,61 @@ public final class SourceBufferingInputStreamTest { | |
assertEquals(13, stream.available()); | ||
} | ||
|
||
@Test public void read() throws IOException { | ||
Buffer data = new Buffer().writeUtf8("Hello, world!"); | ||
BufferedSource source = Okio.buffer((Source) data); | ||
InputStream stream = new SourceBufferingInputStream(source); | ||
byte[] bytes = new byte[5]; | ||
int len = stream.read(bytes); | ||
assertEquals(5, len); | ||
assertArrayEquals(new byte[] {'H', 'e', 'l', 'l', 'o'}, bytes); | ||
|
||
len = stream.read(bytes); | ||
assertEquals(5, len); | ||
assertArrayEquals(new byte[] {',', ' ', 'w', 'o', 'r'}, bytes); | ||
|
||
len = stream.read(bytes); | ||
assertEquals(3, len); | ||
// last two bytes are out of range so untouched from previous run | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: capitalize, period |
||
assertArrayEquals(new byte[] {'l', 'd', '!', 'o', 'r'}, bytes); | ||
|
||
len = stream.read(bytes); | ||
assertEquals(-1, len); | ||
} | ||
|
||
@Test public void markReset() throws IOException { | ||
Buffer data = new Buffer().writeUtf8("Hello, world!"); | ||
BufferedSource source = Okio.buffer((Source) data); | ||
InputStream stream = new SourceBufferingInputStream(source); | ||
stream.mark(2); | ||
|
||
byte[] bytes = new byte[4]; | ||
int len = stream.read(bytes, 0, 2); | ||
assertEquals(2, len); | ||
assertArrayEquals(new byte[] {'H', 'e', 0, 0}, bytes); | ||
|
||
len = stream.read(bytes); | ||
assertEquals(len, 4); | ||
assertArrayEquals(new byte[] {'l', 'l', 'o', ','}, bytes); | ||
|
||
try { | ||
stream.reset(); | ||
fail("expected IOException on reset"); | ||
} catch (IOException e) { | ||
// success | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we tend to name the catch variable "expected" so that we can omit this comment There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or you could assert on the message to make sure we're getting the correct IOE assertEquals("No mark or whatever", e.getMessage()); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The InputStream documentation says |
||
} | ||
|
||
stream.mark(2); | ||
len = stream.read(bytes, 0, 2); | ||
assertEquals(2, len); | ||
assertArrayEquals(new byte[] {' ', 'w', 'o', ','}, bytes); | ||
|
||
stream.reset(); | ||
len = stream.read(bytes); | ||
assertEquals(4, len); | ||
assertArrayEquals(new byte[] {' ', 'w', 'o', 'r'}, bytes); | ||
} | ||
|
||
/** Prevents a consumer from reading large chunks and exercises edge cases. */ | ||
private static final class OneByteSource implements Source { | ||
private final Source upstream; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we call this
markPosition
then?