| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.content.res; |
| 18 |
|
| 19 |
import android.compat.annotation.UnsupportedAppUsage; |
| 20 |
import android.os.Bundle; |
| 21 |
import android.os.Parcel; |
| 22 |
import android.os.ParcelFileDescriptor; |
| 23 |
import android.os.Parcelable; |
| 24 |
|
| 25 |
import java.io.Closeable; |
| 26 |
import java.io.FileDescriptor; |
| 27 |
import java.io.FileInputStream; |
| 28 |
import java.io.FileOutputStream; |
| 29 |
import java.io.IOException; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public class AssetFileDescriptor implements Parcelable, Closeable { |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public static final long UNKNOWN_LENGTH = -1; |
| 43 |
|
| 44 |
@UnsupportedAppUsage |
| 45 |
private final ParcelFileDescriptor mFd; |
| 46 |
@UnsupportedAppUsage |
| 47 |
private final long mStartOffset; |
| 48 |
@UnsupportedAppUsage |
| 49 |
private final long mLength; |
| 50 |
private final Bundle mExtras; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public AssetFileDescriptor(ParcelFileDescriptor fd, long startOffset, |
| 62 |
long length) { |
| 63 |
this(fd, startOffset, length, null); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public AssetFileDescriptor(ParcelFileDescriptor fd, long startOffset, |
| 78 |
long length, Bundle extras) { |
| 79 |
if (fd == null) { |
| 80 |
throw new IllegalArgumentException("fd must not be null"); |
| 81 |
} |
| 82 |
if (length < 0 && startOffset != 0) { |
| 83 |
throw new IllegalArgumentException( |
| 84 |
"startOffset must be 0 when using UNKNOWN_LENGTH"); |
| 85 |
} |
| 86 |
mFd = fd; |
| 87 |
mStartOffset = startOffset; |
| 88 |
mLength = length; |
| 89 |
mExtras = extras; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public ParcelFileDescriptor getParcelFileDescriptor() { |
| 98 |
return mFd; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public FileDescriptor getFileDescriptor() { |
| 106 |
return mFd.getFileDescriptor(); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public long getStartOffset() { |
| 113 |
return mStartOffset; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public Bundle getExtras() { |
| 121 |
return mExtras; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public long getLength() { |
| 136 |
if (mLength >= 0) { |
| 137 |
return mLength; |
| 138 |
} |
| 139 |
long len = mFd.getStatSize(); |
| 140 |
return len >= 0 ? len : UNKNOWN_LENGTH; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
public long getDeclaredLength() { |
| 152 |
return mLength; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
@Override |
| 159 |
public void close() throws IOException { |
| 160 |
mFd.close(); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
public FileInputStream createInputStream() throws IOException { |
| 172 |
if (mLength < 0) { |
| 173 |
return new ParcelFileDescriptor.AutoCloseInputStream(mFd); |
| 174 |
} |
| 175 |
return new AutoCloseInputStream(this); |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
public FileOutputStream createOutputStream() throws IOException { |
| 187 |
if (mLength < 0) { |
| 188 |
return new ParcelFileDescriptor.AutoCloseOutputStream(mFd); |
| 189 |
} |
| 190 |
return new AutoCloseOutputStream(this); |
| 191 |
} |
| 192 |
|
| 193 |
@Override |
| 194 |
public String toString() { |
| 195 |
return "{AssetFileDescriptor: " + mFd |
| 196 |
+ " start=" + mStartOffset + " len=" + mLength + "}"; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
public static class AutoCloseInputStream |
| 205 |
extends ParcelFileDescriptor.AutoCloseInputStream { |
| 206 |
private long mRemaining; |
| 207 |
|
| 208 |
public AutoCloseInputStream(AssetFileDescriptor fd) throws IOException { |
| 209 |
super(fd.getParcelFileDescriptor()); |
| 210 |
super.skip(fd.getStartOffset()); |
| 211 |
mRemaining = (int) fd.getLength(); |
| 212 |
} |
| 213 |
|
| 214 |
@Override |
| 215 |
public int available() throws IOException { |
| 216 |
return mRemaining >= 0 |
| 217 |
? (mRemaining < 0x7fffffff ? (int) mRemaining : 0x7fffffff) |
| 218 |
: super.available(); |
| 219 |
} |
| 220 |
|
| 221 |
@Override |
| 222 |
public int read() throws IOException { |
| 223 |
byte[] buffer = new byte[1]; |
| 224 |
int result = read(buffer, 0, 1); |
| 225 |
return result == -1 ? -1 : buffer[0] & 0xff; |
| 226 |
} |
| 227 |
|
| 228 |
@Override |
| 229 |
public int read(byte[] buffer, int offset, int count) throws IOException { |
| 230 |
if (mRemaining >= 0) { |
| 231 |
if (mRemaining == 0) return -1; |
| 232 |
if (count > mRemaining) count = (int) mRemaining; |
| 233 |
int res = super.read(buffer, offset, count); |
| 234 |
if (res >= 0) mRemaining -= res; |
| 235 |
return res; |
| 236 |
} |
| 237 |
|
| 238 |
return super.read(buffer, offset, count); |
| 239 |
} |
| 240 |
|
| 241 |
@Override |
| 242 |
public int read(byte[] buffer) throws IOException { |
| 243 |
return read(buffer, 0, buffer.length); |
| 244 |
} |
| 245 |
|
| 246 |
@Override |
| 247 |
public long skip(long count) throws IOException { |
| 248 |
if (mRemaining >= 0) { |
| 249 |
if (mRemaining == 0) return -1; |
| 250 |
if (count > mRemaining) count = mRemaining; |
| 251 |
long res = super.skip(count); |
| 252 |
if (res >= 0) mRemaining -= res; |
| 253 |
return res; |
| 254 |
} |
| 255 |
|
| 256 |
return super.skip(count); |
| 257 |
} |
| 258 |
|
| 259 |
@Override |
| 260 |
public void mark(int readlimit) { |
| 261 |
if (mRemaining >= 0) { |
| 262 |
|
| 263 |
return; |
| 264 |
} |
| 265 |
super.mark(readlimit); |
| 266 |
} |
| 267 |
|
| 268 |
@Override |
| 269 |
public boolean markSupported() { |
| 270 |
if (mRemaining >= 0) { |
| 271 |
return false; |
| 272 |
} |
| 273 |
return super.markSupported(); |
| 274 |
} |
| 275 |
|
| 276 |
@Override |
| 277 |
public synchronized void reset() throws IOException { |
| 278 |
if (mRemaining >= 0) { |
| 279 |
|
| 280 |
return; |
| 281 |
} |
| 282 |
super.reset(); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
public static class AutoCloseOutputStream |
| 292 |
extends ParcelFileDescriptor.AutoCloseOutputStream { |
| 293 |
private long mRemaining; |
| 294 |
|
| 295 |
public AutoCloseOutputStream(AssetFileDescriptor fd) throws IOException { |
| 296 |
super(fd.getParcelFileDescriptor()); |
| 297 |
if (fd.getParcelFileDescriptor().seekTo(fd.getStartOffset()) < 0) { |
| 298 |
throw new IOException("Unable to seek"); |
| 299 |
} |
| 300 |
mRemaining = (int) fd.getLength(); |
| 301 |
} |
| 302 |
|
| 303 |
@Override |
| 304 |
public void write(byte[] buffer, int offset, int count) throws IOException { |
| 305 |
if (mRemaining >= 0) { |
| 306 |
if (mRemaining == 0) return; |
| 307 |
if (count > mRemaining) count = (int) mRemaining; |
| 308 |
super.write(buffer, offset, count); |
| 309 |
mRemaining -= count; |
| 310 |
return; |
| 311 |
} |
| 312 |
|
| 313 |
super.write(buffer, offset, count); |
| 314 |
} |
| 315 |
|
| 316 |
@Override |
| 317 |
public void write(byte[] buffer) throws IOException { |
| 318 |
if (mRemaining >= 0) { |
| 319 |
if (mRemaining == 0) return; |
| 320 |
int count = buffer.length; |
| 321 |
if (count > mRemaining) count = (int) mRemaining; |
| 322 |
super.write(buffer); |
| 323 |
mRemaining -= count; |
| 324 |
return; |
| 325 |
} |
| 326 |
|
| 327 |
super.write(buffer); |
| 328 |
} |
| 329 |
|
| 330 |
@Override |
| 331 |
public void write(int oneByte) throws IOException { |
| 332 |
if (mRemaining >= 0) { |
| 333 |
if (mRemaining == 0) return; |
| 334 |
super.write(oneByte); |
| 335 |
mRemaining--; |
| 336 |
return; |
| 337 |
} |
| 338 |
|
| 339 |
super.write(oneByte); |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
@Override |
| 345 |
public int describeContents() { |
| 346 |
return mFd.describeContents(); |
| 347 |
} |
| 348 |
|
| 349 |
@Override |
| 350 |
public void writeToParcel(Parcel out, int flags) { |
| 351 |
mFd.writeToParcel(out, flags); |
| 352 |
out.writeLong(mStartOffset); |
| 353 |
out.writeLong(mLength); |
| 354 |
if (mExtras != null) { |
| 355 |
out.writeInt(1); |
| 356 |
out.writeBundle(mExtras); |
| 357 |
} else { |
| 358 |
out.writeInt(0); |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
AssetFileDescriptor(Parcel src) { |
| 363 |
mFd = ParcelFileDescriptor.CREATOR.createFromParcel(src); |
| 364 |
mStartOffset = src.readLong(); |
| 365 |
mLength = src.readLong(); |
| 366 |
if (src.readInt() != 0) { |
| 367 |
mExtras = src.readBundle(); |
| 368 |
} else { |
| 369 |
mExtras = null; |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
public static final @android.annotation.NonNull Parcelable.Creator<AssetFileDescriptor> CREATOR |
| 374 |
= new Parcelable.Creator<AssetFileDescriptor>() { |
| 375 |
public AssetFileDescriptor createFromParcel(Parcel in) { |
| 376 |
return new AssetFileDescriptor(in); |
| 377 |
} |
| 378 |
public AssetFileDescriptor[] newArray(int size) { |
| 379 |
return new AssetFileDescriptor[size]; |
| 380 |
} |
| 381 |
}; |
| 382 |
|
| 383 |
} |