Skip to content

Commit 666c1e5

Browse files
authored
Fix another file moving crash on lower API levels (#157)
1 parent 9c5b8b4 commit 666c1e5

File tree

4 files changed

+66
-47
lines changed

4 files changed

+66
-47
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ All notable changes to this project will be documented in this file.
77
### Removed
88
### Fixed
99

10+
## [0.71.6]
11+
### Fixed
12+
* Fix another crash caused by org.apache.commons.io.FileUtils.moveFile
13+
1014
## [0.71.5]
1115
### Fixed
1216
* Fix crash caused by org.apache.commons.io.FileUtils.moveFile w/ API level below 26 (Android Oreo)

core/src/main/java/io/snabble/sdk/ProductDatabase.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import io.snabble.sdk.codes.templates.CodeTemplate;
2929
import io.snabble.sdk.utils.Dispatch;
3030
import io.snabble.sdk.utils.Downloader;
31+
import io.snabble.sdk.utils.FileUtilsSupport;
3132
import io.snabble.sdk.utils.Logger;
3233
import io.snabble.sdk.utils.StringNormalizer;
3334

@@ -696,7 +697,7 @@ private void swap(File otherDbFile) throws IOException {
696697
File dbFile = application.getDatabasePath(dbName);
697698

698699
if (!dbFile.exists() || application.deleteDatabase(dbFile.getName())) {
699-
FileUtils.moveFile(otherDbFile, dbFile);
700+
FileUtilsSupport.moveFile(otherDbFile, dbFile);
700701
application.deleteDatabase(otherDbFile.getName());
701702
}
702703

@@ -1468,4 +1469,4 @@ public interface UpdateCallback {
14681469

14691470
void error();
14701471
}
1471-
}
1472+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
@file:JvmName("FileUtilsSupport")
2+
3+
package io.snabble.sdk.utils
4+
5+
import android.os.Build
6+
import org.apache.commons.io.FileUtils
7+
import java.io.File
8+
import java.io.FileInputStream
9+
import java.io.FileNotFoundException
10+
import java.io.FileOutputStream
11+
import java.io.IOException
12+
13+
object FileUtilsSupport {
14+
15+
@JvmStatic
16+
@Throws(IOException::class)
17+
fun moveFile(srcFile: File, dstFile: File) {
18+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
19+
FileUtils.moveFile(srcFile, dstFile)
20+
} else {
21+
moveFileSupport(srcFile, dstFile)
22+
}
23+
}
24+
25+
private fun moveFileSupport(srcFile: File, dstFile: File) {
26+
try {
27+
val isRenamed = srcFile.renameTo(dstFile)
28+
if (!isRenamed) {
29+
copyFile(srcFile, dstFile)
30+
}
31+
} catch (_: SecurityException) {
32+
Logger.d("No write access to either srcFile, dstFile or both.")
33+
} catch (_: NullPointerException) {
34+
Logger.d("dstFile must not be null.")
35+
}
36+
FileUtils.deleteQuietly(srcFile)
37+
}
38+
39+
private fun copyFile(srcFile: File, dstFile: File) {
40+
try {
41+
FileOutputStream(dstFile).channel.use { dstChannel ->
42+
FileInputStream(srcFile).channel.use { srcChannel ->
43+
srcChannel.transferTo(
44+
0,
45+
srcChannel.size(),
46+
dstChannel
47+
)
48+
}
49+
}
50+
} catch (_: FileNotFoundException) {
51+
Logger.d("Either srcFile, dstFile or both do not exist.")
52+
} catch (_: SecurityException) {
53+
Logger.d("No write access to either srcFile, destFile or both.")
54+
} catch (exception: IOException) {
55+
Logger.e("Could not write srcFile to dstFile: %s", exception.toString())
56+
}
57+
}
58+
}

utils/src/main/java/io/snabble/sdk/utils/StringDownloader.java

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,16 @@
44
import android.content.pm.PackageInfo;
55
import android.content.pm.PackageManager;
66
import android.content.res.Resources;
7-
import android.os.Build;
8-
9-
import androidx.annotation.NonNull;
10-
import androidx.annotation.RequiresApi;
117

128
import org.apache.commons.io.FileUtils;
139
import org.apache.commons.io.FilenameUtils;
1410
import org.apache.commons.io.IOUtils;
1511

1612
import java.io.File;
1713
import java.io.FileInputStream;
18-
import java.io.FileNotFoundException;
1914
import java.io.FileOutputStream;
2015
import java.io.IOException;
2116
import java.io.InputStream;
22-
import java.nio.channels.FileChannel;
2317
import java.nio.charset.Charset;
2418

2519
import okhttp3.OkHttpClient;
@@ -132,7 +126,7 @@ public synchronized void updateStorage(String content) {
132126
fos.close();
133127

134128
FileUtils.deleteQuietly(storageFile);
135-
moveFileSupport(tempFile, storageFile);
129+
FileUtilsSupport.moveFile(tempFile, storageFile);
136130
Logger.d("Updated saved data:%s", storageFile.getAbsolutePath());
137131
} catch (IOException e) {
138132
Logger.e("Could not update saved data %s", e.toString());
@@ -149,42 +143,4 @@ protected void onResponse(Response response) throws IOException {
149143
}
150144

151145
protected abstract void onDownloadFinished(String string);
152-
153-
private void moveFileSupport(final File srcFile, final File destFile) throws IOException {
154-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
155-
FileUtils.moveFile(srcFile, destFile);
156-
} else {
157-
moveFile(srcFile, destFile);
158-
}
159-
}
160-
161-
private void moveFile(@NonNull final File srcFile, @NonNull final File destFile) {
162-
try {
163-
final boolean isRenamed = srcFile.renameTo(destFile);
164-
if (!isRenamed) {
165-
copyFile(srcFile, destFile);
166-
}
167-
} catch (final SecurityException exception) {
168-
Logger.d("No write access to either srcFile, destFile or both.");
169-
} catch (final NullPointerException exception) {
170-
Logger.d("destFile must not be null.");
171-
}
172-
FileUtils.deleteQuietly(srcFile);
173-
}
174-
175-
private void copyFile(@NonNull final File srcFile, @NonNull final File destFile) {
176-
//noinspection resource
177-
try (
178-
final FileChannel dstChannel = new FileOutputStream(destFile).getChannel();
179-
final FileChannel srcChannel = new FileInputStream(srcFile).getChannel()
180-
) {
181-
srcChannel.transferTo(0, srcChannel.size(), dstChannel);
182-
} catch (final FileNotFoundException exception) {
183-
Logger.d("Either srcFile, destFile or both do not exist.");
184-
} catch (final SecurityException exception) {
185-
Logger.d("No write access to either srcFile, destFile or both.");
186-
} catch (final IOException exception) {
187-
Logger.e("Could not write srcFile to destFile: %s", exception.toString());
188-
}
189-
}
190146
}

0 commit comments

Comments
 (0)