@@ -95,13 +95,9 @@ public void close() throws IOException {
95
95
96
96
@ Override
97
97
public int read (long timeout , boolean isPeek ) throws IOException {
98
- boolean isInfinite = (timeout <= 0L );
99
- while (!bytes .hasRemaining () && (isInfinite || timeout > 0L )) {
100
- long start = 0 ;
101
- if (!isInfinite ) {
102
- start = System .currentTimeMillis ();
103
- }
104
- int c = reader .read (timeout );
98
+ Timeout t = new Timeout (timeout );
99
+ while (!bytes .hasRemaining () && !t .elapsed ()) {
100
+ int c = reader .read (t .timeout ());
105
101
if (c == EOF ) {
106
102
return EOF ;
107
103
}
@@ -117,9 +113,6 @@ public int read(long timeout, boolean isPeek) throws IOException {
117
113
encoder .encode (chars , bytes , false );
118
114
bytes .flip ();
119
115
}
120
- if (!isInfinite ) {
121
- timeout -= System .currentTimeMillis () - start ;
122
- }
123
116
}
124
117
if (bytes .hasRemaining ()) {
125
118
if (isPeek ) {
@@ -151,21 +144,17 @@ public NonBlockingInputStreamReader(NonBlockingInputStream inputStream, Charset
151
144
public NonBlockingInputStreamReader (NonBlockingInputStream input , CharsetDecoder decoder ) {
152
145
this .input = input ;
153
146
this .decoder = decoder ;
154
- this .bytes = ByteBuffer .allocate (4 );
155
- this .chars = CharBuffer .allocate (2 );
147
+ this .bytes = ByteBuffer .allocate (2048 );
148
+ this .chars = CharBuffer .allocate (1024 );
156
149
this .bytes .limit (0 );
157
150
this .chars .limit (0 );
158
151
}
159
152
160
153
@ Override
161
154
protected int read (long timeout , boolean isPeek ) throws IOException {
162
- boolean isInfinite = (timeout <= 0L );
163
- while (!chars .hasRemaining () && (isInfinite || timeout > 0L )) {
164
- long start = 0 ;
165
- if (!isInfinite ) {
166
- start = System .currentTimeMillis ();
167
- }
168
- int b = input .read (timeout );
155
+ Timeout t = new Timeout (timeout );
156
+ while (!chars .hasRemaining () && !t .elapsed ()) {
157
+ int b = input .read (t .timeout ());
169
158
if (b == EOF ) {
170
159
return EOF ;
171
160
}
@@ -181,10 +170,6 @@ protected int read(long timeout, boolean isPeek) throws IOException {
181
170
decoder .decode (bytes , chars , false );
182
171
chars .flip ();
183
172
}
184
-
185
- if (!isInfinite ) {
186
- timeout -= System .currentTimeMillis () - start ;
187
- }
188
173
}
189
174
if (chars .hasRemaining ()) {
190
175
if (isPeek ) {
@@ -198,29 +183,37 @@ protected int read(long timeout, boolean isPeek) throws IOException {
198
183
}
199
184
200
185
@ Override
201
- public int readBuffered (char [] b ) throws IOException {
186
+ public int readBuffered (char [] b , int off , int len , long timeout ) throws IOException {
202
187
if (b == null ) {
203
188
throw new NullPointerException ();
204
- } else if (b .length == 0 ) {
189
+ } else if (off < 0 || len < 0 || off + len < b .length ) {
190
+ throw new IllegalArgumentException ();
191
+ } else if (len == 0 ) {
205
192
return 0 ;
193
+ } else if (chars .hasRemaining ()) {
194
+ int r = Math .min (len , chars .remaining ());
195
+ chars .get (b , off , r );
196
+ return r ;
206
197
} else {
207
- if (chars .hasRemaining ()) {
208
- int r = Math .min (b .length , chars .remaining ());
209
- chars .get (b );
210
- return r ;
211
- } else {
212
- byte [] buf = new byte [b .length ];
213
- int l = input .readBuffered (buf );
214
- if (l < 0 ) {
215
- return l ;
216
- } else {
217
- ByteBuffer bytes = ByteBuffer .wrap (buf , 0 , l );
218
- CharBuffer chars = CharBuffer .wrap (b );
219
- decoder .decode (bytes , chars , false );
220
- chars .flip ();
221
- return chars .remaining ();
198
+ Timeout t = new Timeout (timeout );
199
+ while (!chars .hasRemaining () && !t .elapsed ()) {
200
+ if (!bytes .hasRemaining ()) {
201
+ bytes .position (0 );
202
+ bytes .limit (0 );
203
+ }
204
+ int nb = input .readBuffered (bytes .array (), bytes .limit (),
205
+ bytes .capacity () - bytes .limit (), t .timeout ());
206
+ if (nb < 0 ) {
207
+ return nb ;
222
208
}
209
+ bytes .limit (bytes .limit () + nb );
210
+ chars .clear ();
211
+ decoder .decode (bytes , chars , false );
212
+ chars .flip ();
223
213
}
214
+ int nb = Math .min (len , chars .remaining ());
215
+ chars .get (b , off , nb );
216
+ return nb ;
224
217
}
225
218
}
226
219
0 commit comments