< prev index next >

src/java.base/unix/native/libnet/SocketInputStream.c

Print this page
rev 14890 : 8158023: SocketExceptions contain too little information sometimes
   1 /*
   2  * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  94         if (bufP == NULL) {
  95             bufP = BUF;
  96             len = MAX_BUFFER_LEN;
  97         }
  98     } else {
  99         bufP = BUF;
 100     }
 101 
 102     if (timeout) {
 103         nread = NET_Timeout(fd, timeout);
 104         if (nread <= 0) {
 105             if (nread == 0) {
 106                 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
 107                             "Read timed out");
 108             } else if (nread == -1) {
 109                 if (errno == EBADF) {
 110                      JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
 111                 } else if (errno == ENOMEM) {
 112                     JNU_ThrowOutOfMemoryError(env, "NET_Timeout native heap allocation failed");
 113                 } else {
 114                     NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
 115                                                   "select/poll failed");
 116                 }
 117             }
 118             if (bufP != BUF) {
 119                 free(bufP);
 120             }
 121             return -1;
 122         }
 123     }
 124 
 125     nread = NET_Read(fd, bufP, len);
 126 
 127     if (nread <= 0) {
 128         if (nread < 0) {
 129 
 130             switch (errno) {
 131                 case ECONNRESET:
 132                 case EPIPE:
 133                     JNU_ThrowByName(env, "sun/net/ConnectionResetException",
 134                         "Connection reset");
 135                     break;
 136 
 137                 case EBADF:
 138                     JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
 139                         "Socket closed");
 140                     break;
 141 
 142                 case EINTR:
 143                      JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException",
 144                            "Operation interrupted");
 145                      break;
 146 
 147                 default:
 148                     NET_ThrowByNameWithLastError(env,
 149                         JNU_JAVANETPKG "SocketException", "Read failed");
 150             }
 151         }
 152     } else {
 153         (*env)->SetByteArrayRegion(env, data, off, nread, (jbyte *)bufP);
 154     }
 155 
 156     if (bufP != BUF) {
 157         free(bufP);
 158     }
 159     return nread;
 160 }
   1 /*
   2  * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  94         if (bufP == NULL) {
  95             bufP = BUF;
  96             len = MAX_BUFFER_LEN;
  97         }
  98     } else {
  99         bufP = BUF;
 100     }
 101 
 102     if (timeout) {
 103         nread = NET_Timeout(fd, timeout);
 104         if (nread <= 0) {
 105             if (nread == 0) {
 106                 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
 107                             "Read timed out");
 108             } else if (nread == -1) {
 109                 if (errno == EBADF) {
 110                      JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
 111                 } else if (errno == ENOMEM) {
 112                     JNU_ThrowOutOfMemoryError(env, "NET_Timeout native heap allocation failed");
 113                 } else {
 114                     JNU_ThrowByNameWithMessageAndLastError
 115                         (env, JNU_JAVANETPKG "SocketException", "select/poll failed");
 116                 }
 117             }
 118             if (bufP != BUF) {
 119                 free(bufP);
 120             }
 121             return -1;
 122         }
 123     }
 124 
 125     nread = NET_Read(fd, bufP, len);
 126 
 127     if (nread <= 0) {
 128         if (nread < 0) {
 129 
 130             switch (errno) {
 131                 case ECONNRESET:
 132                 case EPIPE:
 133                     JNU_ThrowByName(env, "sun/net/ConnectionResetException",
 134                         "Connection reset");
 135                     break;
 136 
 137                 case EBADF:
 138                     JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
 139                         "Socket closed");
 140                     break;
 141 
 142                 case EINTR:
 143                      JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException",
 144                            "Operation interrupted");
 145                      break;
 146 
 147                 default:
 148                     JNU_ThrowByNameWithMessageAndLastError
 149                         (env, JNU_JAVANETPKG "SocketException", "Read failed");
 150             }
 151         }
 152     } else {
 153         (*env)->SetByteArrayRegion(env, data, off, nread, (jbyte *)bufP);
 154     }
 155 
 156     if (bufP != BUF) {
 157         free(bufP);
 158     }
 159     return nread;
 160 }
< prev index next >