diff --git a/library/src/main/java/com/loopj/android/http/DataAsyncHttpResponseHandler.java b/library/src/main/java/com/loopj/android/http/DataAsyncHttpResponseHandler.java index 69fb48457..dc35b42da 100644 --- a/library/src/main/java/com/loopj/android/http/DataAsyncHttpResponseHandler.java +++ b/library/src/main/java/com/loopj/android/http/DataAsyncHttpResponseHandler.java @@ -26,7 +26,6 @@ import java.io.IOException; import java.io.InputStream; -import java.util.Arrays; public abstract class DataAsyncHttpResponseHandler extends AsyncHttpResponseHandler { private static final String LOG_TAG = "DataAsyncHttpResponseHandler"; @@ -42,8 +41,6 @@ public DataAsyncHttpResponseHandler() { /** * Fired when the request progress, override to handle in your own code - * - * @param responseBody */ public void onProgressData(byte[] responseBody) { } @@ -64,7 +61,7 @@ protected void handleMessage(Message message) { response = (Object[]) message.obj; if (response != null && response.length >= 1) { try { - onProgressData((byte[])response[0]); + onProgressData((byte[]) response[0]); } catch (Throwable t) { Log.e(LOG_TAG, "custom onProgressData contains an error", t); } @@ -100,12 +97,11 @@ byte[] getResponseData(HttpEntity entity) throws IOException { ByteArrayBuffer buffer = new ByteArrayBuffer((int) contentLength); try { byte[] tmp = new byte[BUFFER_SIZE]; - int l, 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); - sendProgressDataMessage(Arrays.copyOfRange(tmp, 0, l)); + sendProgressDataMessage(copyOfRange(tmp, 0, l)); } } finally { instream.close(); @@ -119,5 +115,35 @@ byte[] getResponseData(HttpEntity entity) throws IOException { } return responseBody; } + + /** + * Copies elements from {@code original} into a new array, from indexes start (inclusive) to end + * (exclusive). The original order of elements is preserved. If {@code end} is greater than + * {@code original.length}, the result is padded with the value {@code (byte) 0}. + * + * @param original the original array + * @param start the start index, inclusive + * @param end the end index, exclusive + * @return the new array + * @throws ArrayIndexOutOfBoundsException if {@code start < 0 || start > original.length} + * @throws IllegalArgumentException if {@code start > end} + * @throws NullPointerException if {@code original == null} + * @see java.util.Arrays + * @since 1.6 + */ + public static byte[] copyOfRange(byte[] original, int start, int end) throws ArrayIndexOutOfBoundsException, IllegalArgumentException, NullPointerException { + if (start > end) { + throw new IllegalArgumentException(); + } + int originalLength = original.length; + if (start < 0 || start > originalLength) { + throw new ArrayIndexOutOfBoundsException(); + } + int resultLength = end - start; + int copyLength = Math.min(resultLength, originalLength - start); + byte[] result = new byte[resultLength]; + System.arraycopy(original, start, result, 0, copyLength); + return result; + } } diff --git a/library/src/main/java/com/loopj/android/http/JsonStreamerEntity.java b/library/src/main/java/com/loopj/android/http/JsonStreamerEntity.java index 181019e75..493750617 100644 --- a/library/src/main/java/com/loopj/android/http/JsonStreamerEntity.java +++ b/library/src/main/java/com/loopj/android/http/JsonStreamerEntity.java @@ -29,6 +29,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.util.HashMap; +import java.util.Locale; import java.util.Map; import java.util.Set; import java.util.zip.GZIPOutputStream; @@ -297,7 +298,7 @@ static byte[] escape(String string) { for (int zero = 0; zero < intLength; zero++) { BUILDER.append('0'); } - BUILDER.append(intString.toUpperCase()); + BUILDER.append(intString.toUpperCase(Locale.US)); } else { BUILDER.append(ch); } diff --git a/library/src/main/java/com/loopj/android/http/PersistentCookieStore.java b/library/src/main/java/com/loopj/android/http/PersistentCookieStore.java index d7766910e..ee87bd239 100644 --- a/library/src/main/java/com/loopj/android/http/PersistentCookieStore.java +++ b/library/src/main/java/com/loopj/android/http/PersistentCookieStore.java @@ -33,6 +33,7 @@ import java.util.ArrayList; import java.util.Date; import java.util.List; +import java.util.Locale; import java.util.concurrent.ConcurrentHashMap; /** @@ -201,7 +202,7 @@ protected String byteArrayToHexString(byte[] bytes) { } sb.append(Integer.toHexString(v)); } - return sb.toString().toUpperCase(); + return sb.toString().toUpperCase(Locale.US); } /**