23
23
package com .tencent .qcloud .core .http ;
24
24
25
25
26
+ import android .content .ContentResolver ;
27
+ import android .net .Uri ;
28
+ import android .text .TextUtils ;
29
+
26
30
import com .tencent .qcloud .core .common .QCloudClientException ;
27
31
import com .tencent .qcloud .core .common .QCloudProgressListener ;
28
32
import com .tencent .qcloud .core .common .QCloudServiceException ;
29
33
import com .tencent .qcloud .core .util .QCloudHttpUtils ;
30
34
31
35
import java .io .*;
32
36
37
+ import okhttp3 .Response ;
33
38
import okhttp3 .ResponseBody ;
34
39
import okhttp3 .internal .Util ;
35
40
import okio .Buffer ;
38
43
public class ResponseFileConverter <T > extends ResponseBodyConverter <T > implements ProgressBody {
39
44
40
45
private String filePath ;
46
+ private Uri contentUri ;
47
+ private ContentResolver contentResolver ;
48
+
41
49
private long offset ;
42
50
43
51
protected boolean isQuic = false ;
@@ -51,6 +59,12 @@ public ResponseFileConverter(String filePath, long offset) {
51
59
this .offset = offset ;
52
60
}
53
61
62
+ public ResponseFileConverter (Uri contentUri , ContentResolver contentResolver , long offset ) {
63
+ this .contentUri = contentUri ;
64
+ this .contentResolver = contentResolver ;
65
+ this .offset = offset ;
66
+ }
67
+
54
68
@ Override
55
69
public void setProgressListener (QCloudProgressListener progressListener ) {
56
70
this .progressListener = progressListener ;
@@ -80,6 +94,39 @@ public T convert(HttpResponse<T> response) throws QCloudClientException, QCloudS
80
94
contentLength = response .contentLength ();
81
95
}
82
96
97
+ if (!TextUtils .isEmpty (filePath )) {
98
+ return downloadToAbsolutePath (response , contentLength );
99
+ } else if (contentUri != null ) {
100
+ return pipeToContentUri (response , contentLength );
101
+ }
102
+
103
+ throw new QCloudClientException (new IllegalArgumentException ("filePath or ContentUri are both null" ));
104
+ }
105
+
106
+ private T pipeToContentUri (HttpResponse <T > response , long contentLength )
107
+ throws QCloudClientException , QCloudServiceException {
108
+ OutputStream output = getOutputStream ();
109
+ InputStream input = response .byteStream ();
110
+
111
+ byte [] buffer = new byte [8192 ];
112
+ countingSink = new CountingSink (new Buffer (), contentLength , progressListener );
113
+ int len ;
114
+ try {
115
+ while ((len = input .read (buffer )) != -1 ) {
116
+ output .write (buffer , 0 , len );
117
+ countingSink .writeBytesInternal (len );
118
+ }
119
+ return null ;
120
+ } catch (IOException e ) {
121
+ e .printStackTrace ();
122
+ throw new QCloudClientException ("write local uri error for " + e .toString (), e );
123
+ } finally {
124
+ Util .closeQuietly (output );
125
+ }
126
+ }
127
+
128
+ private T downloadToAbsolutePath (HttpResponse <T > response , long contentLength )
129
+ throws QCloudClientException , QCloudServiceException {
83
130
File downloadFilePath = new File (filePath );
84
131
File parentDir = downloadFilePath .getParentFile ();
85
132
if (parentDir != null && !parentDir .exists () && !parentDir .mkdirs ()){
@@ -121,16 +168,25 @@ private void writeRandomAccessFile(File downloadFilePath, InputStream inputStrea
121
168
}
122
169
123
170
public OutputStream getOutputStream () throws QCloudClientException {
124
- File downloadFilePath = new File (filePath );
125
- File parentDir = downloadFilePath .getParentFile ();
126
- if (parentDir != null && !parentDir .exists () && !parentDir .mkdirs ()){
127
- throw new QCloudClientException (new IOException ("local file directory can not create." ));
128
- }
129
- try {
130
- OutputStream outputStream = new FileOutputStream (downloadFilePath );
131
- return outputStream ;
132
- } catch (FileNotFoundException e ) {
133
- throw new QCloudClientException (e );
171
+ if (!TextUtils .isEmpty (filePath )) {
172
+ File downloadFilePath = new File (filePath );
173
+ File parentDir = downloadFilePath .getParentFile ();
174
+ if (parentDir != null && !parentDir .exists () && !parentDir .mkdirs ()){
175
+ throw new QCloudClientException (new IOException ("local file directory can not create." ));
176
+ }
177
+ try {
178
+ return new FileOutputStream (downloadFilePath );
179
+ } catch (FileNotFoundException e ) {
180
+ throw new QCloudClientException (e );
181
+ }
182
+ } else if (contentUri != null ){
183
+ try {
184
+ return contentResolver .openOutputStream (contentUri );
185
+ } catch (FileNotFoundException e ) {
186
+ throw new QCloudClientException (e );
187
+ }
188
+ } else {
189
+ throw new QCloudClientException (new IllegalArgumentException ("filePath or ContentUri are both null" ));
134
190
}
135
191
}
136
192
0 commit comments