Fix downloading/exporting when overwriting file would not truncate
This commit is contained in:
parent
57e66b17c6
commit
df941670a8
3 changed files with 15 additions and 3 deletions
|
@ -76,7 +76,7 @@ public class SubscriptionsExportService extends BaseImportExportService {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
outFile = new StoredFileHelper(this, path, "application/json");
|
outFile = new StoredFileHelper(this, path, "application/json");
|
||||||
outputStream = new SharpOutputStream(outFile.getStream());
|
outputStream = new SharpOutputStream(outFile.openAndTruncateStream());
|
||||||
} catch (final IOException e) {
|
} catch (final IOException e) {
|
||||||
handleError(e);
|
handleError(e);
|
||||||
return START_NOT_STICKY;
|
return START_NOT_STICKY;
|
||||||
|
|
|
@ -24,8 +24,7 @@ class ImportExportManager(private val fileLocator: BackupFileLocator) {
|
||||||
*/
|
*/
|
||||||
@Throws(Exception::class)
|
@Throws(Exception::class)
|
||||||
fun exportDatabase(preferences: SharedPreferences, file: StoredFileHelper) {
|
fun exportDatabase(preferences: SharedPreferences, file: StoredFileHelper) {
|
||||||
file.create()
|
ZipOutputStream(SharpOutputStream(file.openAndTruncateStream()).buffered()).use { outZip ->
|
||||||
ZipOutputStream(SharpOutputStream(file.stream).buffered()).use { outZip ->
|
|
||||||
// add the database
|
// add the database
|
||||||
ZipHelper.addFileToZip(
|
ZipHelper.addFileToZip(
|
||||||
outZip,
|
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.
|
* Indicates whether it's using the {@code java.io} API.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in a new issue