8217461: (ch) Add Net.available to return the number of bytes in the socket input buffer

Reviewed-by: clanger, michaelm
This commit is contained in:
Alan Bateman 2019-01-22 16:39:52 +00:00
parent aa5637f24e
commit 755872aa82
8 changed files with 52 additions and 17 deletions

View File

@ -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,8 +524,12 @@ public class Net {
static native int poll(FileDescriptor fd, int events, long timeout)
throws IOException;
// -- Multicast support --
/**
* Return the number of bytes in the socket input buffer.
*/
static native int available(FileDescriptor fd) throws IOException;
// -- Multicast support --
/**
* Join IPv4 multicast group

View File

@ -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
@ -180,6 +180,9 @@ int NET_IsZeroAddr(jbyte* caddr);
* platform-specific pre/post processing of the arguments and/or results.
*/
JNIEXPORT int JNICALL
NET_SocketAvailable(int fd, int *pbytes);
JNIEXPORT int JNICALL
NET_GetSockOpt(int fd, int level, int opt, void *result, int *len);

View File

@ -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 @@ Java_java_net_PlainSocketImpl_socketAccept(JNIEnv *env, jobject this,
*/
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 @@ Java_java_net_PlainSocketImpl_socketAvailable(JNIEnv *env, jobject this) {
} 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 @@ Java_java_net_PlainSocketImpl_socketAvailable(JNIEnv *env, jobject this) {
(env, JNU_JAVANETPKG "SocketException", "ioctl FIONREAD failed");
}
}
return ret;
return (jint) count;
}
/*

View File

@ -123,12 +123,10 @@ int getDefaultScopeID(JNIEnv *env) {
} 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__

View File

@ -92,7 +92,6 @@ int NET_Accept(int s, struct sockaddr *addr, socklen_t *addrlen);
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,

View File

@ -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
@ -683,6 +683,17 @@ Java_sun_nio_ch_Net_shutdown(JNIEnv *env, jclass cl, jobject fdo, jint jhow)
handleSocketError(env, errno);
}
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)
{

View File

@ -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 @@ NET_GetSockOpt(int s, int level, int optname, void *optval,
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.
*/

View File

@ -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
@ -550,6 +550,17 @@ Java_sun_nio_ch_Net_shutdown(JNIEnv *env, jclass cl, jobject fdo, jint jhow) {
}
}
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)
{