Skip to content

Commit 7ed0053

Browse files
authored
Merge pull request #105 from microsoftgraph/feature/moving-nongen
feature/moving nongen
2 parents 2da8811 + abc2de4 commit 7ed0053

102 files changed

Lines changed: 11283 additions & 32 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎gradle/dependencies.gradle‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
dependencies {
22
// Use JUnit test framework
33
testImplementation 'junit:junit:4.13.1'
4+
testImplementation 'org.mockito:mockito-inline:3.6.0'
45

56
api 'com.squareup.okhttp3:okhttp:4.9.0'
67

7-
api 'com.google.code.findbugs:jsr305:3.0.2' // can be replaced by guava if we need guava
8-
8+
implementation 'com.google.guava:guava:29.0-jre'
9+
910
implementation 'com.google.code.gson:gson:2.8.6'
1011
}
Lines changed: 302 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,302 @@
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+
import com.microsoft.graph.concurrency.ChunkedUploadResponseHandler;
26+
import com.microsoft.graph.concurrency.ChunkedUploadRequest;
27+
import com.microsoft.graph.concurrency.ChunkedUploadResult;
28+
import com.microsoft.graph.concurrency.IUploadSession;
29+
import com.microsoft.graph.core.ClientException;
30+
import com.microsoft.graph.core.IBaseClient;
31+
import com.microsoft.graph.options.Option;
32+
33+
import java.io.IOException;
34+
import java.io.InputStream;
35+
import java.security.InvalidParameterException;
36+
import java.util.List;
37+
import java.util.concurrent.CompletableFuture;
38+
import java.util.concurrent.ExecutionException;
39+
40+
import javax.annotation.Nullable;
41+
import javax.annotation.Nonnull;
42+
43+
/**
44+
* ChunkedUpload service provider
45+
*
46+
* @param <UploadType> the upload item type
47+
*/
48+
public class ChunkedUploadProvider<UploadType> {
49+
50+
/**
51+
* The default chunk size for upload. Currently set to 5 MiB.
52+
*/
53+
private static final int DEFAULT_CHUNK_SIZE = 5 * 1024 * 1024;
54+
55+
/**
56+
* The required chunk size increment by OneDrive service, which is 320 KiB
57+
*/
58+
private static final int REQUIRED_CHUNK_SIZE_INCREMENT = 320 * 1024;
59+
60+
/**
61+
* The maximum chunk size for a single upload allowed by OneDrive service.
62+
* Currently the value is 60 MiB.
63+
*/
64+
private static final int MAXIMUM_CHUNK_SIZE = 60 * 1024 * 1024;
65+
66+
/**
67+
* The client
68+
*/
69+
private final IBaseClient client;
70+
71+
/**
72+
* The input stream
73+
*/
74+
private final InputStream inputStream;
75+
76+
/**
77+
* The upload session URL
78+
*/
79+
private final String uploadUrl;
80+
81+
/**
82+
* The stream size
83+
*/
84+
private final long streamSize;
85+
86+
/**
87+
* The upload response handler
88+
*/
89+
private final ChunkedUploadResponseHandler<UploadType> responseHandler;
90+
91+
/**
92+
* The counter for how many bytes have been read from input stream
93+
*/
94+
private long readSoFar;
95+
96+
/**
97+
* Creates the ChunkedUploadProvider
98+
*
99+
* @param uploadSession the initial upload session
100+
* @param client the Graph client
101+
* @param inputStream the input stream
102+
* @param streamSize the stream size
103+
* @param uploadTypeClass the upload type class
104+
*/
105+
public ChunkedUploadProvider(@Nonnull final IUploadSession uploadSession,
106+
@Nonnull final IBaseClient client,
107+
@Nonnull final InputStream inputStream,
108+
final long streamSize,
109+
@Nonnull final Class<UploadType> uploadTypeClass) {
110+
if (uploadSession == null) {
111+
throw new InvalidParameterException("Upload session is null.");
112+
}
113+
114+
if (client == null) {
115+
throw new InvalidParameterException("OneDrive client is null.");
116+
}
117+
118+
if (inputStream == null) {
119+
throw new InvalidParameterException("Input stream is null.");
120+
}
121+
122+
if (streamSize <= 0) {
123+
throw new InvalidParameterException("Stream size should larger than 0.");
124+
}
125+
126+
this.client = client;
127+
this.readSoFar = 0;
128+
this.inputStream = inputStream;
129+
this.streamSize = streamSize;
130+
this.uploadUrl = uploadSession.getUploadUrl();
131+
this.responseHandler = new ChunkedUploadResponseHandler<UploadType>(uploadTypeClass, uploadSession.getClass());
132+
}
133+
134+
/**
135+
* Uploads content to remote upload session based on the input stream
136+
*
137+
* @param options the upload options
138+
* @param chunkSize the customized chunk size
139+
* @param progressCallback the callback for upload progress
140+
* @return a future with the result
141+
* @throws IOException the IO exception that occurred during upload
142+
*/
143+
@Nonnull
144+
public java.util.concurrent.CompletableFuture<UploadType> uploadAsync(@Nullable final int chunkSize, @Nullable final List<Option> options, @Nullable final IProgressCallback progressCallback)
145+
throws IOException {
146+
147+
int internalChunkSize = chunkSize;
148+
if (internalChunkSize == 0) {
149+
internalChunkSize = DEFAULT_CHUNK_SIZE;
150+
}
151+
152+
if (internalChunkSize % REQUIRED_CHUNK_SIZE_INCREMENT != 0) {
153+
throw new IllegalArgumentException("Chunk size must be a multiple of 320 KiB");
154+
}
155+
156+
if (internalChunkSize > MAXIMUM_CHUNK_SIZE) {
157+
throw new IllegalArgumentException("Please set chunk size smaller than 60 MiB");
158+
}
159+
160+
byte[] buffer = new byte[internalChunkSize];
161+
162+
while (this.readSoFar < this.streamSize) {
163+
int buffRead = 0;
164+
165+
// inner loop is to work-around the case where read buffer size is limited to less than chunk size by a global setting
166+
while (buffRead < internalChunkSize) {
167+
int read = 0;
168+
read = this.inputStream.read(buffer, buffRead, internalChunkSize - buffRead);
169+
if (read == -1) {
170+
break;
171+
}
172+
buffRead += read;
173+
}
174+
175+
final ChunkedUploadRequest<UploadType> request =
176+
new ChunkedUploadRequest<UploadType>(this.uploadUrl, this.client, options, buffer, buffRead,
177+
this.readSoFar, this.streamSize);
178+
final ChunkedUploadResult<UploadType> result = request.upload(this.responseHandler);
179+
// TODO: upload should return a future, use sendfuture instead and the futures should be chained with completableFuture.then apply
180+
181+
if (result.uploadCompleted()) {
182+
if(progressCallback != null) {
183+
progressCallback.progress(this.streamSize, this.streamSize);
184+
}
185+
return completedFuture(result.getItem());
186+
} else if (result.chunkCompleted()) {
187+
if(progressCallback != null) {
188+
progressCallback.progress(this.readSoFar, this.streamSize);
189+
}
190+
} else if (result.hasError()) {
191+
return failedFuture(result.getError());
192+
}
193+
194+
this.readSoFar += buffRead;
195+
}
196+
return failedFuture(new ClientException("Upload did not complete", null));
197+
}
198+
private CompletableFuture<UploadType> completedFuture(final UploadType result) { // CompletableFuture.completedFuture(result.getItem()); missing on android
199+
final CompletableFuture<UploadType> fut = new CompletableFuture<UploadType>();
200+
fut.complete(result);
201+
return fut;
202+
}
203+
private CompletableFuture<UploadType> failedFuture(ClientException ex) { // CompletableFuture.failedFuture not available on android
204+
final CompletableFuture<UploadType> fut = new CompletableFuture<UploadType>();
205+
fut.completeExceptionally(ex);
206+
return fut;
207+
}
208+
209+
/**
210+
* Uploads content to remote upload session based on the input stream
211+
*
212+
* @return a future with the result
213+
* @throws IOException the IO exception that occurred during upload
214+
*/
215+
@Nonnull
216+
public java.util.concurrent.CompletableFuture<UploadType> uploadAsync()
217+
throws IOException {
218+
return uploadAsync(0);
219+
}
220+
/**
221+
* Uploads content to remote upload session based on the input stream
222+
*
223+
* @param chunkSize the customized chunk size
224+
* @return a future with the result
225+
* @throws IOException the IO exception that occurred during upload
226+
*/
227+
@Nonnull
228+
public java.util.concurrent.CompletableFuture<UploadType> uploadAsync(@Nullable final int chunkSize)
229+
throws IOException {
230+
return uploadAsync(chunkSize, null);
231+
}
232+
/**
233+
* Uploads content to remote upload session based on the input stream
234+
*
235+
* @param chunkSize the customized chunk size
236+
* @param options the upload options
237+
* @return a future with the result
238+
* @throws IOException the IO exception that occurred during upload
239+
*/
240+
@Nonnull
241+
public java.util.concurrent.CompletableFuture<UploadType> uploadAsync(@Nullable final int chunkSize, @Nullable final List<Option> options)
242+
throws IOException {
243+
return uploadAsync(chunkSize, options, null);
244+
}
245+
246+
/**
247+
* Uploads content to remote upload session based on the input stream
248+
*
249+
* @param options the upload options
250+
* @param chunkSize the customized chunk size
251+
* @param progressCallback the callback for upload progress
252+
* @return the result
253+
* @throws IOException the IO exception that occurred during upload
254+
*/
255+
@Nonnull
256+
public UploadType upload(@Nullable final int chunkSize, @Nullable final List<Option> options, @Nullable final IProgressCallback progressCallback)
257+
throws IOException {
258+
try {
259+
return uploadAsync(chunkSize, options, progressCallback).get();
260+
} catch (InterruptedException ex) {
261+
throw new ClientException("The request was interrupted", ex);
262+
} catch (ExecutionException ex) {
263+
throw new ClientException("Error while executing the request", ex);
264+
}
265+
}
266+
/**
267+
* Uploads content to remote upload session based on the input stream
268+
*
269+
* @param options the upload options
270+
* @param chunkSize the customized chunk size
271+
* @return the result
272+
* @throws IOException the IO exception that occurred during upload
273+
*/
274+
@Nonnull
275+
public UploadType upload(@Nullable final int chunkSize, @Nullable final List<Option> options)
276+
throws IOException {
277+
return upload(chunkSize, options, null);
278+
}
279+
/**
280+
* Uploads content to remote upload session based on the input stream
281+
*
282+
* @param chunkSize the customized chunk size
283+
* @return the result
284+
* @throws IOException the IO exception that occurred during upload
285+
*/
286+
@Nonnull
287+
public UploadType upload(@Nullable final int chunkSize)
288+
throws IOException {
289+
return upload(chunkSize, null);
290+
}
291+
/**
292+
* Uploads content to remote upload session based on the input stream
293+
*
294+
* @return the result
295+
* @throws IOException the IO exception that occurred during upload
296+
*/
297+
@Nonnull
298+
public UploadType upload()
299+
throws IOException {
300+
return upload(0);
301+
}
302+
}

0 commit comments

Comments
 (0)