All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fireflysource.net.http.common.content.handler.AbstractByteBufferContentHandler.kt Maven / Gradle / Ivy

There is a newer version: 5.0.2
Show newest version
package com.fireflysource.net.http.common.content.handler

import com.fireflysource.common.io.BufferUtils
import com.fireflysource.common.sys.Result
import com.fireflysource.net.http.common.codec.ContentEncoded
import com.fireflysource.net.http.common.exception.BadMessageException
import com.fireflysource.net.http.common.model.ContentEncoding
import com.fireflysource.net.http.common.model.HttpStatus
import java.nio.ByteBuffer
import java.nio.charset.Charset
import java.nio.charset.StandardCharsets
import java.util.*
import java.util.concurrent.CompletableFuture

abstract class AbstractByteBufferContentHandler(
    private val maxRequestBodySize: Long = 200 * 1024 * 1024
) : HttpContentHandler {

    private val byteBufferList = LinkedList()
    private var requestSize: Long = 0
    private val utf8String: String by lazy { toString(StandardCharsets.UTF_8) }

    override fun accept(byteBuffer: ByteBuffer, u: T) {
        requestSize += byteBuffer.remaining()
        if (requestSize > maxRequestBodySize) {
            throw BadMessageException(HttpStatus.PAYLOAD_TOO_LARGE_413)
        }

        byteBufferList.add(byteBuffer)
    }

    override fun closeAsync(): CompletableFuture = Result.DONE

    override fun close() {
    }

    fun getByteBuffers(encoding: Optional = Optional.empty()): List {
        return if (encoding.isPresent) {
            val data = decode(encoding.get())
            listOf(ByteBuffer.wrap(data))
        } else byteBufferList.map { it.duplicate() }
    }

    fun toString(charset: Charset, encoding: Optional = Optional.empty()): String {
        return if (encoding.isPresent) {
            val data = decode(encoding.get())
            String(data, charset)
        } else BufferUtils.toString(byteBufferList.map { it.duplicate() }, charset)
    }

    private fun decode(encoding: ContentEncoding): ByteArray {
        val buffer = BufferUtils.merge(byteBufferList.map { it.duplicate() })
        val bytes = BufferUtils.toArray(buffer)
        return ContentEncoded.decode(bytes, encoding)
    }

    override fun toString(): String = utf8String
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy