|
| 1 | +// ------------------------------------------------------------------------------ |
| 2 | +// Copyright (c) 2017 Microsoft Corporation |
| 3 | +// |
| 4 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +// of this software and associated documentation files (the "Software"), to deal |
| 6 | +// in the Software without restriction, including without limitation the rights |
| 7 | +// to use, copy, modify, merge, publish, distribute, sub-license, and/or sell |
| 8 | +// copies of the Software, and to permit persons to whom the Software is |
| 9 | +// furnished to do so, subject to the following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included in |
| 12 | +// all copies or substantial portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | +// THE SOFTWARE. |
| 21 | +// ------------------------------------------------------------------------------ |
| 22 | + |
| 23 | +package com.microsoft.graph.concurrency; |
| 24 | + |
| 25 | + |
| 26 | +import com.microsoft.graph.concurrency.ChunkedUploadResponseHandler; |
| 27 | +import com.microsoft.graph.concurrency.IProgressCallback; |
| 28 | +import com.microsoft.graph.requests.extensions.ChunkedUploadRequest; |
| 29 | +import com.microsoft.graph.requests.extensions.ChunkedUploadResult; |
| 30 | +import com.microsoft.graph.models.extensions.IGraphServiceClient; |
| 31 | +import com.microsoft.graph.models.extensions.UploadSession; |
| 32 | +import com.microsoft.graph.options.Option; |
| 33 | + |
| 34 | +import java.io.IOException; |
| 35 | +import java.io.InputStream; |
| 36 | +import java.security.InvalidParameterException; |
| 37 | +import java.util.List; |
| 38 | + |
| 39 | +/** |
| 40 | + * ChunkedUpload service provider. |
| 41 | + * |
| 42 | + * @param <UploadType> The upload item type. |
| 43 | + */ |
| 44 | +public class ChunkedUploadProvider<UploadType> { |
| 45 | + |
| 46 | + /** |
| 47 | + * The default chunk size for upload. Currently set to 5 MiB. |
| 48 | + */ |
| 49 | + private static final int DEFAULT_CHUNK_SIZE = 5 * 1024 * 1024; |
| 50 | + |
| 51 | + /** |
| 52 | + * The required chunk size increment by OneDrive service, which is 320 KiB. |
| 53 | + */ |
| 54 | + private static final int REQUIRED_CHUNK_SIZE_INCREMENT = 320 * 1024; |
| 55 | + |
| 56 | + /** |
| 57 | + * The maximum chunk size for a single upload allowed by OneDrive service. |
| 58 | + * Currently the value is 60 MiB. |
| 59 | + */ |
| 60 | + private static final int MAXIMUM_CHUNK_SIZE = 60 * 1024 * 1024; |
| 61 | + |
| 62 | + /** |
| 63 | + * The default retry times for a simple chunk upload if failure happened. |
| 64 | + */ |
| 65 | + private static final int MAXIMUM_RETRY_TIMES = 3; |
| 66 | + |
| 67 | + /** |
| 68 | + * The client. |
| 69 | + */ |
| 70 | + private final IGraphServiceClient client; |
| 71 | + |
| 72 | + /** |
| 73 | + * The input stream. |
| 74 | + */ |
| 75 | + private final InputStream inputStream; |
| 76 | + |
| 77 | + /** |
| 78 | + * The upload session URL. |
| 79 | + */ |
| 80 | + private final String uploadUrl; |
| 81 | + |
| 82 | + /** |
| 83 | + * The stream size. |
| 84 | + */ |
| 85 | + private final int streamSize; |
| 86 | + |
| 87 | + /** |
| 88 | + * The upload response handler. |
| 89 | + */ |
| 90 | + private final ChunkedUploadResponseHandler<UploadType> responseHandler; |
| 91 | + |
| 92 | + /** |
| 93 | + * The counter for how many bytes have been read from input stream. |
| 94 | + */ |
| 95 | + private int readSoFar; |
| 96 | + |
| 97 | + /** |
| 98 | + * Create the ChunkedUploadProvider |
| 99 | + * |
| 100 | + * @param uploadSession The initial upload session. |
| 101 | + * @param client The OneDrive client. |
| 102 | + * @param inputStream The input stream. |
| 103 | + * @param streamSize The stream size. |
| 104 | + * @param uploadTypeClass The upload type class. |
| 105 | + */ |
| 106 | + public ChunkedUploadProvider(final UploadSession uploadSession, |
| 107 | + final IGraphServiceClient client, |
| 108 | + final InputStream inputStream, |
| 109 | + final int streamSize, |
| 110 | + final Class<UploadType> uploadTypeClass) { |
| 111 | + if (uploadSession == null) { |
| 112 | + throw new InvalidParameterException("Upload session is null."); |
| 113 | + } |
| 114 | + |
| 115 | + if (client == null) { |
| 116 | + throw new InvalidParameterException("OneDrive client is null."); |
| 117 | + } |
| 118 | + |
| 119 | + if (inputStream == null) { |
| 120 | + throw new InvalidParameterException("Input stream is null."); |
| 121 | + } |
| 122 | + |
| 123 | + if (streamSize <= 0) { |
| 124 | + throw new InvalidParameterException("Stream size should larger than 0."); |
| 125 | + } |
| 126 | + |
| 127 | + this.client = client; |
| 128 | + this.readSoFar = 0; |
| 129 | + this.inputStream = inputStream; |
| 130 | + this.streamSize = streamSize; |
| 131 | + this.uploadUrl = uploadSession.uploadUrl; |
| 132 | + this.responseHandler = new ChunkedUploadResponseHandler<UploadType>(uploadTypeClass); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Upload content to remote upload session based on the input stream. |
| 137 | + * |
| 138 | + * @param options The upload options. |
| 139 | + * @param callback The progress callback invoked during uploading. |
| 140 | + * @param configs The optional configurations for the upload options, [0] should be the customized chunk |
| 141 | + * size and the [1] should be the maxRetry for upload retry. |
| 142 | + * @throws IOException The IO exception happened during upload. |
| 143 | + */ |
| 144 | + public void upload(final List<Option> options, |
| 145 | + final IProgressCallback<UploadType> callback, |
| 146 | + final int... configs) |
| 147 | + throws IOException { |
| 148 | + int chunkSize = DEFAULT_CHUNK_SIZE; |
| 149 | + |
| 150 | + if (configs.length > 0) { |
| 151 | + chunkSize = configs[0]; |
| 152 | + } |
| 153 | + |
| 154 | + int maxRetry = MAXIMUM_RETRY_TIMES; |
| 155 | + |
| 156 | + if (configs.length > 1) { |
| 157 | + maxRetry = configs[1]; |
| 158 | + } |
| 159 | + |
| 160 | + if (chunkSize % REQUIRED_CHUNK_SIZE_INCREMENT != 0) { |
| 161 | + throw new IllegalArgumentException("Chunk size must be a multiple of 320 KiB"); |
| 162 | + } |
| 163 | + |
| 164 | + if (chunkSize > MAXIMUM_CHUNK_SIZE) { |
| 165 | + throw new IllegalArgumentException("Please set chunk size smaller than 60 MiB"); |
| 166 | + } |
| 167 | + |
| 168 | + byte[] buffer = new byte[chunkSize]; |
| 169 | + |
| 170 | + while (this.readSoFar < this.streamSize) { |
| 171 | + int read = this.inputStream.read(buffer); |
| 172 | + |
| 173 | + if (read == -1) { |
| 174 | + break; |
| 175 | + } |
| 176 | + |
| 177 | + ChunkedUploadRequest request = |
| 178 | + new ChunkedUploadRequest(this.uploadUrl, this.client, options, buffer, read, |
| 179 | + maxRetry, this.readSoFar, this.streamSize); |
| 180 | + ChunkedUploadResult result = request.upload(this.responseHandler); |
| 181 | + |
| 182 | + if (result.uploadCompleted()) { |
| 183 | + callback.progress(this.streamSize, this.streamSize); |
| 184 | + callback.success((UploadType) result.getItem()); |
| 185 | + break; |
| 186 | + } else if (result.chunkCompleted()) { |
| 187 | + callback.progress(this.readSoFar, this.streamSize); |
| 188 | + } else if (result.hasError()) { |
| 189 | + callback.failure(result.getError()); |
| 190 | + break; |
| 191 | + } |
| 192 | + |
| 193 | + this.readSoFar += read; |
| 194 | + } |
| 195 | + } |
| 196 | +} |
0 commit comments