| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.os; |
| 18 |
|
| 19 |
import static android.system.OsConstants.F_DUPFD_CLOEXEC; |
| 20 |
|
| 21 |
import android.annotation.NonNull; |
| 22 |
import android.annotation.SystemApi; |
| 23 |
import android.system.ErrnoException; |
| 24 |
import android.system.Os; |
| 25 |
|
| 26 |
import java.io.Closeable; |
| 27 |
import java.io.FileDescriptor; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
@SystemApi |
| 35 |
public final class NativeHandle implements Closeable { |
| 36 |
|
| 37 |
private boolean mOwn = false; |
| 38 |
private FileDescriptor[] mFds; |
| 39 |
private int[] mInts; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public NativeHandle() { |
| 46 |
this(new FileDescriptor[0], new int[0], false); |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public NativeHandle(@NonNull FileDescriptor descriptor, boolean own) { |
| 54 |
this(new FileDescriptor[] {descriptor}, new int[0], own); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
private static FileDescriptor[] createFileDescriptorArray(@NonNull int[] fds) { |
| 63 |
FileDescriptor[] list = new FileDescriptor[fds.length]; |
| 64 |
for (int i = 0; i < fds.length; i++) { |
| 65 |
FileDescriptor descriptor = new FileDescriptor(); |
| 66 |
descriptor.setInt$(fds[i]); |
| 67 |
list[i] = descriptor; |
| 68 |
} |
| 69 |
return list; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
private NativeHandle(@NonNull int[] fds, @NonNull int[] ints, boolean own) { |
| 79 |
this(createFileDescriptorArray(fds), ints, own); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public NativeHandle(@NonNull FileDescriptor[] fds, @NonNull int[] ints, boolean own) { |
| 88 |
mFds = fds.clone(); |
| 89 |
mInts = ints.clone(); |
| 90 |
mOwn = own; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public boolean hasSingleFileDescriptor() { |
| 100 |
checkOpen(); |
| 101 |
|
| 102 |
return mFds.length == 1 && mInts.length == 0; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public @NonNull NativeHandle dup() throws java.io.IOException { |
| 112 |
FileDescriptor[] fds = new FileDescriptor[mFds.length]; |
| 113 |
try { |
| 114 |
for (int i = 0; i < mFds.length; i++) { |
| 115 |
FileDescriptor newFd = new FileDescriptor(); |
| 116 |
int fdint = Os.fcntlInt(mFds[i], F_DUPFD_CLOEXEC, 0); |
| 117 |
newFd.setInt$(fdint); |
| 118 |
fds[i] = newFd; |
| 119 |
} |
| 120 |
} catch (ErrnoException e) { |
| 121 |
e.rethrowAsIOException(); |
| 122 |
} |
| 123 |
return new NativeHandle(fds, mInts, true ); |
| 124 |
} |
| 125 |
|
| 126 |
private void checkOpen() { |
| 127 |
if (mFds == null) { |
| 128 |
throw new IllegalStateException("NativeHandle is invalidated after close."); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
@Override |
| 138 |
public void close() throws java.io.IOException { |
| 139 |
checkOpen(); |
| 140 |
|
| 141 |
if (mOwn) { |
| 142 |
try { |
| 143 |
for (FileDescriptor fd : mFds) { |
| 144 |
Os.close(fd); |
| 145 |
} |
| 146 |
} catch (ErrnoException e) { |
| 147 |
e.rethrowAsIOException(); |
| 148 |
} |
| 149 |
|
| 150 |
mOwn = false; |
| 151 |
} |
| 152 |
|
| 153 |
mFds = null; |
| 154 |
mInts = null; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
public @NonNull FileDescriptor getFileDescriptor() { |
| 165 |
checkOpen(); |
| 166 |
|
| 167 |
if (!hasSingleFileDescriptor()) { |
| 168 |
throw new IllegalStateException( |
| 169 |
"NativeHandle is not single file descriptor. Contents must" |
| 170 |
+ " be retreived through getFileDescriptors and getInts."); |
| 171 |
} |
| 172 |
|
| 173 |
return mFds[0]; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
private int[] getFdsAsIntArray() { |
| 183 |
checkOpen(); |
| 184 |
|
| 185 |
int numFds = mFds.length; |
| 186 |
int[] fds = new int[numFds]; |
| 187 |
|
| 188 |
for (int i = 0; i < numFds; i++) { |
| 189 |
fds[i] = mFds[i].getInt$(); |
| 190 |
} |
| 191 |
|
| 192 |
return fds; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
public @NonNull FileDescriptor[] getFileDescriptors() { |
| 201 |
checkOpen(); |
| 202 |
|
| 203 |
return mFds; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
public @NonNull int[] getInts() { |
| 212 |
checkOpen(); |
| 213 |
|
| 214 |
return mInts; |
| 215 |
} |
| 216 |
} |