8217461: (ch) Add Net.available to return the number of bytes in the socket input buffer
authoralanb
Tue, 22 Jan 2019 16:39:52 +0000
changeset 53422 6f02e036630e
parent 53421 06862c019f3f
child 53423 1ae823617395
8217461: (ch) Add Net.available to return the number of bytes in the socket input buffer Reviewed-by: clanger, michaelm
src/java.base/share/classes/sun/nio/ch/Net.java
src/java.base/share/native/libnet/net_util.h
src/java.base/unix/native/libnet/PlainSocketImpl.c
src/java.base/unix/native/libnet/net_util_md.c
src/java.base/unix/native/libnet/net_util_md.h
src/java.base/unix/native/libnio/ch/Net.c
src/java.base/windows/native/libnet/net_util_md.c
src/java.base/windows/native/libnio/ch/Net.c
--- a/src/java.base/share/classes/sun/nio/ch/Net.java	Thu Jan 03 17:39:39 2019 +0800
+++ b/src/java.base/share/classes/sun/nio/ch/Net.java	Tue Jan 22 16:39:52 2019 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -524,9 +524,13 @@
     static native int poll(FileDescriptor fd, int events, long timeout)
         throws IOException;
 
+    /**
+     * Return the number of bytes in the socket input buffer.
+     */
+    static native int available(FileDescriptor fd) throws IOException;
+
     // -- Multicast support --
 
-
     /**
      * Join IPv4 multicast group
      */
--- a/src/java.base/share/native/libnet/net_util.h	Thu Jan 03 17:39:39 2019 +0800
+++ b/src/java.base/share/native/libnet/net_util.h	Tue Jan 22 16:39:52 2019 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -181,6 +181,9 @@
  */
 
 JNIEXPORT int JNICALL
+NET_SocketAvailable(int fd, int *pbytes);
+
+JNIEXPORT int JNICALL
 NET_GetSockOpt(int fd, int level, int opt, void *result, int *len);
 
 JNIEXPORT int JNICALL
--- a/src/java.base/unix/native/libnet/PlainSocketImpl.c	Thu Jan 03 17:39:39 2019 +0800
+++ b/src/java.base/unix/native/libnet/PlainSocketImpl.c	Tue Jan 22 16:39:52 2019 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -740,8 +740,7 @@
  */
 JNIEXPORT jint JNICALL
 Java_java_net_PlainSocketImpl_socketAvailable(JNIEnv *env, jobject this) {
-
-    jint ret = -1;
+    int count = 0;
     jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
     jint fd;
 
@@ -752,8 +751,7 @@
     } else {
         fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
     }
-    /* NET_SocketAvailable returns 0 for failure, 1 for success */
-    if (NET_SocketAvailable(fd, &ret) == 0){
+    if (NET_SocketAvailable(fd, &count) != 0) {
         if (errno == ECONNRESET) {
             JNU_ThrowByName(env, "sun/net/ConnectionResetException", "");
         } else {
@@ -761,7 +759,7 @@
                 (env, JNU_JAVANETPKG "SocketException", "ioctl FIONREAD failed");
         }
     }
-    return ret;
+    return (jint) count;
 }
 
 /*
--- a/src/java.base/unix/native/libnet/net_util_md.c	Thu Jan 03 17:39:39 2019 +0800
+++ b/src/java.base/unix/native/libnet/net_util_md.c	Tue Jan 22 16:39:52 2019 +0000
@@ -123,12 +123,10 @@
     } while((_result == -1) && (errno == EINTR)); \
 } while(0)
 
-int NET_SocketAvailable(int s, jint *pbytes) {
+int NET_SocketAvailable(int s, int *pbytes) {
     int result;
     RESTARTABLE(ioctl(s, FIONREAD, pbytes), result);
-    // note: ioctl can return 0 when successful, NET_SocketAvailable
-    // is expected to return 0 on failure and 1 on success.
-    return (result == -1) ? 0 : 1;
+    return result;
 }
 
 #ifdef __solaris__
--- a/src/java.base/unix/native/libnet/net_util_md.h	Thu Jan 03 17:39:39 2019 +0800
+++ b/src/java.base/unix/native/libnet/net_util_md.h	Tue Jan 22 16:39:52 2019 +0000
@@ -92,7 +92,6 @@
 int NET_SocketClose(int s);
 int NET_Dup2(int oldfd, int newfd);
 int NET_Poll(struct pollfd *ufds, unsigned int nfds, int timeout);
-int NET_SocketAvailable(int s, jint *pbytes);
 
 void NET_ThrowUnknownHostExceptionWithGaiError(JNIEnv *env,
                                                const char* hostname,
--- a/src/java.base/unix/native/libnio/ch/Net.c	Thu Jan 03 17:39:39 2019 +0800
+++ b/src/java.base/unix/native/libnio/ch/Net.c	Tue Jan 22 16:39:52 2019 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -684,6 +684,17 @@
 }
 
 JNIEXPORT jint JNICALL
+Java_sun_nio_ch_Net_available(JNIEnv *env, jclass cl, jobject fdo)
+{
+    int count = 0;
+    if (NET_SocketAvailable(fdval(env, fdo), &count) != 0) {
+        handleSocketError(env, errno);
+        return IOS_THROWN;
+    }
+    return (jint) count;
+}
+
+JNIEXPORT jint JNICALL
 Java_sun_nio_ch_Net_poll(JNIEnv* env, jclass this, jobject fdo, jint events, jlong timeout)
 {
     struct pollfd pfd;
--- a/src/java.base/windows/native/libnet/net_util_md.c	Thu Jan 03 17:39:39 2019 +0800
+++ b/src/java.base/windows/native/libnet/net_util_md.c	Tue Jan 22 16:39:52 2019 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -398,6 +398,17 @@
     return rv;
 }
 
+JNIEXPORT int JNICALL
+NET_SocketAvailable(int s, int *pbytes) {
+    u_long arg;
+    if (ioctlsocket((SOCKET)s, FIONREAD, &arg) == SOCKET_ERROR) {
+        return -1;
+    } else {
+        *pbytes = (int) arg;
+        return 0;
+    }
+}
+
 /*
  * Sets SO_ECLUSIVEADDRUSE if SO_REUSEADDR is not already set.
  */
--- a/src/java.base/windows/native/libnio/ch/Net.c	Thu Jan 03 17:39:39 2019 +0800
+++ b/src/java.base/windows/native/libnio/ch/Net.c	Tue Jan 22 16:39:52 2019 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2001, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -551,6 +551,17 @@
 }
 
 JNIEXPORT jint JNICALL
+Java_sun_nio_ch_Net_available(JNIEnv *env, jclass cl, jobject fdo)
+{
+    int count = 0;
+    if (NET_SocketAvailable(fdval(env, fdo), &count) != 0) {
+        handleSocketError(env, WSAGetLastError());
+        return IOS_THROWN;
+    }
+    return (jint) count;
+}
+
+JNIEXPORT jint JNICALL
 Java_sun_nio_ch_Net_poll(JNIEnv* env, jclass this, jobject fdo, jint events, jlong timeout)
 {
     int rv;