Skip to content

Commit 5b1224a

Browse files
GijsWeteringsfacebook-github-bot
authored andcommitted
[skip ci] Fix Nullsafe FIXMES for BundleDownloader.java (#50066)
Summary: Gone trough all the FIXMEs added in the previous diff by the nullsafe tool, marked the class as nullsafe and ensured no remaining violations. Changelog: [Android][Fixed] Made BundleDownloader.java nullsafe Reviewed By: alanleedev Differential Revision: D71126383
1 parent 6368e9f commit 5b1224a

File tree

1 file changed

+31
-18
lines changed

1 file changed

+31
-18
lines changed

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/BundleDownloader.java

+31-18
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import androidx.annotation.Nullable;
1111
import com.facebook.common.logging.FLog;
1212
import com.facebook.infer.annotation.Assertions;
13+
import com.facebook.infer.annotation.Nullsafe;
1314
import com.facebook.react.common.DebugServerException;
1415
import com.facebook.react.common.ReactConstants;
1516
import com.facebook.react.devsupport.interfaces.DevBundleDownloadListener;
@@ -32,6 +33,7 @@
3233
import org.json.JSONException;
3334
import org.json.JSONObject;
3435

36+
@Nullsafe(Nullsafe.Mode.LOCAL)
3537
public class BundleDownloader {
3638
private static final String TAG = "BundleDownloader";
3739

@@ -140,26 +142,29 @@ public void onResponse(Call call, final Response response) throws IOException {
140142
final String url = response.request().url().toString();
141143
// Make sure the result is a multipart response and parse the boundary.
142144
String contentType = response.header("content-type");
145+
if (contentType == null) {
146+
// fallback to empty string for nullability
147+
contentType = "";
148+
}
143149
Pattern regex = Pattern.compile("multipart/mixed;.*boundary=\"([^\"]+)\"");
144-
// NULLSAFE_FIXME[Parameter Not Nullable]
145150
Matcher match = regex.matcher(contentType);
146-
if (match.find()) {
147-
// NULLSAFE_FIXME[Parameter Not Nullable]
148-
processMultipartResponse(url, r, match.group(1), outputFile, bundleInfo, callback);
151+
if (!contentType.isEmpty() && match.find()) {
152+
String boundary = Assertions.assertNotNull(match.group(1));
153+
processMultipartResponse(url, r, boundary, outputFile, bundleInfo, callback);
149154
} else {
150155
// In case the server doesn't support multipart/mixed responses, fallback to normal
151156
// download.
152157
try (ResponseBody body = r.body()) {
153-
processBundleResult(
154-
url,
155-
r.code(),
156-
r.headers(),
157-
// NULLSAFE_FIXME[Nullable Dereference]
158-
r.body().source(),
159-
outputFile,
160-
// NULLSAFE_FIXME[Parameter Not Nullable]
161-
bundleInfo,
162-
callback);
158+
if (body != null) {
159+
processBundleResult(
160+
url,
161+
r.code(),
162+
r.headers(),
163+
body.source(),
164+
outputFile,
165+
bundleInfo,
166+
callback);
167+
}
163168
}
164169
}
165170
}
@@ -175,9 +180,18 @@ private void processMultipartResponse(
175180
@Nullable final BundleInfo bundleInfo,
176181
final DevBundleDownloadListener callback)
177182
throws IOException {
178-
183+
if (response.body() == null) {
184+
callback.onFailure(
185+
new DebugServerException(
186+
"Error while reading multipart response.\n\nResponse body was empty: "
187+
+ response.code()
188+
+ "\n\n"
189+
+ "URL: "
190+
+ url.toString()
191+
+ "\n\n"));
192+
return;
193+
}
179194
MultipartStreamReader bodyReader =
180-
// NULLSAFE_FIXME[Nullable Dereference]
181195
new MultipartStreamReader(response.body().source(), boundary);
182196
boolean completed =
183197
bodyReader.readAllParts(
@@ -197,7 +211,6 @@ public void onChunkComplete(
197211
status = Integer.parseInt(headers.get("X-Http-Status"));
198212
}
199213
processBundleResult(
200-
// NULLSAFE_FIXME[Parameter Not Nullable]
201214
url, status, Headers.of(headers), body, outputFile, bundleInfo, callback);
202215
} else {
203216
if (!headers.containsKey("Content-Type")
@@ -249,7 +262,7 @@ private void processBundleResult(
249262
Headers headers,
250263
BufferedSource body,
251264
File outputFile,
252-
BundleInfo bundleInfo,
265+
@Nullable BundleInfo bundleInfo,
253266
DevBundleDownloadListener callback)
254267
throws IOException {
255268
// Check for server errors. If the server error has the expected form, fail with more info.

0 commit comments

Comments
 (0)