275 n = tryRead(fd, b, off, len);
276 }
277 return n;
278 }
279
280 /**
281 * Reads bytes from the socket into the given byte array.
282 * @return the number of bytes read or -1 at EOF
283 * @throws SocketException if the socket is closed or a socket I/O error occurs
284 * @throws SocketTimeoutException if the read timeout elapses
285 */
286 private int implRead(byte[] b, int off, int len, long remainingNanos) throws IOException {
287 int n = 0;
288 SocketException ex = null;
289 FileDescriptor fd = beginRead();
290 try {
291 if (connectionReset)
292 throw new SocketException("Connection reset");
293 if (isInputClosed)
294 return -1;
295 configureNonBlockingIfNeeded(fd, remainingNanos > 0);
296 if (remainingNanos > 0) {
297 // read with timeout
298 n = timedRead(fd, b, off, len, remainingNanos);
299 } else {
300 // read, no timeout
301 n = tryRead(fd, b, off, len);
302 while (IOStatus.okayToRetry(n) && isOpen()) {
303 park(fd, Net.POLLIN);
304 n = tryRead(fd, b, off, len);
305 }
306 }
307 } catch (InterruptedIOException e) {
308 throw e;
309 } catch (ConnectionResetException e) {
310 connectionReset = true;
311 throw new SocketException("Connection reset");
312 } catch (IOException ioe) {
313 // translate to SocketException to maintain compatibility
314 ex = asSocketException(ioe);
400 ByteBuffer src = Util.getTemporaryDirectBuffer(len);
401 assert src.position() == 0;
402 try {
403 src.put(b, off, len);
404 return nd.write(fd, ((DirectBuffer)src).address(), len);
405 } finally {
406 Util.offerFirstTemporaryDirectBuffer(src);
407 }
408 }
409
410 /**
411 * Writes a sequence of bytes to the socket from the given byte array.
412 * @return the number of bytes written
413 * @throws SocketException if the socket is closed or a socket I/O error occurs
414 */
415 private int implWrite(byte[] b, int off, int len) throws IOException {
416 int n = 0;
417 SocketException ex = null;
418 FileDescriptor fd = beginWrite();
419 try {
420 configureNonBlockingIfNeeded(fd, false);
421 n = tryWrite(fd, b, off, len);
422 while (IOStatus.okayToRetry(n) && isOpen()) {
423 park(fd, Net.POLLOUT);
424 n = tryWrite(fd, b, off, len);
425 }
426 } catch (InterruptedIOException e) {
427 throw e;
428 } catch (IOException ioe) {
429 // translate to SocketException to maintain compatibility
430 ex = asSocketException(ioe);
431 } finally {
432 endWrite(n > 0);
433 }
434 if (ex != null) {
435 throw ex;
436 }
437 return n;
438 }
439
|
275 n = tryRead(fd, b, off, len);
276 }
277 return n;
278 }
279
280 /**
281 * Reads bytes from the socket into the given byte array.
282 * @return the number of bytes read or -1 at EOF
283 * @throws SocketException if the socket is closed or a socket I/O error occurs
284 * @throws SocketTimeoutException if the read timeout elapses
285 */
286 private int implRead(byte[] b, int off, int len, long remainingNanos) throws IOException {
287 int n = 0;
288 SocketException ex = null;
289 FileDescriptor fd = beginRead();
290 try {
291 if (connectionReset)
292 throw new SocketException("Connection reset");
293 if (isInputClosed)
294 return -1;
295
296 // experimental
297 if (Poller.supportReadOps() && Thread.currentThread().isVirtual()) {
298 n = Poller.read(fdVal(fd), b, off, len, remainingNanos, this::isOpen);
299 if (n != IOStatus.UNAVAILABLE) return n;
300 }
301
302 configureNonBlockingIfNeeded(fd, remainingNanos > 0);
303 if (remainingNanos > 0) {
304 // read with timeout
305 n = timedRead(fd, b, off, len, remainingNanos);
306 } else {
307 // read, no timeout
308 n = tryRead(fd, b, off, len);
309 while (IOStatus.okayToRetry(n) && isOpen()) {
310 park(fd, Net.POLLIN);
311 n = tryRead(fd, b, off, len);
312 }
313 }
314 } catch (InterruptedIOException e) {
315 throw e;
316 } catch (ConnectionResetException e) {
317 connectionReset = true;
318 throw new SocketException("Connection reset");
319 } catch (IOException ioe) {
320 // translate to SocketException to maintain compatibility
321 ex = asSocketException(ioe);
407 ByteBuffer src = Util.getTemporaryDirectBuffer(len);
408 assert src.position() == 0;
409 try {
410 src.put(b, off, len);
411 return nd.write(fd, ((DirectBuffer)src).address(), len);
412 } finally {
413 Util.offerFirstTemporaryDirectBuffer(src);
414 }
415 }
416
417 /**
418 * Writes a sequence of bytes to the socket from the given byte array.
419 * @return the number of bytes written
420 * @throws SocketException if the socket is closed or a socket I/O error occurs
421 */
422 private int implWrite(byte[] b, int off, int len) throws IOException {
423 int n = 0;
424 SocketException ex = null;
425 FileDescriptor fd = beginWrite();
426 try {
427
428 // experimental
429 if (Poller.supportWriteOps() && Thread.currentThread().isVirtual()) {
430 n = Poller.write(fdVal(fd), b, off, len, this::isOpen);
431 if (n != IOStatus.UNAVAILABLE) return n;
432 }
433
434 configureNonBlockingIfNeeded(fd, false);
435 n = tryWrite(fd, b, off, len);
436 while (IOStatus.okayToRetry(n) && isOpen()) {
437 park(fd, Net.POLLOUT);
438 n = tryWrite(fd, b, off, len);
439 }
440 } catch (InterruptedIOException e) {
441 throw e;
442 } catch (IOException ioe) {
443 // translate to SocketException to maintain compatibility
444 ex = asSocketException(ioe);
445 } finally {
446 endWrite(n > 0);
447 }
448 if (ex != null) {
449 throw ex;
450 }
451 return n;
452 }
453
|