• 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 package libcore.io;
18 
19 import java.io.Closeable;
20 import java.io.FileDescriptor;
21 import java.io.IOException;
22 
23 public final class IoUtils {
IoUtils()24     private IoUtils() {
25     }
26 
27     /**
28      * Calls close(2) on 'fd'. Also resets the internal int to -1.
29      */
close(FileDescriptor fd)30     public static native void close(FileDescriptor fd) throws IOException;
31 
32     /**
33      * Closes 'closeable', ignoring any exceptions. Does nothing if 'closeable' is null.
34      */
closeQuietly(Closeable closeable)35     public static void closeQuietly(Closeable closeable) {
36         if (closeable != null) {
37             try {
38                 closeable.close();
39             } catch (IOException ignored) {
40             }
41         }
42     }
43 
44     /**
45      * Returns the int file descriptor from within the given FileDescriptor 'fd'.
46      */
getFd(FileDescriptor fd)47     public static native int getFd(FileDescriptor fd);
48 
49     /**
50      * Returns a new FileDescriptor whose internal integer is set to 'fd'.
51      */
newFileDescriptor(int fd)52     public static FileDescriptor newFileDescriptor(int fd) {
53         FileDescriptor result = new FileDescriptor();
54         setFd(result, fd);
55         return result;
56     }
57 
58     /**
59      * Creates a pipe by calling pipe(2), returning the two file descriptors in
60      * elements 0 and 1 of the array 'fds'. fds[0] is the read end of the pipe.
61      * fds[1] is the write end of the pipe. Throws an appropriate IOException on failure.
62      */
pipe(int[] fds)63     public static native void pipe(int[] fds) throws IOException;
64 
65     /**
66      * Sets the int file descriptor within the given FileDescriptor 'fd' to 'newValue'.
67      */
setFd(FileDescriptor fd, int newValue)68     public static native void setFd(FileDescriptor fd, int newValue);
69 
70     /**
71      * Sets 'fd' to be blocking or non-blocking, according to the state of 'blocking'.
72      */
setBlocking(FileDescriptor fd, boolean blocking)73     public static native void setBlocking(FileDescriptor fd, boolean blocking) throws IOException;
74 }
75