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

Commit 2ef3a7eb authored by Jeremy Meyer's avatar Jeremy Meyer Committed by Android (Google) Code Review
Browse files

Merge "Revert "Revert "Have AssetFileDescriptor.AutoCloseInputStream us...""

parents 73505f68 f26bb9d6
Loading
Loading
Loading
Loading
+70 −63
Original line number Diff line number Diff line
@@ -21,6 +21,8 @@ import android.os.Bundle;
import android.os.Parcel;
import android.os.ParcelFileDescriptor;
import android.os.Parcelable;
import android.system.ErrnoException;
import android.system.Os;

import java.io.Closeable;
import java.io.FileDescriptor;
@@ -203,19 +205,24 @@ public class AssetFileDescriptor implements Parcelable, Closeable {
     */
    public static class AutoCloseInputStream
            extends ParcelFileDescriptor.AutoCloseInputStream {
        private long mRemaining;
        private final long mSizeFromStartOffset;
        private final long mStartOffset;
        private long mPosFromStartOffset;

        public AutoCloseInputStream(AssetFileDescriptor fd) throws IOException {
            super(fd.getParcelFileDescriptor());
            // this skip is necessary if getChannel() is called
            super.skip(fd.getStartOffset());
            mRemaining = (int)fd.getLength();
            mSizeFromStartOffset = fd.getLength();
            mStartOffset = fd.getStartOffset();
        }

        @Override
        public int available() throws IOException {
            return mRemaining >= 0
                    ? (mRemaining < 0x7fffffff ? (int)mRemaining : 0x7fffffff)
                    : super.available();
            long available = mSizeFromStartOffset - mPosFromStartOffset;
            return available >= 0
                    ? (available < 0x7fffffff ? (int) available : 0x7fffffff)
                    : 0;
        }

        @Override
@@ -227,15 +234,24 @@ public class AssetFileDescriptor implements Parcelable, Closeable {

        @Override
        public int read(byte[] buffer, int offset, int count) throws IOException {
            if (mRemaining >= 0) {
                if (mRemaining == 0) return -1;
                if (count > mRemaining) count = (int)mRemaining;
                int res = super.read(buffer, offset, count);
                if (res >= 0) mRemaining -= res;
            int available = available();

            if (available <= 0) {
                return -1;
            } else {
                if (count > available) count = available;
                try {
                    int res = Os.pread(getFD(), buffer, offset, count,
                            mStartOffset + mPosFromStartOffset);
                    // pread returns 0 at end of file, while java's InputStream interface
                    // requires -1
                    if (res == 0) res = -1;
                    if (res >= 0) mPosFromStartOffset += res;
                    return res;
                } catch (ErrnoException e) {
                    throw new IOException(e);
                }
            }
            
            return super.read(buffer, offset, count);
        }

        @Override
@@ -245,42 +261,32 @@ public class AssetFileDescriptor implements Parcelable, Closeable {

        @Override
        public long skip(long count) throws IOException {
            if (mRemaining >= 0) {
                if (mRemaining == 0) return -1;
                if (count > mRemaining) count = mRemaining;
                long res = super.skip(count);
                if (res >= 0) mRemaining -= res;
                return res;
            int available = available();
            if (available <= 0) {
                return -1;
            } else {
                if (count > available) count = available;
                mPosFromStartOffset += count;
                return count;
            }
            
            return super.skip(count);
        }

        @Override
        public void mark(int readlimit) {
            if (mRemaining >= 0) {
            // Not supported.
            return;
        }
            super.mark(readlimit);
        }

        @Override
        public boolean markSupported() {
            if (mRemaining >= 0) {
            return false;
        }
            return super.markSupported();
        }

        @Override
        public synchronized void reset() throws IOException {
            if (mRemaining >= 0) {
            // Not supported.
            return;
        }
            super.reset();
        }
    }

    /**
@@ -375,6 +381,7 @@ public class AssetFileDescriptor implements Parcelable, Closeable {
        public AssetFileDescriptor createFromParcel(Parcel in) {
            return new AssetFileDescriptor(in);
        }

        public AssetFileDescriptor[] newArray(int size) {
            return new AssetFileDescriptor[size];
        }