org.apache.http.util.ByteArrayBuffer Java Examples

The following examples show how to use org.apache.http.util.ByteArrayBuffer. 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: HttpHandler.java    From JerryMouse with MIT License 6 votes vote down vote up
@Override
public boolean inputIsComplete(ByteBuffer input, ByteArrayBuffer rawRequest, int bytes) {
    if (bytes > 0) {
        input.flip();
        while (input.hasRemaining()) {
            byte ch = input.get();
            if (ch == 3) {
                setState(CLOSED);
                return true;
            } else {
                rawRequest.append((char) ch);
            }
        }
    } else if (bytes == -1) {
        setState(CLOSED);
        return true;
    }

    return true;
}
 
Example #2
Source File: HttpAltConnection.java    From mdw with Apache License 2.0 6 votes vote down vote up
/**
 * 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 #3
Source File: EchoHandler.java    From JerryMouse with MIT License 6 votes vote down vote up
@Override
public boolean inputIsComplete(ByteBuffer input, ByteArrayBuffer request, int bytes) throws IOException {
    if (bytes > 0) {
        input.flip();
        while (input.hasRemaining()) {
            byte ch = input.get();
            if (ch == 3) {
                setState(CLOSED);
                return true;
            } else if (ch == '\r') {
            } else if (ch == '\n') {
                setState(SENDING);
                return true;
            } else {
                request.append((char) ch);
            }
        }

    } else if (bytes == -1) {
        throw new EOFException();
    }
    return false;
}
 
Example #4
Source File: CountBaseHandler.java    From JerryMouse with MIT License 6 votes vote down vote up
@Override
public boolean inputIsComplete(ByteBuffer input, ByteArrayBuffer request, int bytes) {
    if (bytes > 0) {
        input.flip(); // 切换成读取模式
        while (input.hasRemaining()) {
            byte ch = input.get();
            if (ch == 3) {
                setState(CLOSED);
                return true;
            } else if (ch == '\r') { // continue
            } else if (ch == '\n') {
                setState(SENDING);
                return true;
            } else {
                request.append((char) ch);
            }
        }
    } else if (bytes == -1) {
        setState(CLOSED);
        return true;
    }

    return false;
}
 
Example #5
Source File: MultipartForm.java    From iaf with Apache License 2.0 6 votes vote down vote up
void doWriteTo(
	final OutputStream out,
	final boolean writeContent) throws IOException {

	final ByteArrayBuffer boundaryEncoded = encode(this.charset, this.boundary);
	for (final FormBodyPart part: getBodyParts()) {
		writeBytes(TWO_DASHES, out);
		writeBytes(boundaryEncoded, out);
		writeBytes(CR_LF, out);

		formatMultipartHeader(part, out);

		writeBytes(CR_LF, out);

		if (writeContent) {
			part.getBody().writeTo(out);
		}
		writeBytes(CR_LF, out);
	}
	writeBytes(TWO_DASHES, out);
	writeBytes(boundaryEncoded, out);
	writeBytes(TWO_DASHES, out);
	writeBytes(CR_LF, out);
}
 
Example #6
Source File: HttpMultipart.java    From android-open-project-demo with Apache License 2.0 5 votes vote down vote up
private static ByteArrayBuffer encode(
        final Charset charset, final String string) {
    ByteBuffer encoded = charset.encode(CharBuffer.wrap(string));
    ByteArrayBuffer bab = new ByteArrayBuffer(encoded.remaining());
    bab.append(encoded.array(), encoded.position(), encoded.remaining());
    return bab;
}
 
Example #7
Source File: DataAsyncHttpResponseHandler.java    From android-project-wo2b with Apache License 2.0 5 votes vote down vote up
/**
 * 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 #8
Source File: AsyncHttpResponseHandler.java    From Android-Basics-Codes with Artistic License 2.0 5 votes vote down vote up
/**
 * 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 #9
Source File: AsyncHttpResponseHandler.java    From Android-Basics-Codes with Artistic License 2.0 5 votes vote down vote up
/**
 * 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 #10
Source File: DataAsyncHttpResponseHandler.java    From Android-Basics-Codes with Artistic License 2.0 5 votes vote down vote up
/**
 * 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 vote down vote up
/**
 * 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: DataAsyncHttpResponseHandler.java    From Android-Basics-Codes with Artistic License 2.0 5 votes vote down vote up
/**
 * 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 #13
Source File: BasePart.java    From volley with Apache License 2.0 5 votes vote down vote up
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 #14
Source File: Utils.java    From Evolve with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
protected Boolean doInBackground(String... vals) {
    try{
        URL updateURL = new URL(vals[0]);
        URLConnection conn = updateURL.openConnection();
        InputStream is = conn.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        ByteArrayBuffer baf = new ByteArrayBuffer(50);

        int current = 0;
        while((current = bis.read()) != -1){
            baf.append((byte)current);
        }

     /* Convert the Bytes read to a String. */
        final String s = new String(baf.toByteArray());

     /* Get current Version Number */
        int curVersion = Integer.valueOf(vals[1]);
        int newVersion = Integer.valueOf(s);

     /* Is a higher version than the current already out? */
        if (newVersion > curVersion) {
            return true;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}
 
Example #15
Source File: BasePart.java    From barterli_android with Apache License 2.0 5 votes vote down vote up
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 #16
Source File: DataAsyncHttpResponseHandler.java    From Android-Basics-Codes with Artistic License 2.0 5 votes vote down vote up
/**
 * 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 #17
Source File: ByteArrayResponseHandler.java    From YiBo with Apache License 2.0 5 votes vote down vote up
/**
 * HttpEntity到 byte数组的转换
 *
 * @param entity HttpEntity对象
 * @return byte[] byte数组对象
 * @throws IOException
 */
private byte[] toByteArray(final HttpEntity entity) throws IOException {
       if (entity == null) {
       	throw new LibRuntimeException(LibResultCode.E_PARAM_ERROR, "", "Null HttpEntity", ServiceProvider.None);
       }
       InputStream instream = entity.getContent();
       if (instream == null) {
           return new byte[] {};
       }
       if (entity.getContentLength() > Integer.MAX_VALUE) {
       	throw new LibRuntimeException(LibResultCode.E_PARAM_ERROR, "", "HTTP entity too large to be buffered in memory", ServiceProvider.None);
       }
       
	Logger.verbose("ByteArrayResponseHandler: content-type{}", entity.getContentType());

       int i = (int) entity.getContentLength();
       if (i < 0) {
           i = 4096;
       }
       ByteArrayBuffer buffer = new ByteArrayBuffer(i);
       try {
           byte[] tmp = new byte[4096];
           int l;
           while ((l = instream.read(tmp)) != -1) {
               buffer.append(tmp, 0, l);
           }
       } finally {
           instream.close();
       }
       return buffer.buffer();
   }
 
Example #18
Source File: MultipartForm.java    From iaf with Apache License 2.0 5 votes vote down vote up
private static ByteArrayBuffer encode(
		final Charset charset, final String string) {
	final ByteBuffer encoded = charset.encode(CharBuffer.wrap(string));
	final ByteArrayBuffer bab = new ByteArrayBuffer(encoded.remaining());
	bab.append(encoded.array(), encoded.position(), encoded.remaining());
	return bab;
}
 
Example #19
Source File: HttpProtocol.java    From storm-crawler with Apache License 2.0 5 votes vote down vote up
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 #20
Source File: AsyncHttpResponseHandler.java    From Libraries-for-Android-Developers with MIT License 5 votes vote down vote up
/**
 * 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 #21
Source File: DataAsyncHttpResponseHandler.java    From Libraries-for-Android-Developers with MIT License 5 votes vote down vote up
/**
 * 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 #22
Source File: EntityUtils.java    From RoboZombie with Apache License 2.0 5 votes vote down vote up
/**
 * 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 #23
Source File: AsyncHttpResponseHandler.java    From android-project-wo2b with Apache License 2.0 5 votes vote down vote up
/**
 * 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 #24
Source File: AsyncHttpResponseHandler.java    From Mobike with Apache License 2.0 5 votes vote down vote up
/**
 * 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 #25
Source File: DataAsyncHttpResponseHandler.java    From Mobike with Apache License 2.0 5 votes vote down vote up
/**
 * 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 #26
Source File: AsyncHttpResponseHandler.java    From AndroidWear-OpenWear with MIT License 5 votes vote down vote up
/**
 * 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 #27
Source File: MultiReactorTest.java    From JerryMouse with MIT License 5 votes vote down vote up
@Override
public void process(ByteBuffer output, ByteArrayBuffer request) throws EOFException {
    int state = getState();
    if (state == CLOSED) {
        throw new EOFException();
    } else if (state == SENDING) {
        output.clear();
        String name = getReactorName();
        output.put(name.getBytes());
    }
}
 
Example #28
Source File: HttpMultipart.java    From BigApp_Discuz_Android with Apache License 2.0 5 votes vote down vote up
private static ByteArrayBuffer encode(
        final Charset charset, final String string) {
    ByteBuffer encoded = charset.encode(CharBuffer.wrap(string));
    ByteArrayBuffer bab = new ByteArrayBuffer(encoded.remaining());
    bab.append(encoded.array(), encoded.position(), encoded.remaining());
    return bab;
}
 
Example #29
Source File: CountBaseHandler.java    From JerryMouse with MIT License 5 votes vote down vote up
@Override
public void process(ByteBuffer output, ByteArrayBuffer request) throws EOFException {
    int state = getState();
    if (state == CLOSED) {
        throw new EOFException();
    } else if (state == SENDING) {
        output.put("1".getBytes());
    }
}
 
Example #30
Source File: HandlerTest.java    From JerryMouse with MIT License 5 votes vote down vote up
@Override
public void process(ByteBuffer output, ByteArrayBuffer request) throws EOFException {
    int state = getState();
    if (state == CLOSED) {
        throw new EOFException();
    } else if (state == Constants.SENDING) {
        output.clear();
        output.put(Thread.currentThread().getName().getBytes());
    }
}