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

run.smt.ktest.util.resource.resource.kt Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
package run.smt.ktest.util.resource

import java.nio.charset.StandardCharsets.UTF_8
import java.nio.file.NoSuchFileException

private val classLoader: ClassLoader
  get() {
    return Thread.currentThread().contextClassLoader
  }

fun String.load() = classLoader.getResourceAsStream(this)
    ?: throw NoSuchFileException("No file found at resource path \"$this\"")

fun String.loadAsBytes() = load().use { it.readBytes() }

fun String.loadAsString() = String(loadAsBytes(), UTF_8)

fun String.resourceExists() = classLoader.getResource(this) != null

inline fun  C.use(action: (C) -> R): R {
  try {
    return action(this)
  } finally {
    try {
      close()
    } catch (ee: Exception) {
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy