From fa5b64ce2e60086d244ea0591ca13a0a119dc102 Mon Sep 17 00:00:00 2001 From: NoodleMage Date: Thu, 25 Feb 2016 21:08:42 +0100 Subject: [PATCH] Rewrote IOHandler to Kotlin --- .../kanade/tachiyomi/data/io/IOHandler.java | 45 -------------- .../eu/kanade/tachiyomi/data/io/IOHandler.kt | 62 +++++++++++++++++++ .../tachiyomi/ui/library/LibraryFragment.kt | 9 +-- 3 files changed, 67 insertions(+), 49 deletions(-) delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/data/io/IOHandler.java create mode 100644 app/src/main/java/eu/kanade/tachiyomi/data/io/IOHandler.kt diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/io/IOHandler.java b/app/src/main/java/eu/kanade/tachiyomi/data/io/IOHandler.java deleted file mode 100644 index cfe587929..000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/data/io/IOHandler.java +++ /dev/null @@ -1,45 +0,0 @@ -package eu.kanade.tachiyomi.data.io; - -import android.content.Context; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; - -public class IOHandler { - private static String getTempFilename(Context context) throws IOException { - File outputDir = context.getCacheDir(); - File outputFile = File.createTempFile("temp_cover", "0", outputDir); - return outputFile.getAbsolutePath(); - } - - public static String downloadMediaAndReturnPath(FileInputStream input, Context context) { - FileOutputStream output = null; - try { - - String tempFilename = getTempFilename(context); - output = new FileOutputStream(tempFilename); - - int read; - byte[] bytes = new byte[4096]; - while ((read = input.read(bytes)) != -1) { - output.write(bytes, 0, read); - } - return tempFilename; - } catch (IOException ignored) { - } finally { - if (input != null) try { - input.close(); - } catch (Exception ignored) { - } - if (output != null) try { - output.close(); - } catch (Exception ignored) { - } - } - return null; - - } - -} diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/io/IOHandler.kt b/app/src/main/java/eu/kanade/tachiyomi/data/io/IOHandler.kt new file mode 100644 index 000000000..cbf4a0e79 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/data/io/IOHandler.kt @@ -0,0 +1,62 @@ +@file:JvmName("IOHandler") +package eu.kanade.tachiyomi.data.io + +import android.content.Context +import eu.kanade.tachiyomi.R +import eu.kanade.tachiyomi.util.ToastUtil +import java.io.File +import java.io.FileInputStream +import java.io.FileOutputStream +import java.io.IOException + +/** + * Returns temp file location + * + * @param context context of application + * @throws IOException IO exception + * @return location of temp file + */ + @Throws(IOException::class) + private fun getTempFilename(context: Context): String { + // Get output directory. + val outputDir = context.cacheDir + + // Create temporary file + val outputFile = File.createTempFile("temp_cover", "0", outputDir) + + // Return path of temporary file + return outputFile.absolutePath + } + + /** + * Download media to temp location and returns file path + * + * @param input input stream containing input file + * @param context context of application + * @throws IOException IO exception + * @return location of temp file + */ + @Throws(IOException::class) + fun downloadMediaAndReturnPath(input: FileInputStream, context: Context): String { + var tempFilename = "" + var output: FileOutputStream? = null + try { + // Get temp file name. + tempFilename = getTempFilename(context) + + output = FileOutputStream(tempFilename) + // Copy input stream to temp location. + input.copyTo(output) + } catch (e: IOException) { + // Show user something went wrong and print stackTrace. + ToastUtil.showShort(context, R.string.notification_manga_update_failed) + e.printStackTrace() + } finally { + // Close streams. + input.close() + output?.close() + } + + // Return temp name. + return tempFilename + } \ No newline at end of file diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryFragment.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryFragment.kt index 81eb726da..5766d2959 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryFragment.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryFragment.kt @@ -14,7 +14,6 @@ import eu.davidea.flexibleadapter.FlexibleAdapter import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.database.models.Category import eu.kanade.tachiyomi.data.database.models.Manga -import eu.kanade.tachiyomi.data.io.IOHandler import eu.kanade.tachiyomi.data.library.LibraryUpdateService import eu.kanade.tachiyomi.event.LibraryMangasEvent import eu.kanade.tachiyomi.ui.base.fragment.BaseRxFragment @@ -311,17 +310,18 @@ class LibraryFragment : BaseRxFragment(), ActionMode.Callback override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) { if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_IMAGE_OPEN) { selectedCoverManga?.let { manga -> + + try { // Get the file's input stream from the incoming Intent val inputStream = context.contentResolver.openInputStream(data.data) // Convert to absolute path to prevent FileNotFoundException - val result = IOHandler.downloadMediaAndReturnPath(inputStream as FileInputStream, + val result = eu.kanade.tachiyomi.data.io.downloadMediaAndReturnPath(inputStream as FileInputStream, context) // Get file from filepath - val picture = File(result ?: "") + val picture = File(result) - try { // Update cover to selected file, show error if something went wrong if (presenter.editCoverWithLocalFile(picture, manga)) { adapter.refreshRegisteredAdapters() @@ -330,6 +330,7 @@ class LibraryFragment : BaseRxFragment(), ActionMode.Callback } } catch (e: IOException) { + ToastUtil.showShort(context, R.string.notification_manga_update_failed) e.printStackTrace() } }