Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions src/java.base/share/classes/sun/nio/ch/NioSocketImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -289,19 +289,18 @@ private int timedRead(FileDescriptor fd, byte[] b, int off, int len, long nanos)
* @throws SocketException if the socket is closed or a socket I/O error occurs
* @throws SocketTimeoutException if the read timeout elapses
*/
private int implRead(byte[] b, int off, int len) throws IOException {
private int implRead(byte[] b, int off, int len, long remainingNanos) throws IOException {
int n = 0;
FileDescriptor fd = beginRead();
try {
if (connectionReset)
throw new SocketException("Connection reset");
if (isInputClosed)
return -1;
int timeout = this.timeout;
configureNonBlockingIfNeeded(fd, timeout > 0);
if (timeout > 0) {
configureNonBlockingIfNeeded(fd, remainingNanos > 0);
if (remainingNanos > 0) {
// read with timeout
n = timedRead(fd, b, off, len, MILLISECONDS.toNanos(timeout));
n = timedRead(fd, b, off, len, remainingNanos);
} else {
// read, no timeout
n = tryRead(fd, b, off, len);
Expand Down Expand Up @@ -336,14 +335,24 @@ private int read(byte[] b, int off, int len) throws IOException {
if (len == 0) {
return 0;
} else {
readLock.lock();
long remainingNanos = 0;
int timeout = this.timeout;
if (timeout > 0) {
remainingNanos = tryLock(readLock, timeout, MILLISECONDS);
if (remainingNanos <= 0) {
assert !readLock.isHeldByCurrentThread();
throw new SocketTimeoutException("Read timed out");
}
} else {
readLock.lock();
}
try {
// emulate legacy behavior to return -1, even if socket is closed
if (readEOF)
return -1;
// read up to MAX_BUFFER_SIZE bytes
int size = Math.min(len, MAX_BUFFER_SIZE);
int n = implRead(b, off, size);
int n = implRead(b, off, size, remainingNanos);
if (n == -1)
readEOF = true;
return n;
Expand Down
Loading