26
26
27
27
import java .io .IOException ;
28
28
import java .io .InputStream ;
29
- import java .util .Arrays ;
30
29
31
30
public abstract class DataAsyncHttpResponseHandler extends AsyncHttpResponseHandler {
32
31
private static final String LOG_TAG = "DataAsyncHttpResponseHandler" ;
@@ -42,8 +41,6 @@ public DataAsyncHttpResponseHandler() {
42
41
43
42
/**
44
43
* Fired when the request progress, override to handle in your own code
45
- *
46
- * @param responseBody
47
44
*/
48
45
public void onProgressData (byte [] responseBody ) {
49
46
}
@@ -64,7 +61,7 @@ protected void handleMessage(Message message) {
64
61
response = (Object []) message .obj ;
65
62
if (response != null && response .length >= 1 ) {
66
63
try {
67
- onProgressData ((byte [])response [0 ]);
64
+ onProgressData ((byte []) response [0 ]);
68
65
} catch (Throwable t ) {
69
66
Log .e (LOG_TAG , "custom onProgressData contains an error" , t );
70
67
}
@@ -100,12 +97,11 @@ byte[] getResponseData(HttpEntity entity) throws IOException {
100
97
ByteArrayBuffer buffer = new ByteArrayBuffer ((int ) contentLength );
101
98
try {
102
99
byte [] tmp = new byte [BUFFER_SIZE ];
103
- int l , count = 0 ;
100
+ int l ;
104
101
// do not send messages if request has been cancelled
105
102
while ((l = instream .read (tmp )) != -1 && !Thread .currentThread ().isInterrupted ()) {
106
- count += l ;
107
103
buffer .append (tmp , 0 , l );
108
- sendProgressDataMessage (Arrays . copyOfRange (tmp , 0 , l ));
104
+ sendProgressDataMessage (copyOfRange (tmp , 0 , l ));
109
105
}
110
106
} finally {
111
107
instream .close ();
@@ -119,5 +115,35 @@ byte[] getResponseData(HttpEntity entity) throws IOException {
119
115
}
120
116
return responseBody ;
121
117
}
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
+ }
122
148
}
123
149
0 commit comments