2626
2727import java .io .IOException ;
2828import java .io .InputStream ;
29- import java .util .Arrays ;
3029
3130public abstract class DataAsyncHttpResponseHandler extends AsyncHttpResponseHandler {
3231 private static final String LOG_TAG = "DataAsyncHttpResponseHandler" ;
@@ -42,8 +41,6 @@ public DataAsyncHttpResponseHandler() {
4241
4342 /**
4443 * Fired when the request progress, override to handle in your own code
45- *
46- * @param responseBody
4744 */
4845 public void onProgressData (byte [] responseBody ) {
4946 }
@@ -64,7 +61,7 @@ protected void handleMessage(Message message) {
6461 response = (Object []) message .obj ;
6562 if (response != null && response .length >= 1 ) {
6663 try {
67- onProgressData ((byte [])response [0 ]);
64+ onProgressData ((byte []) response [0 ]);
6865 } catch (Throwable t ) {
6966 Log .e (LOG_TAG , "custom onProgressData contains an error" , t );
7067 }
@@ -100,12 +97,11 @@ byte[] getResponseData(HttpEntity entity) throws IOException {
10097 ByteArrayBuffer buffer = new ByteArrayBuffer ((int ) contentLength );
10198 try {
10299 byte [] tmp = new byte [BUFFER_SIZE ];
103- int l , count = 0 ;
100+ int l ;
104101 // do not send messages if request has been cancelled
105102 while ((l = instream .read (tmp )) != -1 && !Thread .currentThread ().isInterrupted ()) {
106- count += l ;
107103 buffer .append (tmp , 0 , l );
108- sendProgressDataMessage (Arrays . copyOfRange (tmp , 0 , l ));
104+ sendProgressDataMessage (copyOfRange (tmp , 0 , l ));
109105 }
110106 } finally {
111107 instream .close ();
@@ -119,5 +115,35 @@ byte[] getResponseData(HttpEntity entity) throws IOException {
119115 }
120116 return responseBody ;
121117 }
118+
119+ /**
120+ * Copies elements from {@code original} into a new array, from indexes start (inclusive) to end
121+ * (exclusive). The original order of elements is preserved. If {@code end} is greater than
122+ * {@code original.length}, the result is padded with the value {@code (byte) 0}.
123+ *
124+ * @param original the original array
125+ * @param start the start index, inclusive
126+ * @param end the end index, exclusive
127+ * @return the new array
128+ * @throws ArrayIndexOutOfBoundsException if {@code start < 0 || start > original.length}
129+ * @throws IllegalArgumentException if {@code start > end}
130+ * @throws NullPointerException if {@code original == null}
131+ * @see java.util.Arrays
132+ * @since 1.6
133+ */
134+ public static byte [] copyOfRange (byte [] original , int start , int end ) throws ArrayIndexOutOfBoundsException , IllegalArgumentException , NullPointerException {
135+ if (start > end ) {
136+ throw new IllegalArgumentException ();
137+ }
138+ int originalLength = original .length ;
139+ if (start < 0 || start > originalLength ) {
140+ throw new ArrayIndexOutOfBoundsException ();
141+ }
142+ int resultLength = end - start ;
143+ int copyLength = Math .min (resultLength , originalLength - start );
144+ byte [] result = new byte [resultLength ];
145+ System .arraycopy (original , start , result , 0 , copyLength );
146+ return result ;
147+ }
122148}
123149
0 commit comments