Skip to content

Commit

Permalink
Fix saving empty json files
Browse files Browse the repository at this point in the history
  • Loading branch information
Chumva committed Feb 26, 2025
1 parent 456531b commit 1ca0d25
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,15 @@ public void writeToStream(@NonNull OutputStream outputStream, @Nullable IProgres
SettingsHelper.LOG.error("Failed to write preference: " + pref.getId(), e);
}
}
if (json.length() > 0) {
try {
int bytesDivisor = 1024;
byte[] bytes = json.toString(2).getBytes("UTF-8");
if (progress != null) {
progress.startWork(bytes.length / bytesDivisor);
}
Algorithms.streamCopy(new ByteArrayInputStream(bytes), outputStream, progress, bytesDivisor);
} catch (JSONException e) {
SettingsHelper.LOG.error("Failed to write json to stream", e);
try {
int bytesDivisor = 1024;
byte[] bytes = json.toString(2).getBytes("UTF-8");
if (progress != null) {
progress.startWork(bytes.length / bytesDivisor);
}
Algorithms.streamCopy(new ByteArrayInputStream(bytes), outputStream, progress, bytesDivisor);
} catch (JSONException e) {
SettingsHelper.LOG.error("Failed to write json to stream", e);
}
if (progress != null) {
progress.finishTask();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,18 +244,16 @@ protected SettingsItemWriter<? extends SettingsItem> getJsonWriter() {
@Override
public void writeToStream(@NonNull OutputStream outputStream, @Nullable IProgress progress) throws IOException {
JSONObject json = writeItemsToJson(new JSONObject());
if (json.length() > 0) {
try {
int bytesDivisor = 1024;
byte[] bytes = json.toString(2).getBytes("UTF-8");
if (progress != null) {
progress.startWork(bytes.length / bytesDivisor);
}
Algorithms.streamCopy(new ByteArrayInputStream(bytes), outputStream, progress, bytesDivisor);
} catch (JSONException e) {
warnings.add(app.getString(R.string.settings_item_write_error, String.valueOf(getType())));
SettingsHelper.LOG.error("Failed to write json to stream", e);
try {
int bytesDivisor = 1024;
byte[] bytes = json.toString(2).getBytes("UTF-8");
if (progress != null) {
progress.startWork(bytes.length / bytesDivisor);
}
Algorithms.streamCopy(new ByteArrayInputStream(bytes), outputStream, progress, bytesDivisor);
} catch (JSONException e) {
warnings.add(app.getString(R.string.settings_item_write_error, String.valueOf(getType())));
SettingsHelper.LOG.error("Failed to write json to stream", e);
}
if (progress != null) {
progress.finishTask();
Expand Down

0 comments on commit 1ca0d25

Please sign in to comment.