Last active
July 13, 2016 01:49
-
-
Save SwordBearer/d9d814828e2327a97f4ac220aaf1a72d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.swordbearer.network.volley; | |
import android.text.TextUtils; | |
import com.android.volley.NetworkResponse; | |
import com.android.volley.ParseError; | |
import com.android.volley.Request; | |
import com.android.volley.Response; | |
import com.android.volley.VolleyError; | |
import com.android.volley.toolbox.HttpHeaderParser; | |
import java.io.BufferedOutputStream; | |
import java.io.ByteArrayInputStream; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.InputStream; | |
import java.security.InvalidParameterException; | |
/** | |
* Project SwordBearer | |
* Package com.swordbearer.network.volley | |
* Created by swordbearer on 7/12/16. | |
*/ | |
public class FileRequest extends Request<File> { | |
private final String filePath; | |
private final FileRequestListener mListener; | |
public FileRequest(int method, String url, String filePath, FileRequestListener pRequestListener) { | |
super(method, url, pRequestListener.buildErrorListener()); | |
this.filePath = filePath; | |
if (TextUtils.isEmpty(filePath)) { | |
throw new InvalidParameterException("please set file path"); | |
} | |
this.mListener = pRequestListener; | |
} | |
@Override | |
protected Response<File> parseNetworkResponse(NetworkResponse response) { | |
try { | |
File file = saveFile(response.data); | |
return Response.success(file, HttpHeaderParser.parseCacheHeaders(response)); | |
} catch (Exception ex) { | |
return Response.error(new ParseError(ex)); | |
} | |
} | |
@Override | |
protected void deliverResponse(File pFile) { | |
if (pFile != null && pFile.exists() && pFile.length() > 0) { | |
if (mListener != null) { | |
mListener.onSuccess(pFile); | |
} | |
} else { | |
if (mListener != null) { | |
mListener.onFailed(); | |
} | |
} | |
} | |
private File saveFile(byte[] data) { | |
try { | |
File file = new File(filePath); | |
InputStream input = new ByteArrayInputStream(data); | |
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file)); | |
byte[] buffer = new byte[1024]; | |
long total = 0; | |
int count; | |
while ((count = input.read(buffer)) != -1) { | |
total += count; | |
bos.write(buffer, 0, count); | |
if (mListener != null) { | |
mListener.onDownload(file, total); | |
} | |
} | |
bos.flush(); | |
bos.close(); | |
input.close(); | |
return file; | |
} catch (Exception ex) { | |
ex.printStackTrace(); | |
} | |
return null; | |
} | |
public static class FileRequestListener implements Response.Listener<File> { | |
@Override | |
public final void onResponse(File pFile) { | |
} | |
public void onSuccess(File file) { | |
} | |
public void onDownload(File file, long downloadSize) { | |
} | |
public void onFailed() { | |
} | |
public Response.ErrorListener buildErrorListener() { | |
return new Response.ErrorListener() { | |
@Override | |
public void onErrorResponse(VolleyError pVolleyError) { | |
onFailed(); | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment