package com.bc_chat.bc_base.task

import com.zhaohaoting.framework.abs.task.RxSimpleDataAsyncTask
import io.reactivex.Flowable
import java.io.*

/**
 * author：Created by zhaohaoting on 2019/3/5
 * email：526309416@qq.com
 * desc：分享
 */
class ShareTask constructor(var path: String?) : RxSimpleDataAsyncTask<ByteArray>() {

    override fun loadDataAsyncTask(): Flowable<ByteArray>? {
        return Flowable.fromCallable<ByteArray> {
            val fis: FileInputStream
            val baos = ByteArrayOutputStream()
            try {
                fis = FileInputStream(File(path))
                val buffer = ByteArray(1024)
                var count = 0
                while ((fis.read(buffer).also { count = it }) >= 0) {
                    baos.write(buffer, 0, count)
                }
                fis.close()

            } catch (e: FileNotFoundException) {
                e.printStackTrace()
            } catch (e: IOException) {
                e.printStackTrace()
            }
            return@fromCallable baos.toByteArray()
        }
    }
}
