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

Conversation

rharter
Copy link
Contributor

@rharter rharter commented Apr 19, 2018

No description provided.

@rharter
Copy link
Contributor Author

rharter commented Apr 19, 2018

This specifically looks at read and mark/reset functionality.

@rharter
Copy link
Contributor Author

rharter commented Apr 19, 2018

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


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.

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.

@@ -33,33 +35,49 @@
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?


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

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.

@@ -42,34 +43,48 @@
private final Buffer temp = new Buffer();
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
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
Copy link
Collaborator

jrodbx commented Apr 19, 2018

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