blob: fbecc8ec1cd9f35136bdce17cb7460720ed44b5a [file] [log] [blame]
Nirav Atre9850dd92018-07-24 17:03:44 -07001/*
2 * Copyright (C) 2018 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
17package android.os;
18
19import android.annotation.NonNull;
20import android.annotation.SystemApi;
21import android.system.ErrnoException;
22import android.system.Os;
23
24import java.io.Closeable;
25import java.io.FileDescriptor;
26
27/**
28 * Collection representing a set of open file descriptors and an opaque data stream.
29 *
30 * @hide
31 */
32@SystemApi
33public final class NativeHandle implements Closeable {
34 // whether this object owns mFds
35 private boolean mOwn = false;
36 private FileDescriptor[] mFds;
37 private int[] mInts;
38
39 /**
40 * Constructs a {@link NativeHandle} object containing
41 * zero file descriptors and an empty data stream.
42 */
43 public NativeHandle() {
44 this(new FileDescriptor[0], new int[0], false);
45 }
46
47 /**
48 * Constructs a {@link NativeHandle} object containing the given
49 * {@link FileDescriptor} object and an empty data stream.
50 */
51 public NativeHandle(@NonNull FileDescriptor descriptor, boolean own) {
52 this(new FileDescriptor[] {descriptor}, new int[0], own);
53 }
54
55 /**
56 * Convenience method for creating a list of file descriptors.
57 *
58 * @hide
59 */
60 private static FileDescriptor[] createFileDescriptorArray(@NonNull int[] fds) {
61 FileDescriptor[] list = new FileDescriptor[fds.length];
62 for (int i = 0; i < fds.length; i++) {
63 FileDescriptor descriptor = new FileDescriptor();
64 descriptor.setInt$(fds[i]);
65 list[i] = descriptor;
66 }
67 return list;
68 }
69
70 /**
71 * Convenience method for instantiating a {@link NativeHandle} from JNI. It does
72 * not take ownership of the int[] params. It does not dupe the FileDescriptors.
73 *
74 * @hide
75 */
76 private NativeHandle(@NonNull int[] fds, @NonNull int[] ints, boolean own) {
77 this(createFileDescriptorArray(fds), ints, own);
78 }
79
80 /**
81 * Instantiate an opaque {@link NativeHandle} from fds and integers.
82 *
83 * @param own whether the fds are owned by this object and should be closed
84 */
85 public NativeHandle(@NonNull FileDescriptor[] fds, @NonNull int[] ints, boolean own) {
86 mFds = fds.clone();
87 mInts = ints.clone();
88 mOwn = own;
89 }
90
91 /**
92 * Returns whether this {@link NativeHandle} object contains a single file
93 * descriptor and nothing else.
94 *
95 * @return a boolean value
96 */
97 public boolean hasSingleFileDescriptor() {
98 return mFds.length == 1 && mInts.length == 0;
99 }
100
101 /**
102 * Explicitly duplicate NativeHandle (this dups all file descritptors).
Steven Moreland12a2c0a2018-08-13 13:53:39 -0700103 *
104 * If this method is called, this must also be explicitly closed with
105 * {@link #close()}.
Nirav Atre9850dd92018-07-24 17:03:44 -0700106 */
107 public NativeHandle dup() throws java.io.IOException {
108 FileDescriptor[] fds = new FileDescriptor[mFds.length];
109 try {
110 for (int i = 0; i < mFds.length; i++) {
111 fds[i] = Os.dup(mFds[i]);
112 }
113 } catch (ErrnoException e) {
114 e.rethrowAsIOException();
115 }
116 return new NativeHandle(fds, mInts, true /*own*/);
117 }
118
119 /**
120 * Closes the file descriptors if they are owned by this object.
121 *
122 * This also invalidates the object.
123 */
124 @Override
125 public void close() throws java.io.IOException {
126 if (!mOwn) {
127 return;
128 }
129
130 try {
131 for (FileDescriptor fd : mFds) {
132 Os.close(fd);
133 }
134 } catch (ErrnoException e) {
135 e.rethrowAsIOException();
136 }
137
138 mOwn = false;
139 mFds = null;
140 mInts = null;
141 }
142
143 /**
144 * Returns the underlying lone file descriptor.
145 *
146 * @return a {@link FileDescriptor} object
147 * @throws IllegalStateException if this object contains either zero or
148 * more than one file descriptor, or a non-empty data stream.
149 */
150 public FileDescriptor getFileDescriptor() {
151 if (!hasSingleFileDescriptor()) {
152 throw new IllegalStateException(
153 "NativeHandle is not single file descriptor. Contents must"
154 + " be retreived through getFileDescriptors and getInts.");
155 }
156
157 return mFds[0];
158 }
159
160 /**
161 * Convenience method for fetching this object's file descriptors from JNI.
162 * @return a mutable copy of the underlying file descriptors (as an int[])
163 *
164 * @hide
165 */
166 private int[] getFdsAsIntArray() {
167 int numFds = mFds.length;
168 int[] fds = new int[numFds];
169
170 for (int i = 0; i < numFds; i++) {
171 fds[i] = mFds[i].getInt$();
172 }
173
174 return fds;
175 }
176
177 /**
178 * Fetch file descriptors.
179 *
180 * @return the fds.
181 */
182 public FileDescriptor[] getFileDescriptors() {
183 return mFds;
184 }
185
186 /**
187 * Fetch opaque ints. Note: This object retains ownership of the data.
188 *
189 * @return the opaque data stream.
190 */
191 public int[] getInts() {
192 return mInts;
193 }
194}