@@ -30,13 +30,7 @@ public final class InstrHttpInputStream extends InputStream {
3030 private long timeToResponseInitiated ;
3131 private long timeToResponseLastRead = -1 ;
3232
33- /**
34- * Instrumented inputStream object
35- *
36- * @param inputStream
37- * @param builder
38- * @param timer
39- */
33+ /** Instrumented inputStream object */
4034 public InstrHttpInputStream (
4135 final InputStream inputStream , final NetworkRequestMetricBuilder builder , Timer timer ) {
4236 this .timer = timer ;
@@ -99,12 +93,13 @@ public int read() throws IOException {
9993 if (timeToResponseInitiated == -1 ) {
10094 timeToResponseInitiated = tempTime ;
10195 }
102- if (bytesRead == -1 && timeToResponseLastRead == -1 ) {
96+ boolean endOfStream = bytesRead == -1 ;
97+ if (endOfStream && timeToResponseLastRead == -1 ) {
10398 timeToResponseLastRead = tempTime ;
10499 networkMetricBuilder .setTimeToResponseCompletedMicros (timeToResponseLastRead );
105100 networkMetricBuilder .build ();
106101 } else {
107- this . bytesRead ++ ;
102+ incrementBytesRead ( 1 ) ;
108103 networkMetricBuilder .setResponsePayloadBytes (this .bytesRead );
109104 }
110105 return bytesRead ;
@@ -124,12 +119,13 @@ public int read(final byte[] buffer, final int byteOffset, final int byteCount)
124119 if (timeToResponseInitiated == -1 ) {
125120 timeToResponseInitiated = tempTime ;
126121 }
127- if (bytesRead == -1 && timeToResponseLastRead == -1 ) {
122+ boolean endOfStream = bytesRead == -1 ;
123+ if (endOfStream && timeToResponseLastRead == -1 ) {
128124 timeToResponseLastRead = tempTime ;
129125 networkMetricBuilder .setTimeToResponseCompletedMicros (timeToResponseLastRead );
130126 networkMetricBuilder .build ();
131127 } else {
132- this . bytesRead += bytesRead ;
128+ incrementBytesRead ( bytesRead ) ;
133129 networkMetricBuilder .setResponsePayloadBytes (this .bytesRead );
134130 }
135131 return bytesRead ;
@@ -148,12 +144,13 @@ public int read(final byte[] buffer) throws IOException {
148144 if (timeToResponseInitiated == -1 ) {
149145 timeToResponseInitiated = tempTime ;
150146 }
151- if (bytesRead == -1 && timeToResponseLastRead == -1 ) {
147+ boolean endOfStream = bytesRead == -1 ;
148+ if (endOfStream && timeToResponseLastRead == -1 ) {
152149 timeToResponseLastRead = tempTime ;
153150 networkMetricBuilder .setTimeToResponseCompletedMicros (timeToResponseLastRead );
154151 networkMetricBuilder .build ();
155152 } else {
156- this . bytesRead += bytesRead ;
153+ incrementBytesRead ( bytesRead ) ;
157154 networkMetricBuilder .setResponsePayloadBytes (this .bytesRead );
158155 }
159156 return bytesRead ;
@@ -183,11 +180,13 @@ public long skip(final long byteCount) throws IOException {
183180 if (timeToResponseInitiated == -1 ) {
184181 timeToResponseInitiated = tempTime ;
185182 }
186- if (skipped == -1 && timeToResponseLastRead == -1 ) {
183+ // InputStream.skip will return 0 for both end of stream and for 0 bytes skipped.
184+ boolean endOfStream = (skipped == 0 && byteCount != 0 );
185+ if (endOfStream && timeToResponseLastRead == -1 ) {
187186 timeToResponseLastRead = tempTime ;
188187 networkMetricBuilder .setTimeToResponseCompletedMicros (timeToResponseLastRead );
189188 } else {
190- bytesRead += skipped ;
189+ incrementBytesRead ( skipped ) ;
191190 networkMetricBuilder .setResponsePayloadBytes (bytesRead );
192191 }
193192 return skipped ;
@@ -197,4 +196,12 @@ public long skip(final long byteCount) throws IOException {
197196 throw e ;
198197 }
199198 }
199+
200+ private void incrementBytesRead (long bytesRead ) {
201+ if (this .bytesRead == -1 ) {
202+ this .bytesRead = bytesRead ;
203+ } else {
204+ this .bytesRead += bytesRead ;
205+ }
206+ }
200207}
0 commit comments