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

invirt.utils.resources.kt Maven / Gradle / Ivy

package invirt.utils

import java.io.BufferedReader
import java.io.InputStream
import java.io.InputStreamReader
import java.nio.charset.Charset
import java.util.*

fun resourceAsInput(classpathLocation: String): InputStream {
    return Thread.currentThread().contextClassLoader.getResourceAsStream(classpathLocation)!!
}

fun resourceAsString(classpathLocation: String, charset: Charset = Charsets.UTF_8): String {
    return resourceAsInput(classpathLocation).use { BufferedReader(InputStreamReader(it, charset)).readText() }
}

fun resourceAsProps(classpathLocation: String, charset: Charset = Charsets.UTF_8): Properties {
    val props = Properties()
    props.load(InputStreamReader(resourceAsInput(classpathLocation), charset))
    return props
}

fun resourceAsStrings(classpathLocation: String, charset: Charset = Charsets.UTF_8): List {
    return resourceAsString(classpathLocation, charset).split("\n").map { it.cleanWhitespace() }
        .filter { it.isNotEmpty() }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy