Skip to content

Commit c194b3b

Browse files
medbchingor13
authored andcommitted
Fix throwIfFalseEOF if skip method was called before read (#447)
* Fix throwIfFalseEOF when using skip method throwIfFalseEOF method don't take into account bytes read/skipped in skip method and throws IOException when legit EOF was reached. * Fix variables naming * Fix variable type * Remove '-1' check - `skip` method never returns it * Add test
1 parent 2777514 commit c194b3b

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

google-http-client/src/main/java/com/google/api/client/http/javanet/NetHttpResponse.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,13 @@ public int read() throws IOException {
186186
return n;
187187
}
188188

189+
@Override
190+
public long skip(long len) throws IOException {
191+
long n = in.skip(len);
192+
bytesRead += n;
193+
return n;
194+
}
195+
189196
// Throws an IOException if gets an EOF in the middle of a response.
190197
private void throwIfFalseEOF() throws IOException {
191198
long contentLength = getContentLength();

google-http-client/src/test/java/com/google/api/client/http/javanet/NetHttpResponseTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,18 @@ public void subtestGetContentWithShortRead(int responseCode) throws IOException
9494
assertEquals(ERROR_RESPONSE, new String(buf, 0, bytes, Charset.forName("UTF-8")));
9595
}
9696
}
97+
98+
public void testSkippingBytes() throws IOException {
99+
MockHttpURLConnection connection = new MockHttpURLConnection(null)
100+
.setResponseCode(200)
101+
.setInputStream(new ByteArrayInputStream(StringUtils.getBytesUtf8("0123456789")))
102+
.addHeader("Content-Length", "10");
103+
NetHttpResponse response = new NetHttpResponse(connection);
104+
InputStream is = response.getContent();
105+
// read 1 byte, then skip 9 (to EOF)
106+
assertEquals('0', is.read());
107+
assertEquals(9, is.skip(9));
108+
// expect EOF, not an exception
109+
assertEquals(-1, is.read());
110+
}
97111
}

0 commit comments

Comments
 (0)