```fun downloadAttachment(id: Long): Single<......
# rx
u
Copy code
fun downloadAttachment(id: Long): Single<...> {
        return apiClient.downloadAttachment(id)
            .flatMap { responseBody ->
                Single.fromCallable {
                    val file = filesHelper.createFinDocFile(finDocId, mimeType)
                    try {
                        responseBody.byteStream().use { iss ->
                            file.outputStream().use { oss ->
                                iss.copyTo(oss)
                            }
                        }
                        ...
                    } catch (ex: Exception) {
                        file.delete()
                        throw ex
                    }
                }
            }
    }