Search in sources :

Example 81 with ErrnoException

use of android.system.ErrnoException in project android_frameworks_base by crdroidandroid.

the class ParcelFileDescriptor method createReliableSocketPair.

/**
     * @hide
     */
public static ParcelFileDescriptor[] createReliableSocketPair(int type) throws IOException {
    try {
        final FileDescriptor[] comm = createCommSocketPair();
        final FileDescriptor fd0 = new FileDescriptor();
        final FileDescriptor fd1 = new FileDescriptor();
        Os.socketpair(AF_UNIX, type, 0, fd0, fd1);
        return new ParcelFileDescriptor[] { new ParcelFileDescriptor(fd0, comm[0]), new ParcelFileDescriptor(fd1, comm[1]) };
    } catch (ErrnoException e) {
        throw e.rethrowAsIOException();
    }
}
Also used : ErrnoException(android.system.ErrnoException) FileDescriptor(java.io.FileDescriptor)

Example 82 with ErrnoException

use of android.system.ErrnoException in project android_frameworks_base by crdroidandroid.

the class ParcelFileDescriptor method fromFd.

/**
     * Create a new ParcelFileDescriptor from a raw native fd.  The new
     * ParcelFileDescriptor holds a dup of the original fd passed in here,
     * so you must still close that fd as well as the new ParcelFileDescriptor.
     *
     * @param fd The native fd that the ParcelFileDescriptor should dup.
     *
     * @return Returns a new ParcelFileDescriptor holding a FileDescriptor
     * for a dup of the given fd.
     */
public static ParcelFileDescriptor fromFd(int fd) throws IOException {
    final FileDescriptor original = new FileDescriptor();
    original.setInt$(fd);
    try {
        final FileDescriptor dup = Os.dup(original);
        return new ParcelFileDescriptor(dup);
    } catch (ErrnoException e) {
        throw e.rethrowAsIOException();
    }
}
Also used : ErrnoException(android.system.ErrnoException) FileDescriptor(java.io.FileDescriptor)

Example 83 with ErrnoException

use of android.system.ErrnoException in project android_frameworks_base by crdroidandroid.

the class ExifInterface method saveAttributes.

/**
     * Save the tag data into the original image file. This is expensive because it involves
     * copying all the data from one file to another and deleting the old file and renaming the
     * other. It's best to use {@link #setAttribute(String,String)} to set all attributes to write
     * and make a single call rather than multiple calls for each attribute.
     * <p>
     * This method is only supported for JPEG files.
     * </p>
     */
public void saveAttributes() throws IOException {
    if (!mIsSupportedFile || mIsRaw) {
        throw new IOException("ExifInterface only supports saving attributes on JPEG formats.");
    }
    if (mIsInputStream || (mSeekableFileDescriptor == null && mFilename == null)) {
        throw new IOException("ExifInterface does not support saving attributes for the current input.");
    }
    // Keep the thumbnail in memory
    mThumbnailBytes = getThumbnail();
    FileInputStream in = null;
    FileOutputStream out = null;
    File tempFile = null;
    try {
        // Move the original file to temporary file.
        if (mFilename != null) {
            tempFile = new File(mFilename + ".tmp");
            File originalFile = new File(mFilename);
            if (!originalFile.renameTo(tempFile)) {
                throw new IOException("Could'nt rename to " + tempFile.getAbsolutePath());
            }
        } else if (mSeekableFileDescriptor != null) {
            tempFile = File.createTempFile("temp", "jpg");
            Os.lseek(mSeekableFileDescriptor, 0, OsConstants.SEEK_SET);
            in = new FileInputStream(mSeekableFileDescriptor);
            out = new FileOutputStream(tempFile);
            Streams.copy(in, out);
        }
    } catch (ErrnoException e) {
        throw e.rethrowAsIOException();
    } finally {
        IoUtils.closeQuietly(in);
        IoUtils.closeQuietly(out);
    }
    in = null;
    out = null;
    try {
        // Save the new file.
        in = new FileInputStream(tempFile);
        if (mFilename != null) {
            out = new FileOutputStream(mFilename);
        } else if (mSeekableFileDescriptor != null) {
            Os.lseek(mSeekableFileDescriptor, 0, OsConstants.SEEK_SET);
            out = new FileOutputStream(mSeekableFileDescriptor);
        }
        saveJpegAttributes(in, out);
    } catch (ErrnoException e) {
        throw e.rethrowAsIOException();
    } finally {
        IoUtils.closeQuietly(in);
        IoUtils.closeQuietly(out);
        tempFile.delete();
    }
    // Discard the thumbnail in memory
    mThumbnailBytes = null;
}
Also used : ErrnoException(android.system.ErrnoException) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 84 with ErrnoException

use of android.system.ErrnoException in project android_frameworks_base by crdroidandroid.

the class SharedPreferencesImpl method writeToFile.

// Note: must hold mWritingToDiskLock
private void writeToFile(MemoryCommitResult mcr, boolean isFromSyncCommit) {
    // Rename the current file so it may be used as a backup during the next read
    if (mFile.exists()) {
        boolean needsWrite = false;
        // Only need to write if the disk state is older than this commit
        if (mDiskStateGeneration < mcr.memoryStateGeneration) {
            if (isFromSyncCommit) {
                needsWrite = true;
            } else {
                synchronized (this) {
                    // be persisted.
                    if (mCurrentMemoryStateGeneration == mcr.memoryStateGeneration) {
                        needsWrite = true;
                    }
                }
            }
        }
        if (!needsWrite) {
            if (DEBUG) {
                Log.d(TAG, "skipped " + mcr.memoryStateGeneration + " -> " + mFile.getName());
            }
            mcr.setDiskWriteResult(true);
            return;
        }
        if (!mBackupFile.exists()) {
            if (!mFile.renameTo(mBackupFile)) {
                Log.e(TAG, "Couldn't rename file " + mFile + " to backup file " + mBackupFile);
                mcr.setDiskWriteResult(false);
                return;
            }
        } else {
            mFile.delete();
        }
    }
    // from the backup.
    try {
        FileOutputStream str = createFileOutputStream(mFile);
        if (str == null) {
            mcr.setDiskWriteResult(false);
            return;
        }
        XmlUtils.writeMapXml(mcr.mapToWriteToDisk, str);
        FileUtils.sync(str);
        if (DEBUG) {
            Log.d(TAG, "wrote " + mcr.memoryStateGeneration + " -> " + mFile.getName());
        }
        str.close();
        ContextImpl.setFilePermissionsFromMode(mFile.getPath(), mMode, 0);
        try {
            final StructStat stat = Os.stat(mFile.getPath());
            synchronized (this) {
                mStatTimestamp = stat.st_mtime;
                mStatSize = stat.st_size;
            }
        } catch (ErrnoException e) {
        // Do nothing
        }
        // Writing was successful, delete the backup file if there is one.
        mBackupFile.delete();
        mDiskStateGeneration = mcr.memoryStateGeneration;
        mcr.setDiskWriteResult(true);
        return;
    } catch (XmlPullParserException e) {
        Log.w(TAG, "writeToFile: Got exception:", e);
    } catch (IOException e) {
        Log.w(TAG, "writeToFile: Got exception:", e);
    }
    // Clean up an unsuccessfully written file
    if (mFile.exists()) {
        if (!mFile.delete()) {
            Log.e(TAG, "Couldn't clean up partially-written file " + mFile);
        }
    }
    mcr.setDiskWriteResult(false);
}
Also used : StructStat(android.system.StructStat) ErrnoException(android.system.ErrnoException) FileOutputStream(java.io.FileOutputStream) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException)

Example 85 with ErrnoException

use of android.system.ErrnoException in project android_frameworks_base by crdroidandroid.

the class SharedPreferencesImpl method hasFileChangedUnexpectedly.

// Has the file changed out from under us?  i.e. writes that
// we didn't instigate.
private boolean hasFileChangedUnexpectedly() {
    synchronized (this) {
        if (mDiskWritesInFlight > 0) {
            // If we know we caused it, it's not unexpected.
            if (DEBUG)
                Log.d(TAG, "disk write in flight, not unexpected.");
            return false;
        }
    }
    final StructStat stat;
    try {
        /*
             * Metadata operations don't usually count as a block guard
             * violation, but we explicitly want this one.
             */
        BlockGuard.getThreadPolicy().onReadFromDisk();
        stat = Os.stat(mFile.getPath());
    } catch (ErrnoException e) {
        return true;
    }
    synchronized (this) {
        return mStatTimestamp != stat.st_mtime || mStatSize != stat.st_size;
    }
}
Also used : StructStat(android.system.StructStat) ErrnoException(android.system.ErrnoException)

Aggregations

ErrnoException (android.system.ErrnoException)215 IOException (java.io.IOException)99 FileDescriptor (java.io.FileDescriptor)95 File (java.io.File)74 StructStat (android.system.StructStat)45 FileInputStream (java.io.FileInputStream)40 FileOutputStream (java.io.FileOutputStream)29 ParcelFileDescriptor (android.os.ParcelFileDescriptor)17 SocketException (java.net.SocketException)17 BufferedInputStream (java.io.BufferedInputStream)15 InputStream (java.io.InputStream)11 AssetFileDescriptor (android.content.res.AssetFileDescriptor)10 ExifInterface (android.media.ExifInterface)10 StructLinger (android.system.StructLinger)10 StructTimeval (android.system.StructTimeval)10 FileNotFoundException (java.io.FileNotFoundException)10 StructPollfd (android.system.StructPollfd)9 InterruptedIOException (java.io.InterruptedIOException)9 PacketSocketAddress (android.system.PacketSocketAddress)8 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)8