Skip to content

Updates SourceBufferingInputStream to conform to InputStream contract#1891

Merged
JakeWharton merged 2 commits into
square:masterfrom
rharter:rh/sbinputstream_fixes
Apr 19, 2018
Merged

Updates SourceBufferingInputStream to conform to InputStream contract#1891
JakeWharton merged 2 commits into
square:masterfrom
rharter:rh/sbinputstream_fixes

Conversation

@rharter

@rharter rharter commented Apr 19, 2018

Copy link
Copy Markdown
Contributor

No description provided.

@rharter

rharter commented Apr 19, 2018

Copy link
Copy Markdown
Contributor Author

This specifically looks at read and mark/reset functionality.

@rharter

rharter commented Apr 19, 2018

Copy link
Copy Markdown
Contributor Author

It should be noted that this comes from #1890 and makes SourceBufferingInputStream consumable by Android's BitmapFactory and the AndroidSVG library.

}

private final Buffer temp = new Buffer();
// offset is the write offset in the dest array

Copy link
Copy Markdown
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
Copy Markdown
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.

@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
Copy Markdown
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
Copy Markdown
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.

@@ -33,33 +35,49 @@ final class SourceBufferingInputStream extends InputStream {
private final Buffer buffer;
private long position;
private long mark = -1;

Copy link
Copy Markdown
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?


len = stream.read(bytes);
assertEquals(3, len);
// last two bytes are out of range so untouched from previous run

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: capitalize, period

stream.reset();
fail("expected IOException on reset");
} catch (IOException e) {
// success

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

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);
// `copyTo` treats offset as the read position, `read` treats offset as the write offset.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Very nice

@JakeWharton JakeWharton merged commit d959c4e into square:master Apr 19, 2018
@jrodbx

jrodbx commented Apr 19, 2018

Copy link
Copy Markdown
Collaborator

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants