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

io.github.binaryfoo.tlv.ConstructedBerTlv.kt Maven / Gradle / Ivy

There is a newer version: 0.1.8
Show newest version
package io.github.binaryfoo.tlv

import java.io.ByteArrayOutputStream
import java.io.IOException

/**
 * The V represents a set of TLVs.
 */
class ConstructedBerTlv(tag: Tag, private val children: List) : BerTlv(tag) {

    override fun getChildren(): List = children

    override fun getValue(): ByteArray {
        val value = ByteArrayOutputStream()
        for (tlv in children) {
            try {
                value.write(tlv.toBinary())
            } catch (e: IOException) {
                throw RuntimeException(e)
            }

        }
        return value.toByteArray()
    }

    override fun findTlv(tag: Tag): BerTlv? = children.firstOrNull { it.tag == tag }

    override fun findTlvs(tag: Tag): List = children.filter { it.tag == tag }

    override fun toString(): String {
        val buffer = StringBuilder("\nTag: $tag\n")
        for (tlv in children) {
            buffer.append(tlv).append('\n')
        }
        buffer.append("End tag: $tag\n")
        return buffer.toString()
    }
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy