下面列出了android.os.ParcelFileDescriptor#dup ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@TargetApi(13)
public void setDataSource(FileDescriptor fd) throws IOException, IllegalArgumentException, IllegalStateException {
if (VERSION.SDK_INT < 12) {
try {
Field f = fd.getClass().getDeclaredField("descriptor");
f.setAccessible(true);
_setDataSourceFd(f.getInt(fd));
return;
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e2) {
throw new RuntimeException(e2);
}
}
ParcelFileDescriptor pfd = ParcelFileDescriptor.dup(fd);
try {
_setDataSourceFd(pfd.getFd());
} finally {
pfd.close();
}
}
@TargetApi(13)
public void setDataSource(FileDescriptor fd) throws IOException, IllegalArgumentException, IllegalStateException {
if (VERSION.SDK_INT < 12) {
try {
Field f = fd.getClass().getDeclaredField("descriptor");
f.setAccessible(true);
_setDataSourceFd(f.getInt(fd));
return;
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e2) {
throw new RuntimeException(e2);
}
}
ParcelFileDescriptor pfd = ParcelFileDescriptor.dup(fd);
try {
_setDataSourceFd(pfd.getFd());
} finally {
pfd.close();
}
}
private ParcelFileDescriptor openReadInternal(String name) throws IOException {
assertPreparedAndNotSealed("openRead");
try {
if (!FileUtils.isValidExtFilename(name)) {
throw new IllegalArgumentException("Invalid name: " + name);
}
final File target = new File(resolveStageDir(), name);
final FileDescriptor targetFd = Os.open(target.getAbsolutePath(), O_RDONLY, 0);
return ParcelFileDescriptor.dup(targetFd);
} catch (ErrnoException e) {
throw new IOException(e);
}
}
public void dumpService(FileDescriptor fd, IBinder servicetoken, String[] args) {
DumpComponentInfo data = new DumpComponentInfo();
try {
data.fd = ParcelFileDescriptor.dup(fd);
data.token = servicetoken;
data.args = args;
queueOrSendMessage(H.DUMP_SERVICE, data);
} catch (IOException e) {
Slog.w(TAG, "dumpService failed", e);
}
}
public void dumpActivity(FileDescriptor fd, IBinder activitytoken,
String prefix, String[] args) {
DumpComponentInfo data = new DumpComponentInfo();
try {
data.fd = ParcelFileDescriptor.dup(fd);
data.token = activitytoken;
data.prefix = prefix;
data.args = args;
queueOrSendMessage(H.DUMP_ACTIVITY, data);
} catch (IOException e) {
Slog.w(TAG, "dumpActivity failed", e);
}
}
public void dumpProvider(FileDescriptor fd, IBinder providertoken,
String[] args) {
DumpComponentInfo data = new DumpComponentInfo();
try {
data.fd = ParcelFileDescriptor.dup(fd);
data.token = providertoken;
data.args = args;
queueOrSendMessage(H.DUMP_PROVIDER, data);
} catch (IOException e) {
Slog.w(TAG, "dumpProvider failed", e);
}
}
public void dumpService(FileDescriptor fd, IBinder servicetoken, String[] args) {
DumpComponentInfo data = new DumpComponentInfo();
try {
data.fd = ParcelFileDescriptor.dup(fd);
data.token = servicetoken;
data.args = args;
queueOrSendMessage(H.DUMP_SERVICE, data);
} catch (IOException e) {
Slog.w(TAG, "dumpService failed", e);
}
}
public void dumpActivity(FileDescriptor fd, IBinder activitytoken,
String prefix, String[] args) {
DumpComponentInfo data = new DumpComponentInfo();
try {
data.fd = ParcelFileDescriptor.dup(fd);
data.token = activitytoken;
data.prefix = prefix;
data.args = args;
queueOrSendMessage(H.DUMP_ACTIVITY, data);
} catch (IOException e) {
Slog.w(TAG, "dumpActivity failed", e);
}
}
@Override
public void onSuccess(ParcelFileDescriptor profileReadFd) {
mSuccess = true;
try {
// We need to dup the descriptor. We are in the same process as system server
// and we will be receiving the same object (which will be closed on the
// server side).
mProfileReadFd = profileReadFd.dup();
} catch (IOException e) {
e.printStackTrace();
}
mDoneSignal.countDown();
}
public IpSecUdpEncapResponse(int inStatus, int inResourceId, int inPort, FileDescriptor inFd)
throws IOException {
if (inStatus == IpSecManager.Status.OK && inFd == null) {
throw new IllegalArgumentException("Valid status implies FD must be non-null");
}
status = inStatus;
resourceId = inResourceId;
port = inPort;
fileDescriptor = (status == IpSecManager.Status.OK) ? ParcelFileDescriptor.dup(inFd) : null;
}
public static ParcelFileDescriptor dupSilently(FileDescriptor fd) {
try {
return ParcelFileDescriptor.dup(fd);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
private ParcelFileDescriptor openWriteInternal(String name, long offsetBytes, long lengthBytes)
throws IOException {
// Quick sanity check of state, and allocate a pipe for ourselves. We
// then do heavy disk allocation outside the lock, but this open pipe
// will block any attempted install transitions.
final FileBridge bridge;
synchronized (mLock) {
assertPreparedAndNotSealed("openWrite");
bridge = new FileBridge();
mBridges.add(bridge);
}
try {
final File target = new File(resolveStageDir(), name);
// TODO: this should delegate to DCS so the system process avoids
// holding open FDs into containers.
final FileDescriptor targetFd = Os.open(target.getAbsolutePath(),
O_CREAT | O_WRONLY, 0644);
// If caller specified a total length, allocate it for them. Free up
// cache space to grow, if needed.
if (lengthBytes > 0) {
Os.posix_fallocate(targetFd, 0, lengthBytes);
}
if (offsetBytes > 0) {
Os.lseek(targetFd, offsetBytes, OsConstants.SEEK_SET);
}
bridge.setTargetFile(targetFd);
bridge.start();
return ParcelFileDescriptor.dup(bridge.getClientSocket());
} catch (ErrnoException e) {
throw new IOException(e);
}
}
private static Object marshallParamater(Class type,Object obj) {
if (FileDescriptor.class.equals(type) && obj instanceof FileDescriptor) {
try {
return ParcelFileDescriptor.dup(((FileDescriptor) obj));
} catch (IOException e) {
e.printStackTrace();
}
}
return obj;
}