Fix downloading/exporting when overwriting file would not truncate
This commit is contained in:
parent
57e66b17c6
commit
df941670a8
|
@ -76,7 +76,7 @@ public class SubscriptionsExportService extends BaseImportExportService {
|
|||
|
||||
try {
|
||||
outFile = new StoredFileHelper(this, path, "application/json");
|
||||
outputStream = new SharpOutputStream(outFile.getStream());
|
||||
outputStream = new SharpOutputStream(outFile.openAndTruncateStream());
|
||||
} catch (final IOException e) {
|
||||
handleError(e);
|
||||
return START_NOT_STICKY;
|
||||
|
|
|
@ -24,8 +24,7 @@ class ImportExportManager(private val fileLocator: BackupFileLocator) {
|
|||
*/
|
||||
@Throws(Exception::class)
|
||||
fun exportDatabase(preferences: SharedPreferences, file: StoredFileHelper) {
|
||||
file.create()
|
||||
ZipOutputStream(SharpOutputStream(file.stream).buffered()).use { outZip ->
|
||||
ZipOutputStream(SharpOutputStream(file.openAndTruncateStream()).buffered()).use { outZip ->
|
||||
// add the database
|
||||
ZipHelper.addFileToZip(
|
||||
outZip,
|
||||
|
|
|
@ -189,6 +189,19 @@ public class StoredFileHelper implements Serializable {
|
|||
}
|
||||
}
|
||||
|
||||
public SharpStream openAndTruncateStream() throws IOException {
|
||||
final SharpStream sharpStream = getStream();
|
||||
try {
|
||||
sharpStream.setLength(0);
|
||||
} catch (final Throwable e) {
|
||||
// we can't use try-with-resources here, since we only want to close the stream if an
|
||||
// exception occurs, but leave it open if everything goes well
|
||||
sharpStream.close();
|
||||
throw e;
|
||||
}
|
||||
return sharpStream;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates whether it's using the {@code java.io} API.
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue