...
 
Commits (6)
......@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
* KSocket (`klib.net.socket.KSocket`)
* KString (`klib.text.KString`)
* `asFileInputStream` extension for String
* `asFileOutputStream` extension for String
### Changed
* Moved and Renamed a lot of packages (Major Breaking Change)
### Deprecated
......
......@@ -10,7 +10,9 @@ import klib.library.LClass
import klib.library.LFunction
import klib.library.Library
import java.io.File
import java.io.FileInputStream
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.lang.reflect.Type
import java.util.Base64
......@@ -232,6 +234,30 @@ fun String.fromBase64(): String {
*/
fun String.asFile() = File(this)
/**
* Opens the string as file and returns the input stream
*
* @return FileInputStream with string as file
*
* @since 4.0.0
* @author Thomas Obernosterer
*/
fun String.asFileInputStream(): FileInputStream {
return asFile().inputStream()
}
/**
* Opens the string as file and returns the output stream
*
* @return FileOutputStream with string as file
*
* @since 4.0.0
* @author Thomas Obernosterer
*/
fun String.asFileOutputStream(): FileOutputStream {
return asFile().outputStream()
}
/**
* Make the string a file and load it as library and load a class from it
*
......
......@@ -2,6 +2,15 @@ package klib.functions
import klib.text.KString
/**
* StringBuilder DSL
*
* @param builder Receiver of KString Builder
* @return Build String
*
* @since 4.0.0
* @author Thomas Obernosterer
*/
fun kstring(builder: KString.() -> Unit): String {
return KString().apply(builder).toString()
}
\ No newline at end of file
package klib.functions
import java.io.Closeable
/**
* .Net like function that proxies Kotlin's .use
*
* @param receiver The Closable input
* @param block The function that uses receiver
* @return Some data from block
*
* @since 4.0.0
* @author Thomas Obernosterer
*/
inline fun <T : Closeable?, R> using(receiver: T, block: (T) -> R): R {
return receiver.use(block)
}
\ No newline at end of file
package klib.library
/**
* Library Data Wrapper class for DSLs
*
* @param file The file to load from
* @param className The class to load
* @param functionName The function to load
*
* @since 4.0.0
* @author Thomas Obernosterer
*/
class LibLoad(var file: String = "", var className: String = "", var functionName: String = "")
\ No newline at end of file
......@@ -5,6 +5,12 @@ import java.nio.CharBuffer
import java.util.Timer
import java.util.TimerTask
/**
* Simple Read-Only UNIX inspired "Socket"
*
* @since 4.0.0
* @author Thomas Obernosterer
*/
class KSocket {
private lateinit var file: File
private lateinit var buffer: CharBuffer
......@@ -17,6 +23,19 @@ class KSocket {
get() = canReadFile
set(value) = TODO("Cannot set value '$value' on read-only field 'canRead'")
/**
* Open up a new Socket
*
* For creation a CharBuffer has the be passed in. The reason for this is customization of Buffer-Size and where
* the buffer is located (in class with default Buffer or in class-usage scope)
*
* @param file Socket File
* @param timeout Timeout for Timed Reads (Default 25 ms)
* @param buffer CharBuffer for output date (Default 1024)
*
* @since 4.0.0
* @author Thomas Obernosterer
*/
fun open(file: String, timeout: Long = 25, buffer: CharBuffer = CharBuffer.allocate(1024)) {
this.fileName = file
this.timeout = timeout
......@@ -27,6 +46,16 @@ class KSocket {
this.canReadFile = this.file.exists() && this.file.canRead()
}
/**
* Tries to read from the Socket
*
* If the read was successful, the data property is filled.
*
* @return The size of the read (-1 on error)
*
* @since 4.0.0
* @author Thomas Obernosterer
*/
fun read(): Int {
if (!isReadable()) {
data.clear()
......@@ -36,6 +65,14 @@ class KSocket {
return file.reader().read(buffer)
}
/**
* Try to read every X milliseconds if it changed
*
* @param block Function that uses read data
*
* @since 4.0.0
* @author Thomas Obernosterer
*/
fun readTimed(block: (size: Int, data: CharBuffer) -> Unit) {
var lastTime = 0L
var size: Int
......
package klib.text
/**
* Simple StringBuilder DSL-wrapper
*
* @since 4.0.0
* @author Thomas Obernosterer
*/
class KString {
private val stringBuilder = StringBuilder()
val length = stringBuilder.length
/**
* Append Line to Builder
*
* @since 4.0.0
* @author Thomas Obernosterer
*/
fun line(value: String) {
stringBuilder.appendln(value)
}
/**
* Append Text to Builder
*
* @since 4.0.0
* @author Thomas Obernosterer
*/
fun text(value: String) {
stringBuilder.append(value)
}
/**
* Append Text to Builder
*
* @since 4.0.0
* @author Thomas Obernosterer
*/
operator fun plus(value: String) {
stringBuilder.append(value)
}
/**
* Append Text to Builder
*
* @since 4.0.0
* @author Thomas Obernosterer
*/
operator fun String.unaryPlus() {
stringBuilder.append(this)
}
/**
* Append Text to Builder
*
* @since 4.0.0
* @author Thomas Obernosterer
*/
operator fun plusAssign(value: String) {
stringBuilder.append(value)
}
/**
* Get the Builder's Data
*
* @return Build string
*
* @since 4.0.0
* @author Thomas Obernosterer
*/
override fun toString(): String {
return stringBuilder.toString()
}
......
package devtests
import klib.functions.loadClass
import klib.library.LClass
import klib.library.Library
import java.io.File
/**
* Source of hello.kt
......
package devtests
import klib.functions.using
import java.io.File
fun main() {
using(File("").inputStream()) {
}
}
\ No newline at end of file