Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fd082907 authored by Jin Seok Park's avatar Jin Seok Park Committed by Android (Google) Code Review
Browse files

Merge "Remove using File(FileDescriptor, boolean)"

parents 8d96ab39 06e7d766
Loading
Loading
Loading
Loading
+15 −8
Original line number Original line Diff line number Diff line
@@ -17,6 +17,7 @@
package android.media;
package android.media;


import static android.media.ExifInterfaceUtils.byteArrayToHexString;
import static android.media.ExifInterfaceUtils.byteArrayToHexString;
import static android.media.ExifInterfaceUtils.closeFileDescriptor;
import static android.media.ExifInterfaceUtils.closeQuietly;
import static android.media.ExifInterfaceUtils.closeQuietly;
import static android.media.ExifInterfaceUtils.convertToLongArray;
import static android.media.ExifInterfaceUtils.convertToLongArray;
import static android.media.ExifInterfaceUtils.copy;
import static android.media.ExifInterfaceUtils.copy;
@@ -1529,9 +1530,8 @@ public class ExifInterface {


        mAssetInputStream = null;
        mAssetInputStream = null;
        mFilename = null;
        mFilename = null;
        // When FileDescriptor is duplicated and set to FileInputStream, ownership needs to be

        // clarified in order for garbage collection to take place.
        boolean isFdDuped = false;
        boolean isFdOwner = false;
        if (isSeekableFD(fileDescriptor)) {
        if (isSeekableFD(fileDescriptor)) {
            mSeekableFileDescriptor = fileDescriptor;
            mSeekableFileDescriptor = fileDescriptor;
            // Keep the original file descriptor in order to save attributes when it's seekable.
            // Keep the original file descriptor in order to save attributes when it's seekable.
@@ -1539,7 +1539,7 @@ public class ExifInterface {
            // feature won't be working.
            // feature won't be working.
            try {
            try {
                fileDescriptor = Os.dup(fileDescriptor);
                fileDescriptor = Os.dup(fileDescriptor);
                isFdOwner = true;
                isFdDuped = true;
            } catch (ErrnoException e) {
            } catch (ErrnoException e) {
                throw e.rethrowAsIOException();
                throw e.rethrowAsIOException();
            }
            }
@@ -1549,10 +1549,13 @@ public class ExifInterface {
        mIsInputStream = false;
        mIsInputStream = false;
        FileInputStream in = null;
        FileInputStream in = null;
        try {
        try {
            in = new FileInputStream(fileDescriptor, isFdOwner);
            in = new FileInputStream(fileDescriptor);
            loadAttributes(in);
            loadAttributes(in);
        } finally {
        } finally {
            closeQuietly(in);
            closeQuietly(in);
            if (isFdDuped) {
                closeFileDescriptor(fileDescriptor);
            }
        }
        }
    }
    }


@@ -2199,6 +2202,7 @@ public class ExifInterface {


        // Read the thumbnail.
        // Read the thumbnail.
        InputStream in = null;
        InputStream in = null;
        FileDescriptor newFileDescriptor = null;
        try {
        try {
            if (mAssetInputStream != null) {
            if (mAssetInputStream != null) {
                in = mAssetInputStream;
                in = mAssetInputStream;
@@ -2211,9 +2215,9 @@ public class ExifInterface {
            } else if (mFilename != null) {
            } else if (mFilename != null) {
                in = new FileInputStream(mFilename);
                in = new FileInputStream(mFilename);
            } else if (mSeekableFileDescriptor != null) {
            } else if (mSeekableFileDescriptor != null) {
                FileDescriptor fileDescriptor = Os.dup(mSeekableFileDescriptor);
                newFileDescriptor = Os.dup(mSeekableFileDescriptor);
                Os.lseek(fileDescriptor, 0, OsConstants.SEEK_SET);
                Os.lseek(newFileDescriptor, 0, OsConstants.SEEK_SET);
                in = new FileInputStream(fileDescriptor, true);
                in = new FileInputStream(newFileDescriptor);
            }
            }
            if (in == null) {
            if (in == null) {
                // Should not be reached this.
                // Should not be reached this.
@@ -2234,6 +2238,9 @@ public class ExifInterface {
            Log.d(TAG, "Encountered exception while getting thumbnail", e);
            Log.d(TAG, "Encountered exception while getting thumbnail", e);
        } finally {
        } finally {
            closeQuietly(in);
            closeQuietly(in);
            if (newFileDescriptor != null) {
                closeFileDescriptor(newFileDescriptor);
            }
        }
        }
        return null;
        return null;
    }
    }
+18 −0
Original line number Original line Diff line number Diff line
@@ -16,7 +16,12 @@


package android.media;
package android.media;


import android.system.ErrnoException;
import android.system.Os;
import android.util.Log;

import java.io.Closeable;
import java.io.Closeable;
import java.io.FileDescriptor;
import java.io.IOException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStream;
@@ -25,6 +30,8 @@ import java.io.OutputStream;
 * Package private utility class for ExifInterface.
 * Package private utility class for ExifInterface.
 */
 */
class ExifInterfaceUtils {
class ExifInterfaceUtils {
    private static final String TAG = "ExifInterface";

    /**
    /**
     * Copies all of the bytes from {@code in} to {@code out}. Neither stream is closed.
     * Copies all of the bytes from {@code in} to {@code out}. Neither stream is closed.
     * Returns the total number of bytes transferred.
     * Returns the total number of bytes transferred.
@@ -114,4 +121,15 @@ class ExifInterfaceUtils {
            }
            }
        }
        }
    }
    }

    /**
     * Closes a file descriptor that has been duplicated.
     */
    public static void closeFileDescriptor(FileDescriptor fd) {
        try {
            Os.close(fd);
        } catch (ErrnoException ex) {
            Log.e(TAG, "Error closing fd.", ex);
        }
    }
}
}