com.fireflysource.net.http.common.content.provider.AbstractByteBufferContentProvider.kt Maven / Gradle / Ivy
package com.fireflysource.net.http.common.content.provider
import com.fireflysource.common.io.InputChannel
import com.fireflysource.common.sys.Result
import java.nio.ByteBuffer
import java.util.concurrent.CompletableFuture
import kotlin.math.min
abstract class AbstractByteBufferContentProvider(private val content: ByteBuffer) : InputChannel {
private val buffer = content.duplicate()
private val length = content.remaining().toLong()
private var open = true
override fun isOpen(): Boolean = open
override fun close() {
open = false
}
open fun length(): Long = length
open fun toByteBuffer(): ByteBuffer = buffer.duplicate()
override fun read(byteBuffer: ByteBuffer): CompletableFuture {
if (!isOpen) {
return endStream()
}
if (!content.hasRemaining()) {
return endStream()
}
if (!byteBuffer.hasRemaining()) {
val future = CompletableFuture()
future.complete(0)
return future
}
val len = min(content.remaining(), byteBuffer.remaining())
val to = ByteArray(len)
content.get(to)
byteBuffer.put(to)
val future = CompletableFuture()
future.complete(len)
return future
}
override fun closeAsync(): CompletableFuture {
close()
return Result.DONE
}
}