@@ -30,13 +30,7 @@ public final class InstrHttpInputStream extends InputStream {
30
30
private long timeToResponseInitiated ;
31
31
private long timeToResponseLastRead = -1 ;
32
32
33
- /**
34
- * Instrumented inputStream object
35
- *
36
- * @param inputStream
37
- * @param builder
38
- * @param timer
39
- */
33
+ /** Instrumented inputStream object */
40
34
public InstrHttpInputStream (
41
35
final InputStream inputStream , final NetworkRequestMetricBuilder builder , Timer timer ) {
42
36
this .timer = timer ;
@@ -99,12 +93,13 @@ public int read() throws IOException {
99
93
if (timeToResponseInitiated == -1 ) {
100
94
timeToResponseInitiated = tempTime ;
101
95
}
102
- if (bytesRead == -1 && timeToResponseLastRead == -1 ) {
96
+ boolean endOfStream = bytesRead == -1 ;
97
+ if (endOfStream && timeToResponseLastRead == -1 ) {
103
98
timeToResponseLastRead = tempTime ;
104
99
networkMetricBuilder .setTimeToResponseCompletedMicros (timeToResponseLastRead );
105
100
networkMetricBuilder .build ();
106
101
} else {
107
- this . bytesRead ++ ;
102
+ incrementBytesRead ( 1 ) ;
108
103
networkMetricBuilder .setResponsePayloadBytes (this .bytesRead );
109
104
}
110
105
return bytesRead ;
@@ -124,12 +119,13 @@ public int read(final byte[] buffer, final int byteOffset, final int byteCount)
124
119
if (timeToResponseInitiated == -1 ) {
125
120
timeToResponseInitiated = tempTime ;
126
121
}
127
- if (bytesRead == -1 && timeToResponseLastRead == -1 ) {
122
+ boolean endOfStream = bytesRead == -1 ;
123
+ if (endOfStream && timeToResponseLastRead == -1 ) {
128
124
timeToResponseLastRead = tempTime ;
129
125
networkMetricBuilder .setTimeToResponseCompletedMicros (timeToResponseLastRead );
130
126
networkMetricBuilder .build ();
131
127
} else {
132
- this . bytesRead += bytesRead ;
128
+ incrementBytesRead ( bytesRead ) ;
133
129
networkMetricBuilder .setResponsePayloadBytes (this .bytesRead );
134
130
}
135
131
return bytesRead ;
@@ -148,12 +144,13 @@ public int read(final byte[] buffer) throws IOException {
148
144
if (timeToResponseInitiated == -1 ) {
149
145
timeToResponseInitiated = tempTime ;
150
146
}
151
- if (bytesRead == -1 && timeToResponseLastRead == -1 ) {
147
+ boolean endOfStream = bytesRead == -1 ;
148
+ if (endOfStream && timeToResponseLastRead == -1 ) {
152
149
timeToResponseLastRead = tempTime ;
153
150
networkMetricBuilder .setTimeToResponseCompletedMicros (timeToResponseLastRead );
154
151
networkMetricBuilder .build ();
155
152
} else {
156
- this . bytesRead += bytesRead ;
153
+ incrementBytesRead ( bytesRead ) ;
157
154
networkMetricBuilder .setResponsePayloadBytes (this .bytesRead );
158
155
}
159
156
return bytesRead ;
@@ -183,11 +180,13 @@ public long skip(final long byteCount) throws IOException {
183
180
if (timeToResponseInitiated == -1 ) {
184
181
timeToResponseInitiated = tempTime ;
185
182
}
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 ) {
187
186
timeToResponseLastRead = tempTime ;
188
187
networkMetricBuilder .setTimeToResponseCompletedMicros (timeToResponseLastRead );
189
188
} else {
190
- bytesRead += skipped ;
189
+ incrementBytesRead ( skipped ) ;
191
190
networkMetricBuilder .setResponsePayloadBytes (bytesRead );
192
191
}
193
192
return skipped ;
@@ -197,4 +196,12 @@ public long skip(final long byteCount) throws IOException {
197
196
throw e ;
198
197
}
199
198
}
199
+
200
+ private void incrementBytesRead (long bytesRead ) {
201
+ if (this .bytesRead == -1 ) {
202
+ this .bytesRead = bytesRead ;
203
+ } else {
204
+ this .bytesRead += bytesRead ;
205
+ }
206
+ }
200
207
}
0 commit comments