use of java.io.FileDescriptor in project GalleryFinal by pengjianbo.
the class CropUtil method getFromMediaUriPfd.
@Nullable
private static File getFromMediaUriPfd(Context context, ContentResolver resolver, Uri uri) {
if (uri == null)
return null;
FileInputStream input = null;
FileOutputStream output = null;
try {
ParcelFileDescriptor pfd = resolver.openFileDescriptor(uri, "r");
FileDescriptor fd = pfd.getFileDescriptor();
input = new FileInputStream(fd);
String tempFilename = getTempFilename(context);
output = new FileOutputStream(tempFilename);
int read;
byte[] bytes = new byte[4096];
while ((read = input.read(bytes)) != -1) {
output.write(bytes, 0, read);
}
return new File(tempFilename);
} catch (IOException ignored) {
// Nothing we can do
} finally {
closeSilently(input);
closeSilently(output);
}
return null;
}
use of java.io.FileDescriptor in project XobotOS by xamarin.
the class ParcelFileDescriptor method open.
/**
* Create a new ParcelFileDescriptor accessing a given file.
*
* @param file The file to be opened.
* @param mode The desired access mode, must be one of
* {@link #MODE_READ_ONLY}, {@link #MODE_WRITE_ONLY}, or
* {@link #MODE_READ_WRITE}; may also be any combination of
* {@link #MODE_CREATE}, {@link #MODE_TRUNCATE},
* {@link #MODE_WORLD_READABLE}, and {@link #MODE_WORLD_WRITEABLE}.
*
* @return Returns a new ParcelFileDescriptor pointing to the given
* file.
*
* @throws FileNotFoundException Throws FileNotFoundException if the given
* file does not exist or can not be opened with the requested mode.
*/
public static ParcelFileDescriptor open(File file, int mode) throws FileNotFoundException {
String path = file.getPath();
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkRead(path);
if ((mode & MODE_WRITE_ONLY) != 0) {
security.checkWrite(path);
}
}
if ((mode & MODE_READ_WRITE) == 0) {
throw new IllegalArgumentException("Must specify MODE_READ_ONLY, MODE_WRITE_ONLY, or MODE_READ_WRITE");
}
FileDescriptor fd = Parcel.openFileDescriptor(path, mode);
return fd != null ? new ParcelFileDescriptor(fd) : null;
}
use of java.io.FileDescriptor in project XobotOS by xamarin.
the class ParcelFileDescriptor method fromData.
/**
* @hide Please use createPipe() or ContentProvider.openPipeHelper().
* Gets a file descriptor for a read-only copy of the given data.
*
* @param data Data to copy.
* @param name Name for the shared memory area that may back the file descriptor.
* This is purely informative and may be {@code null}.
* @return A ParcelFileDescriptor.
* @throws IOException if there is an error while creating the shared memory area.
*/
@Deprecated
public static ParcelFileDescriptor fromData(byte[] data, String name) throws IOException {
if (data == null)
return null;
MemoryFile file = new MemoryFile(name, data.length);
if (data.length > 0) {
file.writeBytes(data, 0, 0, data.length);
}
file.deactivate();
FileDescriptor fd = file.getFileDescriptor();
return fd != null ? new ParcelFileDescriptor(fd) : null;
}
use of java.io.FileDescriptor in project XobotOS by xamarin.
the class ZygoteConnection method runOnce.
/**
* Reads one start command from the command socket. If successful,
* a child is forked and a {@link ZygoteInit.MethodAndArgsCaller}
* exception is thrown in that child while in the parent process,
* the method returns normally. On failure, the child is not
* spawned and messages are printed to the log and stderr. Returns
* a boolean status value indicating whether an end-of-file on the command
* socket has been encountered.
*
* @return false if command socket should continue to be read from, or
* true if an end-of-file has been encountered.
* @throws ZygoteInit.MethodAndArgsCaller trampoline to invoke main()
* method in child process
*/
boolean runOnce() throws ZygoteInit.MethodAndArgsCaller {
String[] args;
Arguments parsedArgs = null;
FileDescriptor[] descriptors;
try {
args = readArgumentList();
descriptors = mSocket.getAncillaryFileDescriptors();
} catch (IOException ex) {
Log.w(TAG, "IOException on command socket " + ex.getMessage());
closeSocket();
return true;
}
if (args == null) {
// EOF reached.
closeSocket();
return true;
}
/** the stderr of the most recent request, if avail */
PrintStream newStderr = null;
if (descriptors != null && descriptors.length >= 3) {
newStderr = new PrintStream(new FileOutputStream(descriptors[2]));
}
int pid = -1;
FileDescriptor childPipeFd = null;
FileDescriptor serverPipeFd = null;
try {
parsedArgs = new Arguments(args);
applyUidSecurityPolicy(parsedArgs, peer);
applyRlimitSecurityPolicy(parsedArgs, peer);
applyCapabilitiesSecurityPolicy(parsedArgs, peer);
applyInvokeWithSecurityPolicy(parsedArgs, peer);
applyDebuggerSystemProperty(parsedArgs);
applyInvokeWithSystemProperty(parsedArgs);
int[][] rlimits = null;
if (parsedArgs.rlimits != null) {
rlimits = parsedArgs.rlimits.toArray(intArray2d);
}
if (parsedArgs.runtimeInit && parsedArgs.invokeWith != null) {
FileDescriptor[] pipeFds = Libcore.os.pipe();
childPipeFd = pipeFds[1];
serverPipeFd = pipeFds[0];
ZygoteInit.setCloseOnExec(serverPipeFd, true);
}
pid = Zygote.forkAndSpecialize(parsedArgs.uid, parsedArgs.gid, parsedArgs.gids, parsedArgs.debugFlags, rlimits);
} catch (IOException ex) {
logAndPrintError(newStderr, "Exception creating pipe", ex);
} catch (ErrnoException ex) {
logAndPrintError(newStderr, "Exception creating pipe", ex);
} catch (IllegalArgumentException ex) {
logAndPrintError(newStderr, "Invalid zygote arguments", ex);
} catch (ZygoteSecurityException ex) {
logAndPrintError(newStderr, "Zygote security policy prevents request: ", ex);
}
try {
if (pid == 0) {
// in child
IoUtils.closeQuietly(serverPipeFd);
serverPipeFd = null;
handleChildProc(parsedArgs, descriptors, childPipeFd, newStderr);
// throw ZygoteInit.MethodAndArgsCaller or exec().
return true;
} else {
// in parent...pid of < 0 means failure
IoUtils.closeQuietly(childPipeFd);
childPipeFd = null;
return handleParentProc(pid, descriptors, serverPipeFd, parsedArgs);
}
} finally {
IoUtils.closeQuietly(childPipeFd);
IoUtils.closeQuietly(serverPipeFd);
}
}
use of java.io.FileDescriptor in project XobotOS by xamarin.
the class InetAddress method isReachable.
private boolean isReachable(InetAddress destination, InetAddress source, int timeout) throws IOException {
// TODO: try ICMP first (http://code.google.com/p/android/issues/detail?id=20106)
FileDescriptor fd = IoBridge.socket(true);
boolean reached = false;
try {
if (source != null) {
IoBridge.bind(fd, source, 0);
}
IoBridge.connect(fd, destination, 7, timeout);
reached = true;
} catch (IOException e) {
if (e.getCause() instanceof ErrnoException) {
// "Connection refused" means the IP address was reachable.
reached = (((ErrnoException) e.getCause()).errno == ECONNREFUSED);
}
}
IoBridge.closeSocket(fd);
return reached;
}
Aggregations