Merge "Revert "Revert "Revert "Have AssetFileDescriptor.AutoCloseInputStream us...""" into tm-dev
This commit is contained in:
@@ -21,8 +21,6 @@ import android.os.Bundle;
|
|||||||
import android.os.Parcel;
|
import android.os.Parcel;
|
||||||
import android.os.ParcelFileDescriptor;
|
import android.os.ParcelFileDescriptor;
|
||||||
import android.os.Parcelable;
|
import android.os.Parcelable;
|
||||||
import android.system.ErrnoException;
|
|
||||||
import android.system.Os;
|
|
||||||
|
|
||||||
import java.io.Closeable;
|
import java.io.Closeable;
|
||||||
import java.io.FileDescriptor;
|
import java.io.FileDescriptor;
|
||||||
@@ -54,11 +52,11 @@ public class AssetFileDescriptor implements Parcelable, Closeable {
|
|||||||
/**
|
/**
|
||||||
* Create a new AssetFileDescriptor from the given values.
|
* Create a new AssetFileDescriptor from the given values.
|
||||||
*
|
*
|
||||||
* @param fd The underlying file descriptor.
|
* @param fd The underlying file descriptor.
|
||||||
* @param startOffset The location within the file that the asset starts.
|
* @param startOffset The location within the file that the asset starts.
|
||||||
* This must be 0 if length is UNKNOWN_LENGTH.
|
* This must be 0 if length is UNKNOWN_LENGTH.
|
||||||
* @param length The number of bytes of the asset, or
|
* @param length The number of bytes of the asset, or
|
||||||
* {@link #UNKNOWN_LENGTH} if it extends to the end of the file.
|
* {@link #UNKNOWN_LENGTH} if it extends to the end of the file.
|
||||||
*/
|
*/
|
||||||
public AssetFileDescriptor(ParcelFileDescriptor fd, long startOffset,
|
public AssetFileDescriptor(ParcelFileDescriptor fd, long startOffset,
|
||||||
long length) {
|
long length) {
|
||||||
@@ -68,13 +66,13 @@ public class AssetFileDescriptor implements Parcelable, Closeable {
|
|||||||
/**
|
/**
|
||||||
* Create a new AssetFileDescriptor from the given values.
|
* Create a new AssetFileDescriptor from the given values.
|
||||||
*
|
*
|
||||||
* @param fd The underlying file descriptor.
|
* @param fd The underlying file descriptor.
|
||||||
* @param startOffset The location within the file that the asset starts.
|
* @param startOffset The location within the file that the asset starts.
|
||||||
* This must be 0 if length is UNKNOWN_LENGTH.
|
* This must be 0 if length is UNKNOWN_LENGTH.
|
||||||
* @param length The number of bytes of the asset, or
|
* @param length The number of bytes of the asset, or
|
||||||
* {@link #UNKNOWN_LENGTH} if it extends to the end of the file.
|
* {@link #UNKNOWN_LENGTH} if it extends to the end of the file.
|
||||||
* @param extras additional details that can be used to interpret the
|
* @param extras additional details that can be used to interpret the
|
||||||
* underlying file descriptor. May be null.
|
* underlying file descriptor. May be null.
|
||||||
*/
|
*/
|
||||||
public AssetFileDescriptor(ParcelFileDescriptor fd, long startOffset,
|
public AssetFileDescriptor(ParcelFileDescriptor fd, long startOffset,
|
||||||
long length, Bundle extras) {
|
long length, Bundle extras) {
|
||||||
@@ -205,24 +203,19 @@ public class AssetFileDescriptor implements Parcelable, Closeable {
|
|||||||
*/
|
*/
|
||||||
public static class AutoCloseInputStream
|
public static class AutoCloseInputStream
|
||||||
extends ParcelFileDescriptor.AutoCloseInputStream {
|
extends ParcelFileDescriptor.AutoCloseInputStream {
|
||||||
private final long mSizeFromStartOffset;
|
private long mRemaining;
|
||||||
private final long mStartOffset;
|
|
||||||
private long mPosFromStartOffset;
|
|
||||||
|
|
||||||
public AutoCloseInputStream(AssetFileDescriptor fd) throws IOException {
|
public AutoCloseInputStream(AssetFileDescriptor fd) throws IOException {
|
||||||
super(fd.getParcelFileDescriptor());
|
super(fd.getParcelFileDescriptor());
|
||||||
// this skip is necessary if getChannel() is called
|
|
||||||
super.skip(fd.getStartOffset());
|
super.skip(fd.getStartOffset());
|
||||||
mSizeFromStartOffset = fd.getLength();
|
mRemaining = (int) fd.getLength();
|
||||||
mStartOffset = fd.getStartOffset();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int available() throws IOException {
|
public int available() throws IOException {
|
||||||
long available = mSizeFromStartOffset - mPosFromStartOffset;
|
return mRemaining >= 0
|
||||||
return available >= 0
|
? (mRemaining < 0x7fffffff ? (int) mRemaining : 0x7fffffff)
|
||||||
? (available < 0x7fffffff ? (int) available : 0x7fffffff)
|
: super.available();
|
||||||
: 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -234,24 +227,15 @@ public class AssetFileDescriptor implements Parcelable, Closeable {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int read(byte[] buffer, int offset, int count) throws IOException {
|
public int read(byte[] buffer, int offset, int count) throws IOException {
|
||||||
int available = available();
|
if (mRemaining >= 0) {
|
||||||
|
if (mRemaining == 0) return -1;
|
||||||
if (available <= 0) {
|
if (count > mRemaining) count = (int) mRemaining;
|
||||||
return -1;
|
int res = super.read(buffer, offset, count);
|
||||||
} else {
|
if (res >= 0) mRemaining -= res;
|
||||||
if (count > available) count = available;
|
return res;
|
||||||
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
|
@Override
|
||||||
@@ -261,31 +245,41 @@ public class AssetFileDescriptor implements Parcelable, Closeable {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long skip(long count) throws IOException {
|
public long skip(long count) throws IOException {
|
||||||
int available = available();
|
if (mRemaining >= 0) {
|
||||||
if (available <= 0) {
|
if (mRemaining == 0) return -1;
|
||||||
return -1;
|
if (count > mRemaining) count = mRemaining;
|
||||||
} else {
|
long res = super.skip(count);
|
||||||
if (count > available) count = available;
|
if (res >= 0) mRemaining -= res;
|
||||||
mPosFromStartOffset += count;
|
return res;
|
||||||
return count;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return super.skip(count);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void mark(int readlimit) {
|
public void mark(int readlimit) {
|
||||||
// Not supported.
|
if (mRemaining >= 0) {
|
||||||
return;
|
// Not supported.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
super.mark(readlimit);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean markSupported() {
|
public boolean markSupported() {
|
||||||
return false;
|
if (mRemaining >= 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return super.markSupported();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void reset() throws IOException {
|
public synchronized void reset() throws IOException {
|
||||||
// Not supported.
|
if (mRemaining >= 0) {
|
||||||
return;
|
// Not supported.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
super.reset();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -381,7 +375,6 @@ public class AssetFileDescriptor implements Parcelable, Closeable {
|
|||||||
public AssetFileDescriptor createFromParcel(Parcel in) {
|
public AssetFileDescriptor createFromParcel(Parcel in) {
|
||||||
return new AssetFileDescriptor(in);
|
return new AssetFileDescriptor(in);
|
||||||
}
|
}
|
||||||
|
|
||||||
public AssetFileDescriptor[] newArray(int size) {
|
public AssetFileDescriptor[] newArray(int size) {
|
||||||
return new AssetFileDescriptor[size];
|
return new AssetFileDescriptor[size];
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user