mirror of
https://github.com/mihonapp/mihon.git
synced 2025-11-12 20:19:05 +01:00
Minor changes
This commit is contained in:
@@ -5,11 +5,6 @@ import java.io.IOException;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
import okhttp3.internal.Util;
|
||||
import okio.BufferedSink;
|
||||
import okio.BufferedSource;
|
||||
import okio.Okio;
|
||||
|
||||
public final class DiskUtils {
|
||||
|
||||
private DiskUtils() {
|
||||
@@ -39,34 +34,7 @@ public final class DiskUtils {
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static File saveBufferedSourceToDirectory(BufferedSource bufferedSource, File directory, String name) throws IOException {
|
||||
createDirectory(directory);
|
||||
|
||||
File writeFile = new File(directory, name);
|
||||
if (writeFile.exists()) {
|
||||
if (writeFile.delete()) {
|
||||
writeFile = new File(directory, name);
|
||||
} else {
|
||||
throw new IOException("Failed Deleting Existing File for Overwrite");
|
||||
}
|
||||
}
|
||||
|
||||
BufferedSink bufferedSink = null;
|
||||
try {
|
||||
bufferedSink = Okio.buffer(Okio.sink(writeFile));
|
||||
bufferedSink.writeAll(bufferedSource);
|
||||
Util.closeQuietly(bufferedSink);
|
||||
} catch (Exception e) {
|
||||
Util.closeQuietly(bufferedSink);
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
writeFile.delete();
|
||||
throw new IOException("Unable to save image");
|
||||
}
|
||||
|
||||
return writeFile;
|
||||
}
|
||||
|
||||
|
||||
public static void deleteFiles(File inputFile) {
|
||||
if (inputFile.isDirectory()) {
|
||||
for (File childFile : inputFile.listFiles()) {
|
||||
|
||||
39
app/src/main/java/eu/kanade/tachiyomi/util/OkioExtensions.kt
Normal file
39
app/src/main/java/eu/kanade/tachiyomi/util/OkioExtensions.kt
Normal file
@@ -0,0 +1,39 @@
|
||||
package eu.kanade.tachiyomi.util
|
||||
|
||||
import okio.BufferedSource
|
||||
import okio.Okio
|
||||
import java.io.File
|
||||
import java.io.OutputStream
|
||||
|
||||
/**
|
||||
* Saves the given source to a file and closes it. Directories will be created if needed.
|
||||
*
|
||||
* @param file the file where the source is copied.
|
||||
*/
|
||||
fun BufferedSource.saveTo(file: File) {
|
||||
try {
|
||||
// Create parent dirs if needed
|
||||
file.parentFile.mkdirs()
|
||||
|
||||
// Copy to destination
|
||||
saveTo(file.outputStream())
|
||||
} catch (e: Exception) {
|
||||
close()
|
||||
file.delete()
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the given source to an output stream and closes both resources.
|
||||
*
|
||||
* @param stream the stream where the source is copied.
|
||||
*/
|
||||
fun BufferedSource.saveTo(stream: OutputStream) {
|
||||
use { input ->
|
||||
Okio.buffer(Okio.sink(stream)).use {
|
||||
it.writeAll(input)
|
||||
it.flush()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user