123456789101112131415161718192021222324252627282930313233343536373839 |
- package io.github.zadam.triliumsender.services
- import okhttp3.MediaType
- import okhttp3.RequestBody
- import okhttp3.internal.closeQuietly
- import okio.BufferedSink
- import okio.Source
- import okio.source
- import java.io.IOException
- import java.io.InputStream
- object RequestBodyUtil {
- fun create(mediaType: MediaType, inputStream: InputStream): RequestBody {
- return object : RequestBody() {
- override fun contentType(): MediaType? {
- return mediaType
- }
- override fun contentLength(): Long {
- return try {
- inputStream.available().toLong()
- } catch (e: IOException) {
- 0
- }
- }
- @Throws(IOException::class)
- override fun writeTo(sink: BufferedSink) {
- var source: Source? = null
- try {
- source = inputStream.source()
- sink.writeAll(source)
- } finally {
- source?.closeQuietly()
- }
- }
- }
- }
- }
|