|
| 1 | +package com.loopj.android.http; |
| 2 | + |
| 3 | +/* |
| 4 | + Android Asynchronous Http Client |
| 5 | + Copyright (c) 2011 James Smith <[email protected]> |
| 6 | + http://loopj.com |
| 7 | +
|
| 8 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | + you may not use this file except in compliance with the License. |
| 10 | + You may obtain a copy of the License at |
| 11 | +
|
| 12 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | +
|
| 14 | + Unless required by applicable law or agreed to in writing, software |
| 15 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + See the License for the specific language governing permissions and |
| 18 | + limitations under the License. |
| 19 | +*/ |
| 20 | + |
| 21 | +import android.os.Message; |
| 22 | +import android.util.Log; |
| 23 | + |
| 24 | +import org.apache.http.HttpEntity; |
| 25 | +import org.apache.http.util.ByteArrayBuffer; |
| 26 | + |
| 27 | +import java.io.IOException; |
| 28 | +import java.io.InputStream; |
| 29 | +import java.util.Arrays; |
| 30 | + |
| 31 | +public abstract class DataAsyncHttpResponseHandler extends AsyncHttpResponseHandler { |
| 32 | + private static final String LOG_TAG = "DataAsyncHttpResponseHandler"; |
| 33 | + |
| 34 | + protected static final int PROGRESS_DATA_MESSAGE = 6; |
| 35 | + |
| 36 | + /** |
| 37 | + * Creates a new AsyncHttpResponseHandler |
| 38 | + */ |
| 39 | + public DataAsyncHttpResponseHandler() { |
| 40 | + super(); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Fired when the request progress, override to handle in your own code |
| 45 | + * |
| 46 | + * @param responseBody |
| 47 | + */ |
| 48 | + public void onProgressData(byte[] responseBody) { |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + final public void sendProgressDataMessage(byte[] responseBytes) { |
| 53 | + sendMessage(obtainMessage(PROGRESS_DATA_MESSAGE, new Object[]{responseBytes})); |
| 54 | + } |
| 55 | + |
| 56 | + // Methods which emulate android's Handler and Message methods |
| 57 | + @Override |
| 58 | + protected void handleMessage(Message message) { |
| 59 | + super.handleMessage(message); |
| 60 | + Object[] response; |
| 61 | + |
| 62 | + switch (message.what) { |
| 63 | + case PROGRESS_DATA_MESSAGE: |
| 64 | + response = (Object[]) message.obj; |
| 65 | + if (response != null && response.length >= 1) { |
| 66 | + try { |
| 67 | + onProgressData((byte[])response[0]); |
| 68 | + } catch (Throwable t) { |
| 69 | + Log.e(LOG_TAG, "custom onProgressData contains an error", t); |
| 70 | + } |
| 71 | + } else { |
| 72 | + Log.e(LOG_TAG, "PROGRESS_DATA_MESSAGE didn't got enough params"); |
| 73 | + } |
| 74 | + break; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Returns byte array of response HttpEntity contents |
| 80 | + * |
| 81 | + * @param entity can be null |
| 82 | + * @return response entity body or null |
| 83 | + * @throws java.io.IOException if reading entity or creating byte array failed |
| 84 | + */ |
| 85 | + @Override |
| 86 | + byte[] getResponseData(HttpEntity entity) throws IOException { |
| 87 | + |
| 88 | + byte[] responseBody = null; |
| 89 | + if (entity != null) { |
| 90 | + InputStream instream = entity.getContent(); |
| 91 | + if (instream != null) { |
| 92 | + long contentLength = entity.getContentLength(); |
| 93 | + if (contentLength > Integer.MAX_VALUE) { |
| 94 | + throw new IllegalArgumentException("HTTP entity too large to be buffered in memory"); |
| 95 | + } |
| 96 | + if (contentLength < 0) { |
| 97 | + contentLength = BUFFER_SIZE; |
| 98 | + } |
| 99 | + try { |
| 100 | + ByteArrayBuffer buffer = new ByteArrayBuffer((int) contentLength); |
| 101 | + try { |
| 102 | + byte[] tmp = new byte[BUFFER_SIZE]; |
| 103 | + int l, count = 0; |
| 104 | + // do not send messages if request has been cancelled |
| 105 | + while ((l = instream.read(tmp)) != -1 && !Thread.currentThread().isInterrupted()) { |
| 106 | + count += l; |
| 107 | + buffer.append(tmp, 0, l); |
| 108 | + sendProgressDataMessage(Arrays.copyOfRange(tmp, 0, l)); |
| 109 | + } |
| 110 | + } finally { |
| 111 | + instream.close(); |
| 112 | + } |
| 113 | + responseBody = buffer.toByteArray(); |
| 114 | + } catch (OutOfMemoryError e) { |
| 115 | + System.gc(); |
| 116 | + throw new IOException("File too large to fit into available memory"); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + return responseBody; |
| 121 | + } |
| 122 | +} |
| 123 | + |
0 commit comments