use of org.apache.http.util.ByteArrayBuffer in project android_frameworks_base by DirtyUnicorns.
the class BandwidthTestUtil method DownloadFromUrl.
/**
* Download a given file from a target url to a given destination file.
* @param targetUrl the url to download
* @param file the {@link File} location where to save to
* @return true if it succeeded
*/
public static boolean DownloadFromUrl(String targetUrl, File file) {
try {
URL url = new URL(targetUrl);
Log.d(LOG_TAG, "Download begining");
Log.d(LOG_TAG, "Download url:" + url);
Log.d(LOG_TAG, "Downloaded file name:" + file.getAbsolutePath());
URLConnection ucon = url.openConnection();
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.close();
} catch (IOException e) {
Log.d(LOG_TAG, "Failed to download file with error: " + e);
return false;
}
return true;
}
use of org.apache.http.util.ByteArrayBuffer in project crawler4j by yasserg.
the class Page method toByteArray.
/**
* Read contents from an entity, with a specified maximum. This is a replacement of
* EntityUtils.toByteArray because that function does not impose a maximum size.
*
* @param entity The entity from which to read
* @param maxBytes The maximum number of bytes to read
* @return A byte array containing maxBytes or fewer bytes read from the entity
*
* @throws IOException Thrown when reading fails for any reason
*/
protected byte[] toByteArray(HttpEntity entity, int maxBytes) throws IOException {
if (entity == null) {
return new byte[0];
}
try (InputStream is = entity.getContent()) {
int size = (int) entity.getContentLength();
int readBufferLength = size;
if (readBufferLength <= 0) {
readBufferLength = 4096;
}
// in case when the maxBytes is less than the actual page size
readBufferLength = Math.min(readBufferLength, maxBytes);
// We allocate the buffer with either the actual size of the entity (if available)
// or with the default 4KiB if the server did not return a value to avoid allocating
// the full maxBytes (for the cases when the actual size will be smaller than maxBytes).
ByteArrayBuffer buffer = new ByteArrayBuffer(readBufferLength);
byte[] tmpBuff = new byte[4096];
int dataLength;
while ((dataLength = is.read(tmpBuff)) != -1) {
if (maxBytes > 0 && (buffer.length() + dataLength) > maxBytes) {
truncated = true;
dataLength = maxBytes - buffer.length();
}
buffer.append(tmpBuff, 0, dataLength);
if (truncated) {
break;
}
}
return buffer.toByteArray();
}
}
use of org.apache.http.util.ByteArrayBuffer in project android_frameworks_base by ParanoidAndroid.
the class BandwidthTestUtil method DownloadFromUrl.
/**
* Download a given file from a target url to a given destination file.
* @param targetUrl the url to download
* @param file the {@link File} location where to save to
* @return true if it succeeded
*/
public static boolean DownloadFromUrl(String targetUrl, File file) {
try {
URL url = new URL(targetUrl);
Log.d(LOG_TAG, "Download begining");
Log.d(LOG_TAG, "Download url:" + url);
Log.d(LOG_TAG, "Downloaded file name:" + file.getAbsolutePath());
URLConnection ucon = url.openConnection();
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.close();
} catch (IOException e) {
Log.d(LOG_TAG, "Failed to download file with error: " + e);
return false;
}
return true;
}
use of org.apache.http.util.ByteArrayBuffer in project platform_external_apache-http by android.
the class AbstractSessionInputBuffer method init.
protected void init(final InputStream instream, int buffersize, final HttpParams params) {
if (instream == null) {
throw new IllegalArgumentException("Input stream may not be null");
}
if (buffersize <= 0) {
throw new IllegalArgumentException("Buffer size may not be negative or zero");
}
if (params == null) {
throw new IllegalArgumentException("HTTP parameters may not be null");
}
this.instream = instream;
this.buffer = new byte[buffersize];
this.bufferpos = 0;
this.bufferlen = 0;
this.linebuffer = new ByteArrayBuffer(buffersize);
this.charset = HttpProtocolParams.getHttpElementCharset(params);
this.ascii = this.charset.equalsIgnoreCase(HTTP.US_ASCII) || this.charset.equalsIgnoreCase(HTTP.ASCII);
this.maxLineLen = params.getIntParameter(CoreConnectionPNames.MAX_LINE_LENGTH, -1);
this.metrics = new HttpTransportMetricsImpl();
}
use of org.apache.http.util.ByteArrayBuffer in project platform_external_apache-http by android.
the class AbstractSessionOutputBuffer method init.
protected void init(final OutputStream outstream, int buffersize, final HttpParams params) {
if (outstream == null) {
throw new IllegalArgumentException("Input stream may not be null");
}
if (buffersize <= 0) {
throw new IllegalArgumentException("Buffer size may not be negative or zero");
}
if (params == null) {
throw new IllegalArgumentException("HTTP parameters may not be null");
}
this.outstream = outstream;
this.buffer = new ByteArrayBuffer(buffersize);
this.charset = HttpProtocolParams.getHttpElementCharset(params);
this.ascii = this.charset.equalsIgnoreCase(HTTP.US_ASCII) || this.charset.equalsIgnoreCase(HTTP.ASCII);
this.metrics = new HttpTransportMetricsImpl();
}
Aggregations