LEFT | RIGHT |
1 /* | 1 /* |
2 Copyright 2013 Google Inc | 2 Copyright 2013 Google Inc |
3 | 3 |
4 Licensed under the Apache License, Version 2.0 (the "License"); | 4 Licensed under the Apache License, Version 2.0 (the "License"); |
5 you may not use this file except in compliance with the License. | 5 you may not use this file except in compliance with the License. |
6 You may obtain a copy of the License at | 6 You may obtain a copy of the License at |
7 | 7 |
8 http://www.apache.org/licenses/LICENSE-2.0 | 8 http://www.apache.org/licenses/LICENSE-2.0 |
9 | 9 |
10 Unless required by applicable law or agreed to in writing, software | 10 Unless required by applicable law or agreed to in writing, software |
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 var currentRequestLastBytePos = currentRequestFirstBytePos
ChunkSize - 1; | 226 var currentRequestLastBytePos = currentRequestFirstBytePos
ChunkSize - 1; |
227 | 227 |
228 // create the request and set the Range header | 228 // create the request and set the Range header |
229 var request = builder.CreateRequest(); | 229 var request = builder.CreateRequest(); |
230 request.Headers.Range = new RangeHeaderValue(currentRequestF
irstBytePos, | 230 request.Headers.Range = new RangeHeaderValue(currentRequestF
irstBytePos, |
231 currentRequestLastBytePos); | 231 currentRequestLastBytePos); |
232 | 232 |
233 using (var response = await service.HttpClient.SendAsync(req
uest, cancellationToken). | 233 using (var response = await service.HttpClient.SendAsync(req
uest, cancellationToken). |
234 ConfigureAwait(false)) | 234 ConfigureAwait(false)) |
235 { | 235 { |
236 response.EnsureSuccessStatusCode(); | |
237 // read the content and copy to the parameter's stream | 236 // read the content and copy to the parameter's stream |
238 var responseStream = await response.Content.ReadAsStream
Async().ConfigureAwait(false); | 237 var responseStream = await response.Content.ReadAsStream
Async().ConfigureAwait(false); |
239 responseStream.CopyTo(stream); | 238 responseStream.CopyTo(stream); |
240 | 239 |
241 // read the headers and check if all the media content w
as already downloaded | 240 // read the headers and check if all the media content w
as already downloaded |
242 var contentRange = response.Content.Headers.ContentRange
; | 241 var contentRange = response.Content.Headers.ContentRange
; |
243 long mediaContentLength; | 242 long mediaContentLength; |
244 | 243 |
245 if (contentRange == null) | 244 if (contentRange == null) |
246 { | 245 { |
247 // content range is null in case the server doesn't
adhere the media download protocol, in· | 246 // content range is null when the server doesn't adh
ere the media download protocol, in· |
248 // that case we got all the media in one chunk | 247 // that case we got all the media in one chunk |
249 currentRequestFirstBytePos = mediaContentLength = | 248 currentRequestFirstBytePos = mediaContentLength = |
250 response.Content.Headers.ContentLength.Value; | 249 response.Content.Headers.ContentLength.Value; |
251 } | 250 } |
252 else | 251 else |
253 { | 252 { |
254 currentRequestFirstBytePos = contentRange.To.Value
1; | 253 currentRequestFirstBytePos = contentRange.To.Value
1; |
255 mediaContentLength = contentRange.Length.Value; | 254 mediaContentLength = contentRange.Length.Value; |
256 } | 255 } |
257 | 256 |
(...skipping 10 matching lines...) Expand all Loading... |
268 } | 267 } |
269 catch (Exception ex) | 268 catch (Exception ex) |
270 { | 269 { |
271 var progress = new DownloadProgress(ex, currentRequestFirstByteP
os); | 270 var progress = new DownloadProgress(ex, currentRequestFirstByteP
os); |
272 UpdateProgress(progress); | 271 UpdateProgress(progress); |
273 return progress; | 272 return progress; |
274 } | 273 } |
275 } | 274 } |
276 } | 275 } |
277 } | 276 } |
LEFT | RIGHT |