Skip to content

Commit 94fcd32

Browse files
committed
Code formatting and cleanup, javadoc formatting, default locale for string formatting, consuming entity/inputstream
1 parent a8663ff commit 94fcd32

14 files changed

+61
-230
lines changed

library/src/main/java/com/loopj/android/http/base64/Base64.java

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -33,31 +33,31 @@ public class Base64 {
3333
/**
3434
* Encoder flag bit to omit the padding '=' characters at the end of the output (if any).
3535
*/
36-
public static final int NO_PADDING = 1;
36+
private static final int NO_PADDING = 1;
3737

3838
/**
3939
* Encoder flag bit to omit all line terminators (i.e., the output will be on one long line).
4040
*/
41-
public static final int NO_WRAP = 2;
41+
private static final int NO_WRAP = 2;
4242

4343
/**
4444
* Encoder flag bit to indicate lines should be terminated with a CRLF pair instead of just an
4545
* LF. Has no effect if {@code NO_WRAP} is specified as well.
4646
*/
47-
public static final int CRLF = 4;
47+
private static final int CRLF = 4;
4848

4949
/**
5050
* Encoder/decoder flag bit to indicate using the "URL and filename safe" variant of Base64 (see
5151
* RFC 3548 section 4) where {@code -} and {@code _} are used in place of {@code +} and {@code
5252
* /}.
5353
*/
54-
public static final int URL_SAFE = 8;
54+
private static final int URL_SAFE = 8;
5555

5656
/**
5757
* Flag to pass to {@link Base64OutputStream} to indicate that it should not close the output
5858
* stream it is wrapping when it itself is closed.
5959
*/
60-
public static final int NO_CLOSE = 16;
60+
static final int NO_CLOSE = 16;
6161

6262
// --------------------------------------------------------
6363
// shared code
@@ -80,7 +80,7 @@ private Base64() {
8080
* @param flags controls certain features of the decoded output. Pass {@code DEFAULT} to decode
8181
* standard Base64.
8282
* @return decoded bytes
83-
* @throws IllegalArgumentException if the input contains incorrect padding
83+
* @throws java.lang.IllegalArgumentException if the input contains incorrect padding
8484
*/
8585
public static byte[] decode(String str, int flags) {
8686
return decode(str.getBytes(), flags);
@@ -95,31 +95,30 @@ public static byte[] decode(String str, int flags) {
9595
* @param flags controls certain features of the decoded output. Pass {@code DEFAULT} to decode
9696
* standard Base64.
9797
* @return decoded bytes
98-
* @throws IllegalArgumentException if the input contains incorrect padding
98+
* @throws java.lang.IllegalArgumentException if the input contains incorrect padding
9999
*/
100-
public static byte[] decode(byte[] input, int flags) {
101-
return decode(input, 0, input.length, flags);
100+
private static byte[] decode(byte[] input, int flags) {
101+
return decode(input, input.length, flags);
102102
}
103103

104104
/**
105105
* Decode the Base64-encoded data in input and return the data in a new byte array.
106106
* <p>&nbsp;</p> <p>The padding '=' characters at the end are considered optional, but if any
107107
* are present, there must be the correct number of them.
108108
*
109-
* @param input the data to decode
110-
* @param offset the position within the input array at which to start
111-
* @param len the number of bytes of input to decode
112-
* @param flags controls certain features of the decoded output. Pass {@code DEFAULT} to decode
113-
* standard Base64.
109+
* @param input the data to decode
110+
* @param len the number of bytes of input to decode
111+
* @param flags controls certain features of the decoded output. Pass {@code DEFAULT} to decode
112+
* standard Base64.
114113
* @return decoded bytes for given offset and length
115-
* @throws IllegalArgumentException if the input contains incorrect padding
114+
* @throws java.lang.IllegalArgumentException if the input contains incorrect padding
116115
*/
117-
public static byte[] decode(byte[] input, int offset, int len, int flags) {
116+
private static byte[] decode(byte[] input, int len, int flags) {
118117
// Allocate space for the most data the input could represent.
119118
// (It could contain less if it contains whitespace, etc.)
120119
Decoder decoder = new Decoder(flags, new byte[len * 3 / 4]);
121120

122-
if (!decoder.process(input, offset, len, true)) {
121+
if (!decoder.process(input, 0, len, true)) {
123122
throw new IllegalArgumentException("bad base-64");
124123
}
125124

@@ -183,7 +182,7 @@ public static String encodeToString(byte[] input, int offset, int len, int flags
183182
* in output that adheres to RFC 2045.
184183
* @return base64 encoded input as bytes
185184
*/
186-
public static byte[] encode(byte[] input, int flags) {
185+
private static byte[] encode(byte[] input, int flags) {
187186
return encode(input, 0, input.length, flags);
188187
}
189188

@@ -197,7 +196,7 @@ public static byte[] encode(byte[] input, int flags) {
197196
* results in output that adheres to RFC 2045.
198197
* @return base64 encoded input as bytes
199198
*/
200-
public static byte[] encode(byte[] input, int offset, int len, int flags) {
199+
private static byte[] encode(byte[] input, int offset, int len, int flags) {
201200
Encoder encoder = new Encoder(flags, null);
202201

203202
// Compute the exact length of the array we will produce.
@@ -238,8 +237,8 @@ public static byte[] encode(byte[] input, int offset, int len, int flags) {
238237
}
239238

240239
/* package */ static abstract class Coder {
241-
public byte[] output;
242-
public int op;
240+
byte[] output;
241+
int op;
243242

244243
/**
245244
* Encode/decode another block of input data. this.output is provided by the caller, and
@@ -264,7 +263,7 @@ public static byte[] encode(byte[] input, int offset, int len, int flags) {
264263
/**
265264
* Lookup table for turning bytes into their position in the Base64 alphabet.
266265
*/
267-
private static final int DECODE[] = {
266+
private static final int[] DECODE = {
268267
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
269268
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
270269
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
@@ -287,7 +286,7 @@ public static byte[] encode(byte[] input, int offset, int len, int flags) {
287286
* Decode lookup table for the "web safe" variant (RFC 3548 sec. 4) where - and _ replace +
288287
* and /.
289288
*/
290-
private static final int DECODE_WEBSAFE[] = {
289+
private static final int[] DECODE_WEBSAFE = {
291290
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
292291
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
293292
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1,
@@ -321,7 +320,7 @@ public static byte[] encode(byte[] input, int offset, int len, int flags) {
321320
private int state; // state number (0 to 6)
322321
private int value;
323322

324-
public Decoder(int flags, byte[] output) {
323+
Decoder(int flags, byte[] output) {
325324
this.output = output;
326325

327326
alphabet = ((flags & URL_SAFE) == 0) ? DECODE : DECODE_WEBSAFE;
@@ -526,12 +525,12 @@ public boolean process(byte[] input, int offset, int len, boolean finish) {
526525
* (the maximum allowable according to <a href="https://www.ietf.org/rfc/rfc2045.txt">RFC
527526
* 2045</a>).
528527
*/
529-
public static final int LINE_GROUPS = 19;
528+
static final int LINE_GROUPS = 19;
530529

531530
/**
532531
* Lookup table for turning Base64 alphabet positions (6 bits) into output bytes.
533532
*/
534-
private static final byte ENCODE[] = {
533+
private static final byte[] ENCODE = {
535534
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
536535
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
537536
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
@@ -541,21 +540,21 @@ public boolean process(byte[] input, int offset, int len, boolean finish) {
541540
/**
542541
* Lookup table for turning Base64 alphabet positions (6 bits) into output bytes.
543542
*/
544-
private static final byte ENCODE_WEBSAFE[] = {
543+
private static final byte[] ENCODE_WEBSAFE = {
545544
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
546545
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
547546
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
548547
'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_',
549548
};
550-
final public boolean do_padding;
551-
final public boolean do_newline;
552-
final public boolean do_cr;
549+
final boolean do_padding;
550+
final boolean do_newline;
551+
final boolean do_cr;
553552
final private byte[] tail;
554553
final private byte[] alphabet;
555554
/* package */ int tailLen;
556555
private int count;
557556

558-
public Encoder(int flags, byte[] output) {
557+
Encoder(int flags, byte[] output) {
559558
this.output = output;
560559

561560
do_padding = (flags & NO_PADDING) == 0;

library/src/main/java/com/loopj/android/http/handlers/AsyncHttpResponseHandler.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@
3030
import java.io.InputStream;
3131
import java.lang.ref.WeakReference;
3232
import java.net.URI;
33+
import java.util.Locale;
3334

3435
import cz.msebera.android.httpclient.Header;
3536
import cz.msebera.android.httpclient.HttpEntity;
3637
import cz.msebera.android.httpclient.HttpResponse;
3738
import cz.msebera.android.httpclient.StatusLine;
3839
import cz.msebera.android.httpclient.client.HttpResponseException;
3940
import cz.msebera.android.httpclient.util.ByteArrayBuffer;
41+
import cz.msebera.android.httpclient.util.EntityUtils;
4042

4143
/**
4244
* Used to intercept and handle the responses from requests made using {@link AsyncHttpClient}. The
@@ -82,7 +84,6 @@
8284
* });
8385
* </pre>
8486
*/
85-
@SuppressWarnings("ALL")
8687
public abstract class AsyncHttpResponseHandler implements ResponseHandlerInterface {
8788

8889
public static final String DEFAULT_CHARSET = "UTF-8";
@@ -243,7 +244,7 @@ public void setCharset(final String charset) {
243244
* @param totalSize total size of file
244245
*/
245246
public void onProgress(long bytesWritten, long totalSize) {
246-
AsyncHttpClient.log.v(LOG_TAG, String.format("Progress %d from %d (%2.0f%%)", bytesWritten, totalSize, (totalSize > 0) ? (bytesWritten * 1.0 / totalSize) * 100 : -1));
247+
AsyncHttpClient.log.v(LOG_TAG, String.format(Locale.getDefault(), "Progress %d from %d (%2.0f%%)", bytesWritten, totalSize, (totalSize > 0) ? (bytesWritten * 1.0 / totalSize) * 100 : -1));
247248
}
248249

249250
/**
@@ -296,7 +297,7 @@ public void onPostProcessResponse(ResponseHandlerInterface instance, HttpRespons
296297
* @param retryNo number of retry
297298
*/
298299
public void onRetry(int retryNo) {
299-
AsyncHttpClient.log.d(LOG_TAG, String.format("Request retry no. %d", retryNo));
300+
AsyncHttpClient.log.d(LOG_TAG, String.format(Locale.getDefault(), "Request retry no. %d", retryNo));
300301
}
301302

302303
public void onCancel() {
@@ -485,8 +486,7 @@ byte[] getResponseData(HttpEntity entity) throws IOException {
485486
sendProgressMessage(count, (contentLength <= 0 ? 1 : contentLength));
486487
}
487488
} finally {
488-
// AsyncHttpClient.silentCloseInputStream(instream);
489-
// AsyncHttpClient.endEntityViaReflection(entity);
489+
EntityUtils.consumeQuietly(entity);
490490
}
491491
responseBody = buffer.toByteArray();
492492
} catch (OutOfMemoryError e) {

library/src/main/java/com/loopj/android/http/handlers/BinaryHttpResponseHandler.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import android.os.Looper;
2222

2323
import com.loopj.android.http.AsyncHttpClient;
24-
import com.loopj.android.http.RequestParams;
2524

2625
import java.io.IOException;
2726
import java.util.regex.Pattern;

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

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727

2828
import cz.msebera.android.httpclient.HttpEntity;
2929
import cz.msebera.android.httpclient.util.ByteArrayBuffer;
30+
import cz.msebera.android.httpclient.util.EntityUtils;
3031

31-
@SuppressWarnings("ALL")
3232
public abstract class DataAsyncHttpResponseHandler extends AsyncHttpResponseHandler {
3333
protected static final int PROGRESS_DATA_MESSAGE = 7;
3434
private static final String LOG_TAG = "DataAsyncHttpRH";
@@ -89,19 +89,17 @@ protected void handleMessage(Message message) {
8989
super.handleMessage(message);
9090
Object[] response;
9191

92-
switch (message.what) {
93-
case PROGRESS_DATA_MESSAGE:
94-
response = (Object[]) message.obj;
95-
if (response != null && response.length >= 1) {
96-
try {
97-
onProgressData((byte[]) response[0]);
98-
} catch (Throwable t) {
99-
AsyncHttpClient.log.e(LOG_TAG, "custom onProgressData contains an error", t);
100-
}
101-
} else {
102-
AsyncHttpClient.log.e(LOG_TAG, "PROGRESS_DATA_MESSAGE didn't got enough params");
92+
if (message.what == PROGRESS_DATA_MESSAGE) {
93+
response = (Object[]) message.obj;
94+
if (response != null && response.length >= 1) {
95+
try {
96+
onProgressData((byte[]) response[0]);
97+
} catch (Throwable t) {
98+
AsyncHttpClient.log.e(LOG_TAG, "custom onProgressData contains an error", t);
10399
}
104-
break;
100+
} else {
101+
AsyncHttpClient.log.e(LOG_TAG, "PROGRESS_DATA_MESSAGE didn't got enough params");
102+
}
105103
}
106104
}
107105

@@ -138,7 +136,7 @@ byte[] getResponseData(HttpEntity entity) throws IOException {
138136
sendProgressMessage(count, contentLength);
139137
}
140138
} finally {
141-
// AsyncHttpClient.silentCloseInputStream(instream);
139+
EntityUtils.consumeQuietly(entity);
142140
}
143141
responseBody = buffer.toByteArray();
144142
} catch (OutOfMemoryError e) {

library/src/main/java/com/loopj/android/http/handlers/FileAsyncHttpResponseHandler.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
import cz.msebera.android.httpclient.Header;
3232
import cz.msebera.android.httpclient.HttpEntity;
33+
import cz.msebera.android.httpclient.util.EntityUtils;
3334

3435
public abstract class FileAsyncHttpResponseHandler extends AsyncHttpResponseHandler {
3536

@@ -165,14 +166,14 @@ protected File getTargetFileByParsingURL() {
165166
Utils.asserts(getOriginalFile().isDirectory(), "Target file is not a directory, cannot proceed");
166167
Utils.asserts(getRequestURI() != null, "RequestURI is null, cannot proceed");
167168
String requestURL = getRequestURI().toString();
168-
String filename = requestURL.substring(requestURL.lastIndexOf('/') + 1, requestURL.length());
169+
String filename = requestURL.substring(requestURL.lastIndexOf('/') + 1);
169170
File targetFileRtn = new File(getOriginalFile(), filename);
170171
if (targetFileRtn.exists() && renameIfExists) {
171172
String format;
172173
if (!filename.contains(".")) {
173174
format = filename + " (%d)";
174175
} else {
175-
format = filename.substring(0, filename.lastIndexOf('.')) + " (%d)" + filename.substring(filename.lastIndexOf('.'), filename.length());
176+
format = filename.substring(0, filename.lastIndexOf('.')) + " (%d)" + filename.substring(filename.lastIndexOf('.'));
176177
}
177178
int index = 0;
178179
while (true) {
@@ -232,9 +233,8 @@ protected byte[] getResponseData(HttpEntity entity) throws IOException {
232233
sendProgressMessage(count, contentLength);
233234
}
234235
} finally {
235-
// AsyncHttpClient.silentCloseInputStream(instream);
236236
buffer.flush();
237-
// AsyncHttpClient.silentCloseOutputStream(buffer);
237+
EntityUtils.consumeQuietly(entity);
238238
}
239239
}
240240
}

library/src/main/java/com/loopj/android/http/handlers/SaxAsyncHttpResponseHandler.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
import cz.msebera.android.httpclient.Header;
3737
import cz.msebera.android.httpclient.HttpEntity;
38+
import cz.msebera.android.httpclient.util.EntityUtils;
3839

3940
/**
4041
* Provides interface to deserialize SAX responses, using AsyncHttpResponseHandler. Can be used like
@@ -65,7 +66,7 @@ public abstract class SaxAsyncHttpResponseHandler<T extends DefaultHandler> exte
6566
/**
6667
* Generic Type of handler
6768
*/
68-
private T handler = null;
69+
private T handler;
6970

7071
/**
7172
* Constructs new SaxAsyncHttpResponseHandler with given handler instance
@@ -107,12 +108,12 @@ protected byte[] getResponseData(HttpEntity entity) throws IOException {
107108
} catch (ParserConfigurationException e) {
108109
AsyncHttpClient.log.e(LOG_TAG, "getResponseData exception", e);
109110
} finally {
110-
// AsyncHttpClient.silentCloseInputStream(instream);
111111
if (inputStreamReader != null) {
112112
try {
113113
inputStreamReader.close();
114114
} catch (IOException e) { /*ignore*/ }
115115
}
116+
EntityUtils.consumeQuietly(entity);
116117
}
117118
}
118119
}

library/src/main/java/com/loopj/android/http/interfaces/AsyncHttpClientInterface.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public interface AsyncHttpClientInterface {
3333
ExecutorService getThreadPool();
3434

3535
RequestHandle sendRequest(CloseableHttpClient httpClient, HttpUriRequest request, ResponseHandlerInterface responseHandler);
36+
3637
RequestHandle sendRequest(RequestInterface request, ResponseHandlerInterface responseHandler);
3738

3839
boolean isLoggingEnabled();

library/src/main/java/com/loopj/android/http/interfaces/RequestParamInterface.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
public interface RequestParamInterface<T> {
2121

2222
String getName();
23+
2324
T getValue();
25+
2426
ContentType getContentType();
2527

2628
}

library/src/main/java/com/loopj/android/http/interfaces/ResponseInterface.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@
2020
public interface ResponseInterface<T> {
2121

2222
boolean isSuccess();
23+
2324
int getStatusCode();
25+
2426
Header[] getHeaders();
27+
2528
T getResponse();
29+
2630
Throwable getThrowable();
2731

2832
}

0 commit comments

Comments
 (0)