|
| 1 | +/* |
| 2 | + * Copyright (c) 2015-present, Parse, LLC. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + */ |
| 9 | +package com.parse; |
| 10 | + |
| 11 | +import static com.parse.Parse.getApplicationContext; |
| 12 | + |
| 13 | +import android.content.res.AssetFileDescriptor; |
| 14 | +import android.database.Cursor; |
| 15 | +import android.net.Uri; |
| 16 | +import android.os.ParcelFileDescriptor; |
| 17 | +import android.provider.OpenableColumns; |
| 18 | +import com.parse.http.ParseHttpBody; |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.InputStream; |
| 21 | +import java.io.OutputStream; |
| 22 | + |
| 23 | +class ParseUriHttpBody extends ParseHttpBody { |
| 24 | + |
| 25 | + /* package */ final Uri uri; |
| 26 | + |
| 27 | + public ParseUriHttpBody(Uri uri) { |
| 28 | + this(uri, null); |
| 29 | + } |
| 30 | + |
| 31 | + public ParseUriHttpBody(Uri uri, String contentType) { |
| 32 | + super(contentType, getUriLength(uri)); |
| 33 | + this.uri = uri; |
| 34 | + } |
| 35 | + |
| 36 | + private static long getUriLength(Uri uri) { |
| 37 | + long length = -1; |
| 38 | + |
| 39 | + try (Cursor cursor = |
| 40 | + getApplicationContext() |
| 41 | + .getContentResolver() |
| 42 | + .query(uri, null, null, null, null, null)) { |
| 43 | + if (cursor != null && cursor.moveToFirst()) { |
| 44 | + int sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE); |
| 45 | + if (!cursor.isNull(sizeIndex)) { |
| 46 | + length = cursor.getLong(sizeIndex); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + if (length == -1) { |
| 51 | + try { |
| 52 | + ParcelFileDescriptor parcelFileDescriptor = |
| 53 | + getApplicationContext().getContentResolver().openFileDescriptor(uri, "r"); |
| 54 | + if (parcelFileDescriptor != null) { |
| 55 | + length = parcelFileDescriptor.getStatSize(); |
| 56 | + parcelFileDescriptor.close(); |
| 57 | + } |
| 58 | + } catch (IOException ignored) { |
| 59 | + } |
| 60 | + } |
| 61 | + if (length == -1) { |
| 62 | + try { |
| 63 | + AssetFileDescriptor assetFileDescriptor = |
| 64 | + getApplicationContext() |
| 65 | + .getContentResolver() |
| 66 | + .openAssetFileDescriptor(uri, "r"); |
| 67 | + if (assetFileDescriptor != null) { |
| 68 | + length = assetFileDescriptor.getLength(); |
| 69 | + assetFileDescriptor.close(); |
| 70 | + } |
| 71 | + } catch (IOException ignored) { |
| 72 | + } |
| 73 | + } |
| 74 | + return length; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public InputStream getContent() throws IOException { |
| 79 | + return getApplicationContext().getContentResolver().openInputStream(uri); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public void writeTo(OutputStream out) throws IOException { |
| 84 | + if (out == null) { |
| 85 | + throw new IllegalArgumentException("Output stream can not be null"); |
| 86 | + } |
| 87 | + |
| 88 | + final InputStream fileInput = |
| 89 | + getApplicationContext().getContentResolver().openInputStream(uri); |
| 90 | + try { |
| 91 | + ParseIOUtils.copy(fileInput, out); |
| 92 | + } finally { |
| 93 | + ParseIOUtils.closeQuietly(fileInput); |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments