blob: f3dc5c8bc309b555d85727a02da521a69528ee48 [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).
103 */
104 public NativeHandle dup() throws java.io.IOException {
105 FileDescriptor[] fds = new FileDescriptor[mFds.length];
106 try {
107 for (int i = 0; i < mFds.length; i++) {
108 fds[i] = Os.dup(mFds[i]);
109 }
110 } catch (ErrnoException e) {
111 e.rethrowAsIOException();
112 }
113 return new NativeHandle(fds, mInts, true /*own*/);
114 }
115
116 /**
117 * Closes the file descriptors if they are owned by this object.
118 *
119 * This also invalidates the object.
120 */
121 @Override
122 public void close() throws java.io.IOException {
123 if (!mOwn) {
124 return;
125 }
126
127 try {
128 for (FileDescriptor fd : mFds) {
129 Os.close(fd);
130 }
131 } catch (ErrnoException e) {
132 e.rethrowAsIOException();
133 }
134
135 mOwn = false;
136 mFds = null;
137 mInts = null;
138 }
139
140 /**
141 * Returns the underlying lone file descriptor.
142 *
143 * @return a {@link FileDescriptor} object
144 * @throws IllegalStateException if this object contains either zero or
145 * more than one file descriptor, or a non-empty data stream.
146 */
147 public FileDescriptor getFileDescriptor() {
148 if (!hasSingleFileDescriptor()) {
149 throw new IllegalStateException(
150 "NativeHandle is not single file descriptor. Contents must"
151 + " be retreived through getFileDescriptors and getInts.");
152 }
153
154 return mFds[0];
155 }
156
157 /**
158 * Convenience method for fetching this object's file descriptors from JNI.
159 * @return a mutable copy of the underlying file descriptors (as an int[])
160 *
161 * @hide
162 */
163 private int[] getFdsAsIntArray() {
164 int numFds = mFds.length;
165 int[] fds = new int[numFds];
166
167 for (int i = 0; i < numFds; i++) {
168 fds[i] = mFds[i].getInt$();
169 }
170
171 return fds;
172 }
173
174 /**
175 * Fetch file descriptors.
176 *
177 * @return the fds.
178 */
179 public FileDescriptor[] getFileDescriptors() {
180 return mFds;
181 }
182
183 /**
184 * Fetch opaque ints. Note: This object retains ownership of the data.
185 *
186 * @return the opaque data stream.
187 */
188 public int[] getInts() {
189 return mInts;
190 }
191}