Skip to content

Commit df94167

Browse files
committed
Fix downloading/exporting when overwriting file would not truncate
1 parent 57e66b1 commit df94167

3 files changed

Lines changed: 15 additions & 3 deletions

File tree

app/src/main/java/org/schabi/newpipe/local/subscription/services/SubscriptionsExportService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public int onStartCommand(final Intent intent, final int flags, final int startI
7676

7777
try {
7878
outFile = new StoredFileHelper(this, path, "application/json");
79-
outputStream = new SharpOutputStream(outFile.getStream());
79+
outputStream = new SharpOutputStream(outFile.openAndTruncateStream());
8080
} catch (final IOException e) {
8181
handleError(e);
8282
return START_NOT_STICKY;

app/src/main/java/org/schabi/newpipe/settings/export/ImportExportManager.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ class ImportExportManager(private val fileLocator: BackupFileLocator) {
2424
*/
2525
@Throws(Exception::class)
2626
fun exportDatabase(preferences: SharedPreferences, file: StoredFileHelper) {
27-
file.create()
28-
ZipOutputStream(SharpOutputStream(file.stream).buffered()).use { outZip ->
27+
ZipOutputStream(SharpOutputStream(file.openAndTruncateStream()).buffered()).use { outZip ->
2928
// add the database
3029
ZipHelper.addFileToZip(
3130
outZip,

app/src/main/java/org/schabi/newpipe/streams/io/StoredFileHelper.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,19 @@ public SharpStream getStream() throws IOException {
189189
}
190190
}
191191

192+
public SharpStream openAndTruncateStream() throws IOException {
193+
final SharpStream sharpStream = getStream();
194+
try {
195+
sharpStream.setLength(0);
196+
} catch (final Throwable e) {
197+
// we can't use try-with-resources here, since we only want to close the stream if an
198+
// exception occurs, but leave it open if everything goes well
199+
sharpStream.close();
200+
throw e;
201+
}
202+
return sharpStream;
203+
}
204+
192205
/**
193206
* Indicates whether it's using the {@code java.io} API.
194207
*

0 commit comments

Comments
 (0)