List of usage examples for org.apache.http.util ByteArrayBuffer length
public int length()
From source file:crawler.Page.java
/** * 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 *///w w w . j a va 2 s . c o m 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(); } }
From source file:com.android.idtt.http.client.multipart.HttpMultipart.java
private void doWriteTo(final HttpMultipartMode mode, final OutputStream out, MultipartEntity.CallBackInfo callBackInfo, boolean writeContent) throws IOException { callBackInfo.pos = 0;//from ww w .java 2 s .com ByteArrayBuffer boundary = encode(this.charset, getBoundary()); for (FormBodyPart part : this.parts) { if (!callBackInfo.doCallBack(true)) { return; } writeBytes(TWO_DASHES, out); callBackInfo.pos += TWO_DASHES.length(); writeBytes(boundary, out); callBackInfo.pos += boundary.length(); writeBytes(CR_LF, out); callBackInfo.pos += CR_LF.length(); MinimalFieldHeader header = part.getHeader(); switch (mode) { case STRICT: for (MinimalField field : header) { writeField(field, out); callBackInfo.pos += encode(MIME.DEFAULT_CHARSET, field.getName() + field.getBody()).length() + FIELD_SEP.length() + CR_LF.length(); } break; case BROWSER_COMPATIBLE: // Only write Content-Disposition // Use content charset MinimalField cd = part.getHeader().getField(MIME.CONTENT_DISPOSITION); writeField(cd, this.charset, out); callBackInfo.pos += encode(this.charset, cd.getName() + cd.getBody()).length() + FIELD_SEP.length() + CR_LF.length(); String filename = part.getBody().getFilename(); if (filename != null) { MinimalField ct = part.getHeader().getField(MIME.CONTENT_TYPE); writeField(ct, this.charset, out); callBackInfo.pos += encode(this.charset, ct.getName() + ct.getBody()).length() + FIELD_SEP.length() + CR_LF.length(); } break; } writeBytes(CR_LF, out); callBackInfo.pos += CR_LF.length(); if (writeContent) { ContentBody body = part.getBody(); body.setCallBackInfo(callBackInfo); body.writeTo(out); } writeBytes(CR_LF, out); callBackInfo.pos += CR_LF.length(); } writeBytes(TWO_DASHES, out); callBackInfo.pos += TWO_DASHES.length(); writeBytes(boundary, out); callBackInfo.pos += boundary.length(); writeBytes(TWO_DASHES, out); callBackInfo.pos += TWO_DASHES.length(); writeBytes(CR_LF, out); callBackInfo.pos += CR_LF.length(); callBackInfo.doCallBack(true); }
From source file:cn.isif.util_plus.http.client.multipart.HttpMultipart.java
private void doWriteTo(final HttpMultipartMode mode, final OutputStream out, MultipartEntity.CallBackInfo callBackInfo, boolean writeContent) throws IOException { callBackInfo.pos = 0;// w ww . ja va2s .c o m ByteArrayBuffer boundary = encode(this.charset, getBoundary()); for (FormBodyPart part : this.parts) { if (!callBackInfo.doCallBack(true)) { throw new InterruptedIOException("cancel"); } writeBytes(TWO_DASHES, out); callBackInfo.pos += TWO_DASHES.length(); writeBytes(boundary, out); callBackInfo.pos += boundary.length(); writeBytes(CR_LF, out); callBackInfo.pos += CR_LF.length(); MinimalFieldHeader header = part.getHeader(); switch (mode) { case STRICT: for (MinimalField field : header) { writeField(field, out); callBackInfo.pos += encode(MIME.DEFAULT_CHARSET, field.getName() + field.getBody()).length() + FIELD_SEP.length() + CR_LF.length(); } break; case BROWSER_COMPATIBLE: // Only write Content-Disposition // Use content charset MinimalField cd = header.getField(MIME.CONTENT_DISPOSITION); writeField(cd, this.charset, out); callBackInfo.pos += encode(this.charset, cd.getName() + cd.getBody()).length() + FIELD_SEP.length() + CR_LF.length(); String filename = part.getBody().getFilename(); if (filename != null) { MinimalField ct = header.getField(MIME.CONTENT_TYPE); writeField(ct, this.charset, out); callBackInfo.pos += encode(this.charset, ct.getName() + ct.getBody()).length() + FIELD_SEP.length() + CR_LF.length(); } break; default: break; } writeBytes(CR_LF, out); callBackInfo.pos += CR_LF.length(); if (writeContent) { ContentBody body = part.getBody(); body.setCallBackInfo(callBackInfo); body.writeTo(out); } writeBytes(CR_LF, out); callBackInfo.pos += CR_LF.length(); } writeBytes(TWO_DASHES, out); callBackInfo.pos += TWO_DASHES.length(); writeBytes(boundary, out); callBackInfo.pos += boundary.length(); writeBytes(TWO_DASHES, out); callBackInfo.pos += TWO_DASHES.length(); writeBytes(CR_LF, out); callBackInfo.pos += CR_LF.length(); callBackInfo.doCallBack(true); }
From source file:com.dongfang.net.http.client.multipart.HttpMultipart.java
private void doWriteTo(final HttpMultipartMode mode, final OutputStream out, MultipartEntity.CallBackInfo callBackInfo, boolean writeContent) throws IOException { callBackInfo.pos = 0;/* w w w.ja v a 2 s . co m*/ ByteArrayBuffer boundary = encode(this.charset, getBoundary()); for (FormBodyPart part : this.parts) { if (!callBackInfo.doCallBack(true)) { throw new InterruptedIOException("stop"); } writeBytes(TWO_DASHES, out); callBackInfo.pos += TWO_DASHES.length(); writeBytes(boundary, out); callBackInfo.pos += boundary.length(); writeBytes(CR_LF, out); callBackInfo.pos += CR_LF.length(); MinimalFieldHeader header = part.getHeader(); switch (mode) { case STRICT: for (MinimalField field : header) { writeField(field, out); callBackInfo.pos += encode(MIME.DEFAULT_CHARSET, field.getName() + field.getBody()).length() + FIELD_SEP.length() + CR_LF.length(); } break; case BROWSER_COMPATIBLE: // Only write Content-Disposition // Use content charset MinimalField cd = header.getField(MIME.CONTENT_DISPOSITION); writeField(cd, this.charset, out); callBackInfo.pos += encode(this.charset, cd.getName() + cd.getBody()).length() + FIELD_SEP.length() + CR_LF.length(); String filename = part.getBody().getFilename(); if (filename != null) { MinimalField ct = header.getField(MIME.CONTENT_TYPE); writeField(ct, this.charset, out); callBackInfo.pos += encode(this.charset, ct.getName() + ct.getBody()).length() + FIELD_SEP.length() + CR_LF.length(); } break; default: break; } writeBytes(CR_LF, out); callBackInfo.pos += CR_LF.length(); if (writeContent) { ContentBody body = part.getBody(); body.setCallBackInfo(callBackInfo); body.writeTo(out); } writeBytes(CR_LF, out); callBackInfo.pos += CR_LF.length(); } writeBytes(TWO_DASHES, out); callBackInfo.pos += TWO_DASHES.length(); writeBytes(boundary, out); callBackInfo.pos += boundary.length(); writeBytes(TWO_DASHES, out); callBackInfo.pos += TWO_DASHES.length(); writeBytes(CR_LF, out); callBackInfo.pos += CR_LF.length(); callBackInfo.doCallBack(true); }
From source file:com.bitfable.ammocache.download.UrlImageDownloader.java
@Override protected Bitmap download(String key, WeakReference<ImageView> imageViewRef) { URL url;// w w w . j av a 2s . c o m try { url = new URL(key); } catch (MalformedURLException e) { Log.e(TAG, "url is malformed: " + key, e); return null; } HttpURLConnection urlConnection; try { urlConnection = (HttpURLConnection) url.openConnection(); } catch (IOException e) { Log.e(TAG, "error while opening connection", e); return null; } Bitmap bitmap = null; InputStream httpStream = null; int contentLength; int bytesDownloaded = 0; try { contentLength = urlConnection.getContentLength(); httpStream = new FlushedInputStream(urlConnection.getInputStream()); ByteArrayBuffer baf = new ByteArrayBuffer(BYTE_ARRAY_BUFFER_INCREMENTAL_SIZE); byte[] buffer = new byte[BYTE_ARRAY_BUFFER_INCREMENTAL_SIZE]; while (!isCancelled(imageViewRef)) { int incrementalRead = httpStream.read(buffer); if (incrementalRead == -1) { break; } bytesDownloaded += incrementalRead; if (contentLength > 0 || (bytesDownloaded > 0 && bytesDownloaded == contentLength)) { int progress = bytesDownloaded * 100 / contentLength; publishProgress(progress, imageViewRef); } baf.append(buffer, 0, incrementalRead); } if (isCancelled(imageViewRef)) return null; bitmap = BitmapFactory.decodeByteArray(baf.toByteArray(), 0, baf.length()); } catch (IOException e) { Log.e(TAG, "error creating InputStream", e); } finally { if (urlConnection != null) urlConnection.disconnect(); if (httpStream != null) { try { httpStream.close(); } catch (IOException e) { Log.e(TAG, "IOException while closing http stream", e); } } } return bitmap; }
From source file:org.mycard.net.network.LoadListener.java
protected void handleData(Message msg) { int len = msg.arg1; int totalLen = msg.arg2; ByteArrayBuffer buffer = (ByteArrayBuffer) msg.obj; try {/*from w ww.j av a2s . c o m*/ synchronized (mDataBuffer) { mDataBuffer.append(buffer.buffer(), 0, buffer.length()); } } catch (OutOfMemoryError error) { cancel(); HttpTaskEventArg arg = new HttpTaskEventArg(); arg.mErrorId = HttpTaskListener.ERROR_OUT_OF_MEMORY; mTaskListener.onHttpTaskEvent(m_taskid, HttpTaskListener.HTTPTASK_EVENT_FAIL, arg); return; } if (mTaskListener != null) { if ((mStatusCode >= 301 && mStatusCode <= 303) || mStatusCode == 307) { // } else { HttpTaskEventArg arg = new HttpTaskEventArg(); arg.mlen = len; arg.mTotal = totalLen; arg.buffer = buffer.buffer(); mTaskListener.onHttpTaskEvent(m_taskid, HttpTaskListener.HTTPTASK_EVENT_DATARECIVE, arg); } } }
From source file:com.download.cache.image.UrlImageDownloader.java
@Override protected Bitmap download(String key, WeakReference<ImageView> imageViewRef) { URL url;/*from ww w.j ava 2 s . c o m*/ try { url = new URL(key); } catch (MalformedURLException e) { Log.e(TAG, "url is malformed: " + key, e); return null; } HttpURLConnection urlConnection; try { urlConnection = (HttpURLConnection) url.openConnection(); } catch (IOException e) { Log.e(TAG, "error while opening connection", e); return null; } Bitmap bitmap = null; InputStream httpStream = null; int contentLength; int bytesDownloaded = 0; try { contentLength = urlConnection.getContentLength(); if (contentLength != -1) { httpStream = new FlushedInputStream(urlConnection.getInputStream()); ByteArrayBuffer baf = new ByteArrayBuffer(BYTE_ARRAY_BUFFER_INCREMENTAL_SIZE); byte[] buffer = new byte[BYTE_ARRAY_BUFFER_INCREMENTAL_SIZE]; while (!isCancelled(imageViewRef)) { int incrementalRead = httpStream.read(buffer); if (incrementalRead == -1) { break; } bytesDownloaded += incrementalRead; if (contentLength > 0 || (bytesDownloaded > 0 && bytesDownloaded == contentLength)) { int progress = bytesDownloaded * 100 / contentLength; publishProgress(progress, imageViewRef); } baf.append(buffer, 0, incrementalRead); } if (isCancelled(imageViewRef)) return null; bitmap = BitmapFactory.decodeByteArray(baf.toByteArray(), 0, baf.length()); } else { // steam // byte[] imageBuf; // byte[] buf = new byte[BYTE_ARRAY_BUFFER_INCREMENTAL_SIZE]; // int bufferLeft = buf.length; // int offset = 0; // int result = 0; // // BufferedInputStream bis = new BufferedInputStream(url.openStream()); // // InputStream is = url.openStream(); // outer: do { // while (bufferLeft > 0) { // result = is.read(buf, offset, bufferLeft); // if (result < 0) { // // we're done // break outer; // } // offset += result; // bufferLeft -= result; // } // // resize // bufferLeft = BYTE_ARRAY_BUFFER_INCREMENTAL_SIZE; // int newSize = buf.length + BYTE_ARRAY_BUFFER_INCREMENTAL_SIZE; // byte[] newBuf = new byte[newSize]; // System.arraycopy(buf, 0, newBuf, 0, buf.length); // buf = newBuf; // } while (true); // imageBuf = new byte[offset]; // System.arraycopy(buf, 0, imageBuf, 0, offset); // // bitmap = BitmapFactory.decodeByteArray(imageBuf, 0, imageBuf.length); } } catch (IOException e) { Log.e(TAG, "error creating InputStream", e); } finally { if (urlConnection != null) urlConnection.disconnect(); if (httpStream != null) { try { httpStream.close(); } catch (IOException e) { Log.e(TAG, "IOException while closing http stream", e); } } } return bitmap; }
From source file:com.vuze.android.remote.SessionInfo.java
public void openTorrent(final Activity activity, final String name, InputStream is) { try {//from w w w. j ava2 s . co m int available = is.available(); if (available <= 0) { available = 32 * 1024; } ByteArrayBuffer bab = new ByteArrayBuffer(available); boolean ok = AndroidUtils.readInputStreamIfStartWith(is, bab, new byte[] { 'd' }); if (!ok) { String s; if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT && bab.length() == 0) { s = activity.getResources().getString(R.string.not_torrent_file_kitkat, name); } else { s = activity.getResources().getString(R.string.not_torrent_file, name, Math.max(bab.length(), 5)); } AndroidUtils.showDialog(activity, R.string.add_torrent, Html.fromHtml(s)); return; } final String metainfo = Base64Encode.encodeToString(bab.buffer(), 0, bab.length()); openTorrentWithMetaData(activity, name, metainfo); } catch (IOException e) { if (AndroidUtils.DEBUG) { e.printStackTrace(); } VuzeEasyTracker.getInstance(activity).logError(e); } catch (OutOfMemoryError em) { VuzeEasyTracker.getInstance(activity).logError(em); AndroidUtils.showConnectionError(activity, "Out of Memory", true); } }