2323package com .tencent .qcloud .core .http ;
2424
2525
26+ import android .content .ContentResolver ;
27+ import android .net .Uri ;
28+ import android .text .TextUtils ;
29+
2630import com .tencent .qcloud .core .common .QCloudClientException ;
2731import com .tencent .qcloud .core .common .QCloudProgressListener ;
2832import com .tencent .qcloud .core .common .QCloudServiceException ;
2933import com .tencent .qcloud .core .util .QCloudHttpUtils ;
3034
3135import java .io .*;
3236
37+ import okhttp3 .Response ;
3338import okhttp3 .ResponseBody ;
3439import okhttp3 .internal .Util ;
3540import okio .Buffer ;
3843public class ResponseFileConverter <T > extends ResponseBodyConverter <T > implements ProgressBody {
3944
4045 private String filePath ;
46+ private Uri contentUri ;
47+ private ContentResolver contentResolver ;
48+
4149 private long offset ;
4250
4351 protected boolean isQuic = false ;
@@ -51,6 +59,12 @@ public ResponseFileConverter(String filePath, long offset) {
5159 this .offset = offset ;
5260 }
5361
62+ public ResponseFileConverter (Uri contentUri , ContentResolver contentResolver , long offset ) {
63+ this .contentUri = contentUri ;
64+ this .contentResolver = contentResolver ;
65+ this .offset = offset ;
66+ }
67+
5468 @ Override
5569 public void setProgressListener (QCloudProgressListener progressListener ) {
5670 this .progressListener = progressListener ;
@@ -80,6 +94,39 @@ public T convert(HttpResponse<T> response) throws QCloudClientException, QCloudS
8094 contentLength = response .contentLength ();
8195 }
8296
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 {
83130 File downloadFilePath = new File (filePath );
84131 File parentDir = downloadFilePath .getParentFile ();
85132 if (parentDir != null && !parentDir .exists () && !parentDir .mkdirs ()){
@@ -121,16 +168,25 @@ private void writeRandomAccessFile(File downloadFilePath, InputStream inputStrea
121168 }
122169
123170 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" ));
134190 }
135191 }
136192
0 commit comments