Skip to content

Commit 69c0367

Browse files
author
Ron Radtke
committed
bit of clean up
1 parent 7bb60a1 commit 69c0367

File tree

6 files changed

+37
-39
lines changed

6 files changed

+37
-39
lines changed

android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtil.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import android.net.Uri;
88
import android.os.Build;
99

10+
import androidx.annotation.NonNull;
1011
import androidx.core.content.FileProvider;
1112

1213
import android.util.SparseArray;
@@ -81,6 +82,7 @@ public void onNewIntent(Intent intent) {
8182
});
8283
}
8384

85+
@NonNull
8486
@Override
8587
public String getName() {
8688
return "ReactNativeBlobUtil";

android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilBody.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package com.ReactNativeBlobUtil;
22

3-
import androidx.annotation.NonNull;
4-
3+
import android.net.Uri;
54
import android.util.Base64;
65

6+
import androidx.annotation.NonNull;
7+
78
import com.facebook.react.bridge.Arguments;
89
import com.facebook.react.bridge.ReactApplicationContext;
910
import com.facebook.react.bridge.ReadableArray;
@@ -19,8 +20,6 @@
1920
import java.io.InputStream;
2021
import java.util.ArrayList;
2122

22-
import android.net.Uri;
23-
2423
import okhttp3.MediaType;
2524
import okhttp3.RequestBody;
2625
import okio.BufferedSink;
@@ -187,7 +186,7 @@ private InputStream getRequestStream() throws Exception {
187186
* Create a temp file that contains content of multipart form data content
188187
*
189188
* @return The cache file object
190-
* @throws IOException
189+
* @throws IOException .
191190
*/
192191
private File createMultipartBodyCache() throws IOException {
193192
String boundary = "ReactNativeBlobUtil-" + mTaskId;
@@ -282,7 +281,7 @@ private File createMultipartBodyCache() throws IOException {
282281
*
283282
* @param stream The input stream
284283
* @param sink The request body buffer sink
285-
* @throws IOException
284+
* @throws IOException .
286285
*/
287286
private void pipeStreamToSink(InputStream stream, BufferedSink sink) throws IOException {
288287
byte[] chunk = new byte[10240];

android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilConfig.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ class ReactNativeBlobUtilConfig {
2323
ReactNativeBlobUtilConfig(ReadableMap options) {
2424
if (options == null)
2525
return;
26-
this.fileCache = options.hasKey("fileCache") ? options.getBoolean("fileCache") : false;
26+
this.fileCache = options.hasKey("fileCache") && options.getBoolean("fileCache");
2727
this.path = options.hasKey("path") ? options.getString("path") : null;
2828
this.appendExt = options.hasKey("appendExt") ? options.getString("appendExt") : "";
29-
this.trusty = options.hasKey("trusty") ? options.getBoolean("trusty") : false;
30-
this.wifiOnly = options.hasKey("wifiOnly") ? options.getBoolean("wifiOnly") : false;
29+
this.trusty = options.hasKey("trusty") && options.getBoolean("trusty");
30+
this.wifiOnly = options.hasKey("wifiOnly") && options.getBoolean("wifiOnly");
3131
if (options.hasKey("addAndroidDownloads")) {
3232
this.addAndroidDownloads = options.getMap("addAndroidDownloads");
3333
}
@@ -43,8 +43,8 @@ class ReactNativeBlobUtilConfig {
4343
}
4444
this.key = options.hasKey("key") ? options.getString("key") : null;
4545
this.mime = options.hasKey("contentType") ? options.getString("contentType") : null;
46-
this.increment = options.hasKey("increment") ? options.getBoolean("increment") : false;
47-
this.auto = options.hasKey("auto") ? options.getBoolean("auto") : false;
46+
this.increment = options.hasKey("increment") && options.getBoolean("increment");
47+
this.auto = options.hasKey("auto") && options.getBoolean("auto");
4848
if (options.hasKey("timeout")) {
4949
this.timeout = options.getInt("timeout");
5050
}

android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilFS.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -218,14 +218,11 @@ else if (resolved == null) {
218218
case "ascii":
219219
WritableArray asciiResult = Arguments.createArray();
220220
for (byte b : bytes) {
221-
asciiResult.pushInt((int) b);
221+
asciiResult.pushInt(b);
222222
}
223223
promise.resolve(asciiResult);
224224
break;
225-
case "utf8":
226-
promise.resolve(new String(bytes));
227-
break;
228-
default:
225+
default: // also used for utf-8
229226
promise.resolve(new String(bytes));
230227
break;
231228
}
@@ -361,7 +358,7 @@ else if (resolved == null) {
361358
}
362359

363360
byte[] buffer = new byte[chunkSize];
364-
int cursor = 0;
361+
int cursor;
365362
boolean error = false;
366363

367364
if (encoding.equalsIgnoreCase("utf8")) {
@@ -377,7 +374,7 @@ else if (resolved == null) {
377374
while ((cursor = fs.read(buffer)) != -1) {
378375
WritableArray chunk = Arguments.createArray();
379376
for (int i = 0; i < cursor; i++) {
380-
chunk.pushInt((int) buffer[i]);
377+
chunk.pushInt(buffer[i]);
381378
}
382379
emitStreamEvent(streamId, "data", chunk);
383380
if (tick > 0)
@@ -407,7 +404,6 @@ else if (resolved == null) {
407404
if (!error)
408405
emitStreamEvent(streamId, "end", "");
409406
fs.close();
410-
buffer = null;
411407
} catch (FileNotFoundException err) {
412408
emitStreamEvent(
413409
streamId,
@@ -475,6 +471,7 @@ void writeStream(String path, String encoding, boolean append, Callback callback
475471
*/
476472
static void writeChunk(String streamId, String data, Callback callback) {
477473
ReactNativeBlobUtilFS fs = fileStreams.get(streamId);
474+
assert fs != null;
478475
OutputStream stream = fs.writeStreamInstance;
479476
byte[] chunk = ReactNativeBlobUtilFS.stringToBytes(data, fs.encoding);
480477
try {
@@ -495,6 +492,7 @@ static void writeChunk(String streamId, String data, Callback callback) {
495492
static void writeArrayChunk(String streamId, ReadableArray data, Callback callback) {
496493
try {
497494
ReactNativeBlobUtilFS fs = fileStreams.get(streamId);
495+
assert fs != null;
498496
OutputStream stream = fs.writeStreamInstance;
499497
byte[] chunk = new byte[data.size()];
500498
for (int i = 0; i < data.size(); i++) {
@@ -516,6 +514,7 @@ static void writeArrayChunk(String streamId, ReadableArray data, Callback callba
516514
static void closeStream(String streamId, Callback callback) {
517515
try {
518516
ReactNativeBlobUtilFS fs = fileStreams.get(streamId);
517+
assert fs != null;
519518
OutputStream stream = fs.writeStreamInstance;
520519
fileStreams.remove(streamId);
521520
stream.close();
@@ -633,7 +632,7 @@ static void cp(String path, String dest, Callback callback) {
633632
}
634633
// Only call the callback once to prevent the app from crashing
635634
// with an 'Illegal callback invocation from native module' exception.
636-
if (message != "") {
635+
if (!message.equals("")) {
637636
callback.invoke(message);
638637
} else {
639638
callback.invoke();
@@ -711,7 +710,7 @@ static void exists(String path, Callback callback) {
711710
* List content of folder
712711
*
713712
* @param path Target folder
714-
* @param callback JS context callback
713+
* @param promise JS promise
715714
*/
716715
static void ls(String path, Promise promise) {
717716
try {
@@ -778,7 +777,7 @@ static void slice(String path, String dest, int start, int end, String encode, P
778777
if (read <= 0) {
779778
break;
780779
}
781-
out.write(buffer, 0, (int) Math.min(remain, read));
780+
out.write(buffer, 0, Math.min(remain, read));
782781
now += read;
783782
}
784783
in.close();

android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilPackage.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.ReactNativeBlobUtil;
22

3+
import androidx.annotation.NonNull;
4+
35
import com.facebook.react.ReactPackage;
46
import com.facebook.react.bridge.JavaScriptModule;
57
import com.facebook.react.bridge.NativeModule;
@@ -13,8 +15,9 @@
1315

1416
public class ReactNativeBlobUtilPackage implements ReactPackage {
1517

18+
@NonNull
1619
@Override
17-
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
20+
public List<NativeModule> createNativeModules(@NonNull ReactApplicationContext reactContext) {
1821
List<NativeModule> modules = new ArrayList<>();
1922
modules.add(new ReactNativeBlobUtil(reactContext));
2023
return modules;
@@ -24,8 +27,9 @@ public List<Class<? extends JavaScriptModule>> createJSModules() {
2427
return Collections.emptyList();
2528
}
2629

30+
@NonNull
2731
@Override
28-
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
32+
public List<ViewManager> createViewManagers(@NonNull ReactApplicationContext reactContext) {
2933
return Collections.emptyList();
3034
}
3135

android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilReq.java

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,33 +30,25 @@
3030
import com.facebook.react.bridge.WritableMap;
3131
import com.facebook.react.modules.core.DeviceEventManagerModule;
3232

33-
import javax.net.ssl.TrustManagerFactory;
34-
import javax.net.ssl.TrustManager;
35-
import javax.net.ssl.X509TrustManager;
36-
import javax.net.ssl.SSLContext;
37-
3833
import java.io.File;
3934
import java.io.FileOutputStream;
4035
import java.io.IOException;
4136
import java.io.InputStream;
4237
import java.net.MalformedURLException;
38+
import java.net.Proxy;
4339
import java.net.SocketException;
4440
import java.net.SocketTimeoutException;
4541
import java.net.URL;
46-
import java.net.Proxy;
4742
import java.nio.ByteBuffer;
4843
import java.nio.charset.CharacterCodingException;
4944
import java.nio.charset.Charset;
5045
import java.nio.charset.CharsetDecoder;
51-
import java.security.KeyStore;
5246
import java.util.ArrayList;
53-
import java.util.Arrays;
54-
import java.util.List;
5547
import java.util.HashMap;
56-
48+
import java.util.List;
5749
import java.util.concurrent.TimeUnit;
5850

59-
import javax.net.ssl.SSLSocketFactory;
51+
import javax.net.ssl.SSLContext;
6052

6153
import okhttp3.Call;
6254
import okhttp3.ConnectionPool;
@@ -196,7 +188,7 @@ public void run() {
196188
req.addRequestHeader(key, headers.getString(key));
197189
}
198190
// Attempt to add cookie, if it exists
199-
URL urlObj = null;
191+
URL urlObj;
200192
try {
201193
urlObj = new URL(url);
202194
String baseUrl = urlObj.getProtocol() + "://" + urlObj.getHost();
@@ -388,14 +380,16 @@ else if (value.equalsIgnoreCase("utf8"))
388380
// #156 fix cookie issue
389381
final Request req = builder.build();
390382
clientBuilder.addNetworkInterceptor(new Interceptor() {
383+
@NonNull
391384
@Override
392-
public Response intercept(Chain chain) throws IOException {
385+
public Response intercept(@NonNull Chain chain) throws IOException {
393386
redirects.add(chain.request().url().toString());
394387
return chain.proceed(chain.request());
395388
}
396389
});
397390
// Add request interceptor for upload progress event
398391
clientBuilder.addInterceptor(new Interceptor() {
392+
@NonNull
399393
@Override
400394
public Response intercept(@NonNull Chain chain) throws IOException {
401395
try {
@@ -431,7 +425,7 @@ public Response intercept(@NonNull Chain chain) throws IOException {
431425
} catch (SocketTimeoutException e) {
432426
timeout = true;
433427
//ReactNativeBlobUtilUtils.emitWarningEvent("ReactNativeBlobUtil error when sending request : " + e.getLocalizedMessage());
434-
} catch (Exception ex) {
428+
} catch (Exception ignored) {
435429

436430
}
437431
return chain.proceed(chain.request());
@@ -457,7 +451,7 @@ public Response intercept(@NonNull Chain chain) throws IOException {
457451
call.enqueue(new okhttp3.Callback() {
458452

459453
@Override
460-
public void onFailure(@NonNull Call call, IOException e) {
454+
public void onFailure(@NonNull Call call, @NonNull IOException e) {
461455
cancelTask(taskId);
462456
if (respInfo == null) {
463457
respInfo = Arguments.createMap();

0 commit comments

Comments
 (0)