<commit_before>package com.commonsense.android.kotlin.system.resourceHandling

import android.content.ContentResolver
import android.net.Uri
import android.provider.MediaStore
import com.commonsense.android.kotlin.system.logging.tryAndLog
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.async
import java.io.BufferedInputStream
import java.io.BufferedOutputStream
import java.io.File

/**
 * Created by <NAME> on 31-07-2017.
 *
 */

fun Uri.copyTo(other: Uri, resolver: ContentResolver) = async(CommonPool) {
    val openIS = resolver.openInputStream(this@copyTo)
    val outIS = resolver.openOutputStream(other)
    tryAndLog("Uri.copyTo") {
        val bufferedIn = BufferedInputStream(openIS)
        val bufferedOut = BufferedOutputStream(outIS)
        val buff = ByteArray(32 * 1024) //32 kb buffer.
        var len: Int = bufferedIn.read(buff)
        while (len > 0) {
            bufferedOut.write(buff, 0, len)
            len = bufferedIn.read(buff)
        }
        bufferedIn.close()
        bufferedOut.close()
    }

}


fun Uri.exists(contentResolver: ContentResolver): Boolean {
    val projection = arrayOf(MediaStore.MediaColumns.DATA)
    val query = contentResolver.query(this,
            projection, null, null, null)
    return query.use {
        return@use tryAndLog("Uri.exists") {
            return@tryAndLog if (query?.moveToFirst() == true) {
                val path = query.getString(0)
                File(path).exists()
            } else {
                false
            }
        } ?: true
    }
}<commit_msg>previous method did not work, this according to docs should "do it"; but will try and see if we can avoid exceptions as part of the flow<commit_after>package com.commonsense.android.kotlin.system.resourceHandling

import android.content.ContentResolver
import android.net.Uri
import com.commonsense.android.kotlin.system.logging.tryAndLog
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.async
import java.io.BufferedInputStream
import java.io.BufferedOutputStream

/**
 * Created by <NAME> on 31-07-2017.
 *
 */

fun Uri.copyTo(other: Uri, resolver: ContentResolver) = async(CommonPool) {
    val openIS = resolver.openInputStream(this@copyTo)
    val outIS = resolver.openOutputStream(other)
    tryAndLog("Uri.copyTo") {
        val bufferedIn = BufferedInputStream(openIS)
        val bufferedOut = BufferedOutputStream(outIS)
        val buff = ByteArray(32 * 1024) //32 kb buffer.
        var len: Int = bufferedIn.read(buff)
        while (len > 0) {
            bufferedOut.write(buff, 0, len)
            len = bufferedIn.read(buff)
        }
        bufferedIn.close()
        bufferedOut.close()
    }

}


fun Uri.exists(contentResolver: ContentResolver): Boolean {
    try {
        contentResolver.openAssetFileDescriptor(this, "r").use {
            return true
        }
    } catch (exception: Exception) {
        return false
    }
}
{'fraction_non_alphanumeric': 0.09919571045576407, 'fraction_numerical': 0.012399463806970509, 'mean_word_length': 2.932806324110672, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '989507', 'n_tokens_mistral': 921, 'n_tokens_neox': 856, 'n_words': 195}