Skip to content
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

Merged
merged 2 commits into from
Apr 19, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,33 +35,49 @@ final class SourceBufferingInputStream extends InputStream {
private final Buffer buffer;
private long position;
private long mark = -1;
Copy link
Collaborator

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?

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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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);
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 copyTo to Okio. It becomes really annoying because we need two offsets in the API.

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");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"b == null"

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although I'm not sure we really need this explicit check. b is immediately dereferenced by the if statement below.

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;
Expand All @@ -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;
Expand All @@ -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 {
Expand All @@ -88,6 +107,7 @@ private int copyTo(byte[] sink, int offset, int byteCount) {
}
position = mark;
mark = -1;
markLimit = -1;
}

@Override public int available() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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

Copy link
Collaborator

Choose a reason for hiding this comment

The 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());

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The InputStream documentation says might a lot when referring to throwing exceptions here, so I figured that the error message might change to something more fine grained for separate cases while this test is still valid, so wanted to avoid checking the message content.

}

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;
Expand Down