From 68e5b4cf7995cec6f0e2dbfa22ce525acd9d2c2e Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 5 Nov 2016 18:52:31 +0100 Subject: [PATCH] convert CopyTask to kotlin --- .../filemanager/asynctasks/CopyTask.java | 89 ------------------- .../filemanager/asynctasks/CopyTask.kt | 83 +++++++++++++++++ 2 files changed, 83 insertions(+), 89 deletions(-) delete mode 100644 app/src/main/java/com/simplemobiletools/filemanager/asynctasks/CopyTask.java create mode 100644 app/src/main/java/com/simplemobiletools/filemanager/asynctasks/CopyTask.kt diff --git a/app/src/main/java/com/simplemobiletools/filemanager/asynctasks/CopyTask.java b/app/src/main/java/com/simplemobiletools/filemanager/asynctasks/CopyTask.java deleted file mode 100644 index f282f48e..00000000 --- a/app/src/main/java/com/simplemobiletools/filemanager/asynctasks/CopyTask.java +++ /dev/null @@ -1,89 +0,0 @@ -package com.simplemobiletools.filemanager.asynctasks; - -import android.os.AsyncTask; -import android.support.v4.util.Pair; -import android.util.Log; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.lang.ref.WeakReference; -import java.util.List; - -public class CopyTask extends AsyncTask, File>, Void, Boolean> { - private static final String TAG = CopyTask.class.getSimpleName(); - - private static WeakReference mListener; - private static File destinationDir; - - public CopyTask(CopyListener listener) { - mListener = new WeakReference<>(listener); - } - - @Override - protected Boolean doInBackground(Pair, File>... params) { - final Pair, File> pair = params[0]; - final List files = pair.first; - for (File file : files) { - try { - destinationDir = new File(pair.second, file.getName()); - copy(file, destinationDir); - return true; - } catch (Exception e) { - Log.e(TAG, "copy " + e); - } - } - return false; - } - - private void copy(File source, File destination) throws Exception { - if (source.isDirectory()) { - if (!destination.exists() && !destination.mkdirs()) { - throw new IOException("Could not create dir " + destination.getAbsolutePath()); - } - - final String[] children = source.list(); - for (String child : children) { - copy(new File(source, child), new File(destination, child)); - } - } else { - final File directory = destination.getParentFile(); - if (directory != null && !directory.exists() && !directory.mkdirs()) { - throw new IOException("Could not create dir " + directory.getAbsolutePath()); - } - - final InputStream in = new FileInputStream(source); - final OutputStream out = new FileOutputStream(destination); - - final byte[] buf = new byte[1024]; - int len; - while ((len = in.read(buf)) > 0) { - out.write(buf, 0, len); - } - in.close(); - out.close(); - } - } - - @Override - protected void onPostExecute(Boolean success) { - final CopyListener listener = mListener.get(); - if (listener == null) - return; - - if (success) { - listener.copySucceeded(destinationDir); - } else { - listener.copyFailed(); - } - } - - public interface CopyListener { - void copySucceeded(File destinationDir); - - void copyFailed(); - } -} diff --git a/app/src/main/java/com/simplemobiletools/filemanager/asynctasks/CopyTask.kt b/app/src/main/java/com/simplemobiletools/filemanager/asynctasks/CopyTask.kt new file mode 100644 index 00000000..c7e64772 --- /dev/null +++ b/app/src/main/java/com/simplemobiletools/filemanager/asynctasks/CopyTask.kt @@ -0,0 +1,83 @@ +package com.simplemobiletools.filemanager.asynctasks + +import android.os.AsyncTask +import android.support.v4.util.Pair +import android.util.Log +import java.io.File +import java.io.FileInputStream +import java.io.FileOutputStream +import java.io.IOException +import java.lang.ref.WeakReference + +class CopyTask(listener: CopyTask.CopyListener) : AsyncTask, File>, Void, Boolean>() { + private val TAG = CopyTask::class.java.simpleName + private var mListener: WeakReference? = null + private var destinationDir: File? = null + + init { + mListener = WeakReference(listener) + } + + override fun doInBackground(vararg params: Pair, File>): Boolean? { + val pair = params[0] + val files = pair.first + for (file in files) { + try { + destinationDir = File(pair.second, file.name) + copy(file, destinationDir!!) + return true + } catch (e: Exception) { + Log.e(TAG, "copy " + e) + } + + } + return false + } + + @Throws(Exception::class) + private fun copy(source: File, destination: File) { + if (source.isDirectory) { + if (!destination.exists() && !destination.mkdirs()) { + throw IOException("Could not create dir " + destination.absolutePath) + } + + val children = source.list() + for (child in children) { + copy(File(source, child), File(destination, child)) + } + } else { + val directory = destination.parentFile + if (!directory.exists() && !directory.mkdirs()) { + throw IOException("Could not create dir " + directory.absolutePath) + } + + val inputStream = FileInputStream(source) + val out = FileOutputStream(destination) + + val buf = ByteArray(1024) + var len: Int + while (true) { + len = inputStream.read(buf) + if (len <= 0) + break + out.write(buf, 0, len) + } + } + } + + override fun onPostExecute(success: Boolean) { + val listener = mListener?.get() ?: return + + if (success) { + listener.copySucceeded(destinationDir!!) + } else { + listener.copyFailed() + } + } + + interface CopyListener { + fun copySucceeded(destinationDir: File) + + fun copyFailed() + } +}