Java Code Examples for org.apache.http.util.ByteArrayBuffer#toByteArray()
The following examples show how to use
org.apache.http.util.ByteArrayBuffer#toByteArray() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpAltConnection.java From mdw with Apache License 2.0 | 6 votes |
/** * Converts the inpust stream to byte array. * @param is input stream to be converted * @return byte[] byte array * @throws IOException in case of a problem */ private byte[] extractResponseBytes(InputStream is) throws IOException { BufferedInputStream bis = null; try { ByteArrayBuffer baf = new ByteArrayBuffer(1024); bis = new BufferedInputStream(is); int b; while ((b = bis.read()) != -1) baf.append((byte) b); return baf.toByteArray(); } finally { if (bis != null) bis.close(); } }
Example 2
Source File: DataAsyncHttpResponseHandler.java From Android-Basics-Codes with Artistic License 2.0 | 5 votes |
/** * Returns byte array of response HttpEntity contents * * @param entity can be null * @return response entity body or null * @throws java.io.IOException if reading entity or creating byte array failed */ @Override byte[] getResponseData(HttpEntity entity) throws IOException { byte[] responseBody = null; if (entity != null) { InputStream instream = entity.getContent(); if (instream != null) { long contentLength = entity.getContentLength(); if (contentLength > Integer.MAX_VALUE) { throw new IllegalArgumentException("HTTP entity too large to be buffered in memory"); } if (contentLength < 0) { contentLength = BUFFER_SIZE; } try { ByteArrayBuffer buffer = new ByteArrayBuffer((int) contentLength); try { byte[] tmp = new byte[BUFFER_SIZE]; int l, count = 0; // do not send messages if request has been cancelled while ((l = instream.read(tmp)) != -1 && !Thread.currentThread().isInterrupted()) { buffer.append(tmp, 0, l); sendProgressDataMessage(copyOfRange(tmp, 0, l)); sendProgressMessage(count, (int) contentLength); } } finally { AsyncHttpClient.silentCloseInputStream(instream); } responseBody = buffer.toByteArray(); } catch (OutOfMemoryError e) { System.gc(); throw new IOException("File too large to fit into available memory"); } } } return responseBody; }
Example 3
Source File: AsyncHttpResponseHandler.java From Libraries-for-Android-Developers with MIT License | 5 votes |
/** * Returns byte array of response HttpEntity contents * * @param entity can be null * @return response entity body or null * @throws java.io.IOException if reading entity or creating byte array failed */ byte[] getResponseData(HttpEntity entity) throws IOException { byte[] responseBody = null; if (entity != null) { InputStream instream = entity.getContent(); if (instream != null) { long contentLength = entity.getContentLength(); if (contentLength > Integer.MAX_VALUE) { throw new IllegalArgumentException("HTTP entity too large to be buffered in memory"); } int buffersize = (contentLength < 0) ? BUFFER_SIZE : (int) contentLength; try { ByteArrayBuffer buffer = new ByteArrayBuffer(buffersize); try { byte[] tmp = new byte[BUFFER_SIZE]; int l, count = 0; // do not send messages if request has been cancelled while ((l = instream.read(tmp)) != -1 && !Thread.currentThread().isInterrupted()) { count += l; buffer.append(tmp, 0, l); sendProgressMessage(count, (int) contentLength); } } finally { instream.close(); } responseBody = buffer.toByteArray(); } catch (OutOfMemoryError e) { System.gc(); throw new IOException("File too large to fit into available memory"); } } } return responseBody; }
Example 4
Source File: HttpProtocol.java From storm-crawler with Apache License 2.0 | 5 votes |
private static final byte[] toByteArray(final HttpEntity entity, int maxContent, MutableBoolean trimmed) throws IOException { if (entity == null) return new byte[] {}; final InputStream instream = entity.getContent(); if (instream == null) { return null; } Args.check(entity.getContentLength() <= Integer.MAX_VALUE, "HTTP entity too large to be buffered in memory"); int reportedLength = (int) entity.getContentLength(); // set default size for buffer: 100 KB int bufferInitSize = 102400; if (reportedLength != -1) { bufferInitSize = reportedLength; } // avoid init of too large a buffer when we will trim anyway if (maxContent != -1 && bufferInitSize > maxContent) { bufferInitSize = maxContent; } final ByteArrayBuffer buffer = new ByteArrayBuffer(bufferInitSize); final byte[] tmp = new byte[4096]; int lengthRead; while ((lengthRead = instream.read(tmp)) != -1) { // check whether we need to trim if (maxContent != -1 && buffer.length() + lengthRead > maxContent) { buffer.append(tmp, 0, maxContent - buffer.length()); trimmed.setValue(true); break; } buffer.append(tmp, 0, lengthRead); } return buffer.toByteArray(); }
Example 5
Source File: BasePart.java From barterli_android with Apache License 2.0 | 5 votes |
private byte[] generateHeader(Boundary boundary) { if (headersProvider == null) { throw new RuntimeException("Uninitialized headersProvider"); //$NON-NLS-1$ } final ByteArrayBuffer buf = new ByteArrayBuffer(256); append(buf, boundary.getStartingBoundary()); append(buf, headersProvider.getContentDisposition()); append(buf, CRLF); append(buf, headersProvider.getContentType()); append(buf, CRLF); append(buf, headersProvider.getContentTransferEncoding()); append(buf, CRLF); append(buf, CRLF); return buf.toByteArray(); }
Example 6
Source File: DataAsyncHttpResponseHandler.java From Libraries-for-Android-Developers with MIT License | 5 votes |
/** * Returns byte array of response HttpEntity contents * * @param entity can be null * @return response entity body or null * @throws java.io.IOException if reading entity or creating byte array failed */ @Override byte[] getResponseData(HttpEntity entity) throws IOException { byte[] responseBody = null; if (entity != null) { InputStream instream = entity.getContent(); if (instream != null) { long contentLength = entity.getContentLength(); if (contentLength > Integer.MAX_VALUE) { throw new IllegalArgumentException("HTTP entity too large to be buffered in memory"); } if (contentLength < 0) { contentLength = BUFFER_SIZE; } try { ByteArrayBuffer buffer = new ByteArrayBuffer((int) contentLength); try { byte[] tmp = new byte[BUFFER_SIZE]; int l; // do not send messages if request has been cancelled while ((l = instream.read(tmp)) != -1 && !Thread.currentThread().isInterrupted()) { buffer.append(tmp, 0, l); sendProgressDataMessage(copyOfRange(tmp, 0, l)); } } finally { instream.close(); } responseBody = buffer.toByteArray(); } catch (OutOfMemoryError e) { System.gc(); throw new IOException("File too large to fit into available memory"); } } } return responseBody; }
Example 7
Source File: BasePart.java From volley with Apache License 2.0 | 5 votes |
private byte[] generateHeader(Boundary boundary) { if (headersProvider == null) { throw new RuntimeException("Uninitialized headersProvider"); //$NON-NLS-1$ } final ByteArrayBuffer buf = new ByteArrayBuffer(256); append(buf, boundary.getStartingBoundary()); append(buf, headersProvider.getContentDisposition()); append(buf, CRLF); append(buf, headersProvider.getContentType()); append(buf, CRLF); append(buf, headersProvider.getContentTransferEncoding()); append(buf, CRLF); append(buf, CRLF); return buf.toByteArray(); }
Example 8
Source File: DataAsyncHttpResponseHandler.java From Android-Basics-Codes with Artistic License 2.0 | 5 votes |
/** * Returns byte array of response HttpEntity contents * * @param entity can be null * @return response entity body or null * @throws java.io.IOException if reading entity or creating byte array failed */ @Override byte[] getResponseData(HttpEntity entity) throws IOException { byte[] responseBody = null; if (entity != null) { InputStream instream = entity.getContent(); if (instream != null) { long contentLength = entity.getContentLength(); if (contentLength > Integer.MAX_VALUE) { throw new IllegalArgumentException("HTTP entity too large to be buffered in memory"); } if (contentLength < 0) { contentLength = BUFFER_SIZE; } try { ByteArrayBuffer buffer = new ByteArrayBuffer((int) contentLength); try { byte[] tmp = new byte[BUFFER_SIZE]; int l, count = 0; // do not send messages if request has been cancelled while ((l = instream.read(tmp)) != -1 && !Thread.currentThread().isInterrupted()) { buffer.append(tmp, 0, l); sendProgressDataMessage(copyOfRange(tmp, 0, l)); sendProgressMessage(count, (int) contentLength); } } finally { AsyncHttpClient.silentCloseInputStream(instream); } responseBody = buffer.toByteArray(); } catch (OutOfMemoryError e) { System.gc(); throw new IOException("File too large to fit into available memory"); } } } return responseBody; }
Example 9
Source File: EntityUtils.java From RoboZombie with Apache License 2.0 | 5 votes |
/** * Read the contents of an entity and return it as a byte array. * * @param entity * @return byte array containing the entity content. May be null if * {@link HttpEntity#getContent()} is null. * @throws IOException if an error occurs reading the input stream * @throws IllegalArgumentException if entity is null or if content length > Integer.MAX_VALUE */ public static byte[] toByteArray(final HttpEntity entity) throws IOException { if (entity == null) { throw new IllegalArgumentException("HTTP entity may not be null"); } InputStream instream = entity.getContent(); if (instream == null) { return null; } try { if (entity.getContentLength() > Integer.MAX_VALUE) { throw new IllegalArgumentException("HTTP entity too large to be buffered in memory"); } int i = (int)entity.getContentLength(); if (i < 0) { i = 4096; } ByteArrayBuffer buffer = new ByteArrayBuffer(i); byte[] tmp = new byte[4096]; int l; while((l = instream.read(tmp)) != -1) { buffer.append(tmp, 0, l); } return buffer.toByteArray(); } finally { instream.close(); } }
Example 10
Source File: DataAsyncHttpResponseHandler.java From Android-Basics-Codes with Artistic License 2.0 | 5 votes |
/** * Returns byte array of response HttpEntity contents * * @param entity can be null * @return response entity body or null * @throws java.io.IOException if reading entity or creating byte array failed */ @Override byte[] getResponseData(HttpEntity entity) throws IOException { byte[] responseBody = null; if (entity != null) { InputStream instream = entity.getContent(); if (instream != null) { long contentLength = entity.getContentLength(); if (contentLength > Integer.MAX_VALUE) { throw new IllegalArgumentException("HTTP entity too large to be buffered in memory"); } if (contentLength < 0) { contentLength = BUFFER_SIZE; } try { ByteArrayBuffer buffer = new ByteArrayBuffer((int) contentLength); try { byte[] tmp = new byte[BUFFER_SIZE]; int l, count = 0; // do not send messages if request has been cancelled while ((l = instream.read(tmp)) != -1 && !Thread.currentThread().isInterrupted()) { buffer.append(tmp, 0, l); sendProgressDataMessage(copyOfRange(tmp, 0, l)); sendProgressMessage(count, (int) contentLength); } } finally { AsyncHttpClient.silentCloseInputStream(instream); } responseBody = buffer.toByteArray(); } catch (OutOfMemoryError e) { System.gc(); throw new IOException("File too large to fit into available memory"); } } } return responseBody; }
Example 11
Source File: AsyncHttpResponseHandler.java From Android-Basics-Codes with Artistic License 2.0 | 5 votes |
/** * Returns byte array of response HttpEntity contents * * @param entity can be null * @return response entity body or null * @throws java.io.IOException if reading entity or creating byte array failed */ byte[] getResponseData(HttpEntity entity) throws IOException { byte[] responseBody = null; if (entity != null) { InputStream instream = entity.getContent(); if (instream != null) { long contentLength = entity.getContentLength(); if (contentLength > Integer.MAX_VALUE) { throw new IllegalArgumentException("HTTP entity too large to be buffered in memory"); } int buffersize = (contentLength <= 0) ? BUFFER_SIZE : (int) contentLength; try { ByteArrayBuffer buffer = new ByteArrayBuffer(buffersize); try { byte[] tmp = new byte[BUFFER_SIZE]; int l, count = 0; // do not send messages if request has been cancelled while ((l = instream.read(tmp)) != -1 && !Thread.currentThread().isInterrupted()) { count += l; buffer.append(tmp, 0, l); sendProgressMessage(count, (int) (contentLength <= 0 ? 1 : contentLength)); } } finally { AsyncHttpClient.silentCloseInputStream(instream); AsyncHttpClient.endEntityViaReflection(entity); } responseBody = buffer.toByteArray(); } catch (OutOfMemoryError e) { System.gc(); throw new IOException("File too large to fit into available memory"); } } } return responseBody; }
Example 12
Source File: HttpHandler.java From JerryMouse with MIT License | 5 votes |
@Override public void process(ByteBuffer output, ByteArrayBuffer rawRequest) throws Exception { int state = getState(); if (state == CLOSED) { throw new EOFException(); } else if (state == SENDING) { HttpRequestParser parser = new HttpRequestParser(rawRequest.toByteArray(), HttpRequest.newBuilder()); HttpRequest request = parser.parse(); HttpResponse response = new HttpResponse(outputBuffer); HttpDispatchProcessor processor = new HttpDispatchProcessor(getContext()); processor.process(request, response); response.putOutputBuffer(); } }
Example 13
Source File: AsyncHttpResponseHandler.java From Android-Basics-Codes with Artistic License 2.0 | 5 votes |
/** * Returns byte array of response HttpEntity contents * * @param entity can be null * @return response entity body or null * @throws java.io.IOException if reading entity or creating byte array failed */ byte[] getResponseData(HttpEntity entity) throws IOException { byte[] responseBody = null; if (entity != null) { InputStream instream = entity.getContent(); if (instream != null) { long contentLength = entity.getContentLength(); if (contentLength > Integer.MAX_VALUE) { throw new IllegalArgumentException("HTTP entity too large to be buffered in memory"); } int buffersize = (contentLength <= 0) ? BUFFER_SIZE : (int) contentLength; try { ByteArrayBuffer buffer = new ByteArrayBuffer(buffersize); try { byte[] tmp = new byte[BUFFER_SIZE]; int l, count = 0; // do not send messages if request has been cancelled while ((l = instream.read(tmp)) != -1 && !Thread.currentThread().isInterrupted()) { count += l; buffer.append(tmp, 0, l); sendProgressMessage(count, (int) (contentLength <= 0 ? 1 : contentLength)); } } finally { AsyncHttpClient.silentCloseInputStream(instream); AsyncHttpClient.endEntityViaReflection(entity); } responseBody = buffer.toByteArray(); } catch (OutOfMemoryError e) { System.gc(); throw new IOException("File too large to fit into available memory"); } } } return responseBody; }
Example 14
Source File: DataAsyncHttpResponseHandler.java From android-project-wo2b with Apache License 2.0 | 5 votes |
/** * Returns byte array of response HttpEntity contents * * @param entity can be null * @return response entity body or null * @throws java.io.IOException if reading entity or creating byte array failed */ @Override byte[] getResponseData(HttpEntity entity) throws IOException { byte[] responseBody = null; if (entity != null) { InputStream instream = entity.getContent(); if (instream != null) { long contentLength = entity.getContentLength(); if (contentLength > Integer.MAX_VALUE) { throw new IllegalArgumentException("HTTP entity too large to be buffered in memory"); } if (contentLength < 0) { contentLength = BUFFER_SIZE; } try { ByteArrayBuffer buffer = new ByteArrayBuffer((int) contentLength); try { byte[] tmp = new byte[BUFFER_SIZE]; int l; // do not send messages if request has been cancelled while ((l = instream.read(tmp)) != -1 && !Thread.currentThread().isInterrupted()) { buffer.append(tmp, 0, l); sendProgressDataMessage(copyOfRange(tmp, 0, l)); } } finally { AsyncHttpClient.silentCloseInputStream(instream); } responseBody = buffer.toByteArray(); } catch (OutOfMemoryError e) { System.gc(); throw new IOException("File too large to fit into available memory"); } } } return responseBody; }
Example 15
Source File: AsyncHttpResponseHandler.java From android-project-wo2b with Apache License 2.0 | 5 votes |
/** * Returns byte array of response HttpEntity contents * * @param entity can be null * @return response entity body or null * @throws java.io.IOException if reading entity or creating byte array failed */ byte[] getResponseData(HttpEntity entity) throws IOException { byte[] responseBody = null; if (entity != null) { InputStream instream = entity.getContent(); if (instream != null) { long contentLength = entity.getContentLength(); if (contentLength > Integer.MAX_VALUE) { throw new IllegalArgumentException("HTTP entity too large to be buffered in memory"); } int buffersize = (contentLength <= 0) ? BUFFER_SIZE : (int) contentLength; try { ByteArrayBuffer buffer = new ByteArrayBuffer(buffersize); try { byte[] tmp = new byte[BUFFER_SIZE]; int l, count = 0; // do not send messages if request has been cancelled while ((l = instream.read(tmp)) != -1 && !Thread.currentThread().isInterrupted()) { count += l; buffer.append(tmp, 0, l); sendProgressMessage(count, (int) (contentLength <= 0 ? 1 : contentLength)); } } finally { AsyncHttpClient.silentCloseInputStream(instream); AsyncHttpClient.endEntityViaReflection(entity); } responseBody = buffer.toByteArray(); } catch (OutOfMemoryError e) { System.gc(); throw new IOException("File too large to fit into available memory"); } } } return responseBody; }
Example 16
Source File: AsyncHttpResponseHandler.java From sealtalk-android with MIT License | 5 votes |
byte[] getResponseData(HttpEntity entity) throws IOException { byte[] responseBody = null; if (entity != null) { InputStream instream = entity.getContent(); if (instream != null) { long contentLength = entity.getContentLength(); if (contentLength > Integer.MAX_VALUE) { throw new IllegalArgumentException("HTTP entity too large to be buffered in memory"); } if (contentLength < 0) { contentLength = BUFFER_SIZE; } try { ByteArrayBuffer buffer = new ByteArrayBuffer((int) contentLength); try { byte[] tmp = new byte[BUFFER_SIZE]; int l, count = 0; // do not send messages if request has been cancelled while ((l = instream.read(tmp)) != -1 && !Thread.currentThread().isInterrupted()) { count += l; buffer.append(tmp, 0, l); sendProgressMessage(count, (int) contentLength); } } finally { instream.close(); } responseBody = buffer.toByteArray(); } catch (OutOfMemoryError e) { System.gc(); throw new IOException("File too large to fit into available memory"); } } } return responseBody; }
Example 17
Source File: AsyncHttpResponseHandler.java From AndroidWear-OpenWear with MIT License | 5 votes |
/** * Returns byte array of response HttpEntity contents * * @param entity can be null * @return response entity body or null * @throws IOException if reading entity or creating byte array failed */ byte[] getResponseData(HttpEntity entity) throws IOException { byte[] responseBody = null; if (entity != null) { InputStream instream = entity.getContent(); if (instream != null) { long contentLength = entity.getContentLength(); if (contentLength > Integer.MAX_VALUE) { throw new IllegalArgumentException("HTTP entity too large to be buffered in memory"); } int buffersize = (contentLength <= 0) ? BUFFER_SIZE : (int) contentLength; try { ByteArrayBuffer buffer = new ByteArrayBuffer(buffersize); try { byte[] tmp = new byte[BUFFER_SIZE]; long count = 0; int l; // do not send messages if request has been cancelled while ((l = instream.read(tmp)) != -1 && !Thread.currentThread().isInterrupted()) { count += l; buffer.append(tmp, 0, l); sendProgressMessage(count, (contentLength <= 0 ? 1 : contentLength)); } } finally { AsyncHttpClient.silentCloseInputStream(instream); AsyncHttpClient.endEntityViaReflection(entity); } responseBody = buffer.toByteArray(); } catch (OutOfMemoryError e) { System.gc(); throw new IOException("File too large to fit into available memory"); } } } return responseBody; }
Example 18
Source File: DataAsyncHttpResponseHandler.java From Mobike with Apache License 2.0 | 5 votes |
/** * Returns byte array of response HttpEntity contents * * @param entity can be null * @return response entity body or null * @throws IOException if reading entity or creating byte array failed */ @Override byte[] getResponseData(HttpEntity entity) throws IOException { byte[] responseBody = null; if (entity != null) { InputStream instream = entity.getContent(); if (instream != null) { long contentLength = entity.getContentLength(); if (contentLength > Integer.MAX_VALUE) { throw new IllegalArgumentException("HTTP entity too large to be buffered in memory"); } if (contentLength < 0) { contentLength = BUFFER_SIZE; } try { ByteArrayBuffer buffer = new ByteArrayBuffer((int) contentLength); try { byte[] tmp = new byte[BUFFER_SIZE]; int l; // do not send messages if request has been cancelled while ((l = instream.read(tmp)) != -1 && !Thread.currentThread().isInterrupted()) { buffer.append(tmp, 0, l); sendProgressDataMessage(copyOfRange(tmp, 0, l)); } } finally { AsyncHttpClient.silentCloseInputStream(instream); } responseBody = buffer.toByteArray(); } catch (OutOfMemoryError e) { System.gc(); throw new IOException("File too large to fit into available memory"); } } } return responseBody; }
Example 19
Source File: AsyncHttpResponseHandler.java From Mobike with Apache License 2.0 | 5 votes |
/** * Returns byte array of response HttpEntity contents * * @param entity can be null * @return response entity body or null * @throws IOException if reading entity or creating byte array failed */ byte[] getResponseData(HttpEntity entity) throws IOException { byte[] responseBody = null; if (entity != null) { InputStream instream = entity.getContent(); if (instream != null) { long contentLength = entity.getContentLength(); if (contentLength > Integer.MAX_VALUE) { throw new IllegalArgumentException("HTTP entity too large to be buffered in memory"); } int buffersize = (contentLength <= 0) ? BUFFER_SIZE : (int) contentLength; try { ByteArrayBuffer buffer = new ByteArrayBuffer(buffersize); try { byte[] tmp = new byte[BUFFER_SIZE]; int l, count = 0; // do not send messages if request has been cancelled while ((l = instream.read(tmp)) != -1 && !Thread.currentThread().isInterrupted()) { count += l; buffer.append(tmp, 0, l); sendProgressMessage(count, (int) (contentLength <= 0 ? 1 : contentLength)); } } finally { AsyncHttpClient.silentCloseInputStream(instream); AsyncHttpClient.endEntityViaReflection(entity); } responseBody = buffer.toByteArray(); } catch (OutOfMemoryError e) { System.gc(); throw new IOException("File too large to fit into available memory"); } } } return responseBody; }
Example 20
Source File: EchoHandler.java From JerryMouse with MIT License | 5 votes |
@Override public void process(ByteBuffer output, ByteArrayBuffer rawRequest) throws EOFException { int state = getState(); if (state == CLOSED) { throw new EOFException(); } else if (state == SENDING) { String requestContent = new String(rawRequest.toByteArray()); byte[] response = requestContent.getBytes(StandardCharsets.UTF_8); output.put(response); } }