Skip to content

Commit 1292fef

Browse files
committed
Fixed lint about toUpperCase, and backported Arrays.copyOfRange(byte[],int,int) for API < 9
1 parent 835b5f5 commit 1292fef

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

library/src/main/java/com/loopj/android/http/DataAsyncHttpResponseHandler.java

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
import java.io.IOException;
2828
import java.io.InputStream;
29-
import java.util.Arrays;
3029

3130
public 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

library/src/main/java/com/loopj/android/http/JsonStreamerEntity.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.io.InputStream;
3030
import java.io.OutputStream;
3131
import java.util.HashMap;
32+
import java.util.Locale;
3233
import java.util.Map;
3334
import java.util.Set;
3435
import java.util.zip.GZIPOutputStream;
@@ -297,7 +298,7 @@ static byte[] escape(String string) {
297298
for (int zero = 0; zero < intLength; zero++) {
298299
BUILDER.append('0');
299300
}
300-
BUILDER.append(intString.toUpperCase());
301+
BUILDER.append(intString.toUpperCase(Locale.US));
301302
} else {
302303
BUILDER.append(ch);
303304
}

library/src/main/java/com/loopj/android/http/PersistentCookieStore.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.ArrayList;
3434
import java.util.Date;
3535
import java.util.List;
36+
import java.util.Locale;
3637
import java.util.concurrent.ConcurrentHashMap;
3738

3839
/**
@@ -201,7 +202,7 @@ protected String byteArrayToHexString(byte[] bytes) {
201202
}
202203
sb.append(Integer.toHexString(v));
203204
}
204-
return sb.toString().toUpperCase();
205+
return sb.toString().toUpperCase(Locale.US);
205206
}
206207

207208
/**

0 commit comments

Comments
 (0)