• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "IoUtils"
18 
19 #include "JNIHelp.h"
20 #include "JniConstants.h"
21 #include "NetworkUtilities.h"
22 #include "ScopedPrimitiveArray.h"
23 
24 #include <errno.h>
25 #include <unistd.h>
26 
IoUtils_close(JNIEnv * env,jclass,jobject fileDescriptor)27 static void IoUtils_close(JNIEnv* env, jclass, jobject fileDescriptor) {
28     int fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
29     jint rc = TEMP_FAILURE_RETRY(close(fd));
30     if (rc == -1) {
31         jniThrowIOException(env, errno);
32     }
33     jniSetFileDescriptorOfFD(env, fileDescriptor, -1);
34 }
35 
IoUtils_getFd(JNIEnv * env,jclass,jobject fileDescriptor)36 static jint IoUtils_getFd(JNIEnv* env, jclass, jobject fileDescriptor) {
37     return jniGetFDFromFileDescriptor(env, fileDescriptor);
38 }
39 
IoUtils_pipe(JNIEnv * env,jclass,jintArray javaFds)40 static void IoUtils_pipe(JNIEnv* env, jclass, jintArray javaFds) {
41     ScopedIntArrayRW fds(env, javaFds);
42     if (fds.get() == NULL) {
43         return;
44     }
45     int rc = pipe(&fds[0]);
46     if (rc == -1) {
47         jniThrowIOException(env, errno);
48         return;
49     }
50 }
51 
IoUtils_setFd(JNIEnv * env,jclass,jobject fileDescriptor,jint newValue)52 static void IoUtils_setFd(JNIEnv* env, jclass, jobject fileDescriptor, jint newValue) {
53     return jniSetFileDescriptorOfFD(env, fileDescriptor, newValue);
54 }
55 
IoUtils_setBlocking(JNIEnv * env,jclass,jobject fileDescriptor,jboolean blocking)56 static void IoUtils_setBlocking(JNIEnv* env, jclass, jobject fileDescriptor, jboolean blocking) {
57     int fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
58     if (fd == -1) {
59         return;
60     }
61     if (!setBlocking(fd, blocking)) {
62         jniThrowIOException(env, errno);
63     }
64 }
65 
66 static JNINativeMethod gMethods[] = {
67     NATIVE_METHOD(IoUtils, close, "(Ljava/io/FileDescriptor;)V"),
68     NATIVE_METHOD(IoUtils, getFd, "(Ljava/io/FileDescriptor;)I"),
69     NATIVE_METHOD(IoUtils, pipe, "([I)V"),
70     NATIVE_METHOD(IoUtils, setFd, "(Ljava/io/FileDescriptor;I)V"),
71     NATIVE_METHOD(IoUtils, setBlocking, "(Ljava/io/FileDescriptor;Z)V"),
72 };
register_libcore_io_IoUtils(JNIEnv * env)73 int register_libcore_io_IoUtils(JNIEnv* env) {
74     return jniRegisterNativeMethods(env, "libcore/io/IoUtils", gMethods, NELEM(gMethods));
75 }
76