From b21981a9c714255e52766263be72534275e04781 Mon Sep 17 00:00:00 2001
From: Stypox <stypox@pm.me>
Date: Wed, 27 Nov 2024 16:34:50 +0100
Subject: [PATCH] Add comments to explain why openAndTruncateStream()

---
 .../subscription/services/SubscriptionsExportService.java      | 3 +++
 .../org/schabi/newpipe/settings/export/ImportExportManager.kt  | 2 ++
 2 files changed, 5 insertions(+)

diff --git a/app/src/main/java/org/schabi/newpipe/local/subscription/services/SubscriptionsExportService.java b/app/src/main/java/org/schabi/newpipe/local/subscription/services/SubscriptionsExportService.java
index 641f044bf..ab1a5a10c 100644
--- a/app/src/main/java/org/schabi/newpipe/local/subscription/services/SubscriptionsExportService.java
+++ b/app/src/main/java/org/schabi/newpipe/local/subscription/services/SubscriptionsExportService.java
@@ -76,6 +76,9 @@ public class SubscriptionsExportService extends BaseImportExportService {
 
         try {
             outFile = new StoredFileHelper(this, path, "application/json");
+            // truncate the file before writing to it, otherwise if the new content is smaller than
+            // the previous file size, the file will retain part of the previous content and be
+            // corrupted
             outputStream = new SharpOutputStream(outFile.openAndTruncateStream());
         } catch (final IOException e) {
             handleError(e);
diff --git a/app/src/main/java/org/schabi/newpipe/settings/export/ImportExportManager.kt b/app/src/main/java/org/schabi/newpipe/settings/export/ImportExportManager.kt
index 1a92a8a00..36e0b9ce1 100644
--- a/app/src/main/java/org/schabi/newpipe/settings/export/ImportExportManager.kt
+++ b/app/src/main/java/org/schabi/newpipe/settings/export/ImportExportManager.kt
@@ -24,6 +24,8 @@ class ImportExportManager(private val fileLocator: BackupFileLocator) {
      */
     @Throws(Exception::class)
     fun exportDatabase(preferences: SharedPreferences, file: StoredFileHelper) {
+        // truncate the file before writing to it, otherwise if the new content is smaller than the
+        // previous file size, the file will retain part of the previous content and be corrupted
         ZipOutputStream(SharpOutputStream(file.openAndTruncateStream()).buffered()).use { outZip ->
             // add the database
             ZipHelper.addFileToZip(