< prev index next >

src/java.base/unix/native/libnio/ch/IOUtil.c

Print this page
rev 49271 : [mq]: selector-cleanup
   1 /*
   2  * Copyright (c) 2000, 2015, 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


  87 Java_sun_nio_ch_IOUtil_makePipe(JNIEnv *env, jobject this, jboolean blocking)
  88 {
  89     int fd[2];
  90 
  91     if (pipe(fd) < 0) {
  92         JNU_ThrowIOExceptionWithLastError(env, "Pipe failed");
  93         return 0;
  94     }
  95     if (blocking == JNI_FALSE) {
  96         if ((configureBlocking(fd[0], JNI_FALSE) < 0)
  97             || (configureBlocking(fd[1], JNI_FALSE) < 0)) {
  98             JNU_ThrowIOExceptionWithLastError(env, "Configure blocking failed");
  99             close(fd[0]);
 100             close(fd[1]);
 101             return 0;
 102         }
 103     }
 104     return ((jlong) fd[0] << 32) | (jlong) fd[1];
 105 }
 106 









 107 JNIEXPORT jboolean JNICALL
 108 Java_sun_nio_ch_IOUtil_drain(JNIEnv *env, jclass cl, jint fd)
 109 {
 110     char buf[128];
 111     int tn = 0;
 112 
 113     for (;;) {
 114         int n = read(fd, buf, sizeof(buf));
 115         tn += n;
 116         if ((n < 0) && (errno != EAGAIN))
 117             JNU_ThrowIOExceptionWithLastError(env, "Drain");
 118         if (n == (int)sizeof(buf))
 119             continue;
 120         return (tn > 0) ? JNI_TRUE : JNI_FALSE;
 121     }
 122 }
 123 
 124 JNIEXPORT jint JNICALL
 125 Java_sun_nio_ch_IOUtil_fdLimit(JNIEnv *env, jclass this)
 126 {
 127     struct rlimit rlp;
 128     if (getrlimit(RLIMIT_NOFILE, &rlp) < 0) {
 129         JNU_ThrowIOExceptionWithLastError(env, "getrlimit failed");
 130         return -1;


   1 /*
   2  * Copyright (c) 2000, 2018, 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


  87 Java_sun_nio_ch_IOUtil_makePipe(JNIEnv *env, jobject this, jboolean blocking)
  88 {
  89     int fd[2];
  90 
  91     if (pipe(fd) < 0) {
  92         JNU_ThrowIOExceptionWithLastError(env, "Pipe failed");
  93         return 0;
  94     }
  95     if (blocking == JNI_FALSE) {
  96         if ((configureBlocking(fd[0], JNI_FALSE) < 0)
  97             || (configureBlocking(fd[1], JNI_FALSE) < 0)) {
  98             JNU_ThrowIOExceptionWithLastError(env, "Configure blocking failed");
  99             close(fd[0]);
 100             close(fd[1]);
 101             return 0;
 102         }
 103     }
 104     return ((jlong) fd[0] << 32) | (jlong) fd[1];
 105 }
 106 
 107 
 108 JNIEXPORT jint JNICALL
 109 Java_sun_nio_ch_IOUtil_write1(JNIEnv *env, jclass cl, jint fd, jbyte b)
 110 {
 111     char c = (char)b;
 112     return convertReturnVal(env, write(fd, &c, 1), JNI_FALSE);
 113 }
 114 
 115 
 116 JNIEXPORT jboolean JNICALL
 117 Java_sun_nio_ch_IOUtil_drain(JNIEnv *env, jclass cl, jint fd)
 118 {
 119     char buf[16];
 120     int tn = 0;
 121 
 122     for (;;) {
 123         int n = read(fd, buf, sizeof(buf));
 124         tn += n;
 125         if ((n < 0) && (errno != EAGAIN))
 126             JNU_ThrowIOExceptionWithLastError(env, "Drain");
 127         if (n == (int)sizeof(buf))
 128             continue;
 129         return (tn > 0) ? JNI_TRUE : JNI_FALSE;
 130     }
 131 }
 132 
 133 JNIEXPORT jint JNICALL
 134 Java_sun_nio_ch_IOUtil_fdLimit(JNIEnv *env, jclass this)
 135 {
 136     struct rlimit rlp;
 137     if (getrlimit(RLIMIT_NOFILE, &rlp) < 0) {
 138         JNU_ThrowIOExceptionWithLastError(env, "getrlimit failed");
 139         return -1;


< prev index next >