package external.GifImageViewEx.net.frakbot.remote;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import com.aiyou.utils.logcat.Logcat;
import com.squareup.okhttp.OkHttpClient;
/**
* Helper class that exposes some utility methods for retrieving objects from
* the network.
*
* @author Francesco Pontillo
*/
public class RemoteHelper {
private static final String LOG_TAG = "Loader";
private static final int defaultBufferSize = 2048;
/**
* Download an object from the network.
*
* @param resourceUrl The URL of then rsource.
* @throws IOException If the connection cannot be established.
* @return Byte array of the downloaded object.
*/
public static byte[] download(String resourceUrl) throws IOException {
OkHttpClient client = new OkHttpClient();
URL url = new URL(resourceUrl);
HttpURLConnection connection = client.open(url);
final int responseCode = connection.getResponseCode();
if (responseCode != HttpURLConnection.HTTP_OK) {
Logcat.w(LOG_TAG, "Downloading from URL " + resourceUrl
+ " failed with response code " + responseCode);
return null;
}
// determine the image size and allocate a buffer
int fileSize = connection.getContentLength();
Logcat.d(LOG_TAG, "fetching " + resourceUrl + " ("
+ (fileSize <= 0 ? "size unknown" : Integer.toString(fileSize))
+ ")");
BufferedInputStream istream = new BufferedInputStream(
connection.getInputStream());
try {
if (fileSize <= 0) {
Logcat.w(LOG_TAG,
"Server did not set a Content-Length header, will default to buffer size of "
+ defaultBufferSize + " bytes");
ByteArrayOutputStream buf = new ByteArrayOutputStream(
defaultBufferSize);
byte[] buffer = new byte[defaultBufferSize];
int bytesRead = 0;
while (bytesRead != -1) {
bytesRead = istream.read(buffer, 0, defaultBufferSize);
if (bytesRead > 0) {
buf.write(buffer, 0, bytesRead);
}
}
return buf.toByteArray();
} else {
byte[] data = new byte[fileSize];
int bytesRead = 0;
int offset = 0;
while (bytesRead != -1 && offset < fileSize) {
bytesRead = istream.read(data, offset, fileSize - offset);
offset += bytesRead;
}
return data;
}
} finally {
// clean up
try {
istream.close();
connection.disconnect();
} catch (Exception ignore) {
}
}
}
}