more ios/macos targets implementation
This commit is contained in:
1 parent
87a0c14b85
commit
fa87a0f611
19 files changed
+475
-30
No files matched your search
@@ -5,23 +5,23 @@ import java.io.OutputStream
|
||||
import java.nio.file.Path
|
||||
import kotlin.io.path.*
|
||||
|
||||
class FileDataProvider(val folder: Path): DataProvider {
|
||||
class FileDataProvider(val folder: Path) : DataProvider {
|
||||
|
||||
class Source(private val input: InputStream): DataSource {
|
||||
class Source(private val input: InputStream) : DataSource {
|
||||
override fun readByte(): Byte {
|
||||
val b = input.read()
|
||||
if( b < 0) throw DataSource.EndOfData()
|
||||
if (b < 0) throw DataSource.EndOfData()
|
||||
return b.toByte()
|
||||
}
|
||||
|
||||
override fun readBytes(size: Int): ByteArray {
|
||||
return input.readNBytes(size).also {
|
||||
if( it.size < size ) throw DataSource.EndOfData()
|
||||
return input.readNBytes2(size).also {
|
||||
if (it.size < size) throw DataSource.EndOfData()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Sink(private val out: OutputStream): DataSink {
|
||||
class Sink(private val out: OutputStream) : DataSink {
|
||||
override fun writeByte(data: Byte) {
|
||||
out.write(data.toInt())
|
||||
}
|
||||
@@ -44,4 +44,16 @@ class FileDataProvider(val folder: Path): DataProvider {
|
||||
.listDirectoryEntries()
|
||||
.filter { it.isRegularFile() && it.isReadable() }
|
||||
.map { it.name }
|
||||
}
|
||||
|
||||
/**
|
||||
* Compatibility with Java8 and android. Read up to N bytes
|
||||
*/
|
||||
fun InputStream.readNBytes2(size: Int): ByteArray {
|
||||
val result = ByteArray(size)
|
||||
val len = read(result)
|
||||
return if (len < size)
|
||||
result.sliceArray(0..<len)
|
||||
else
|
||||
result
|
||||
}
|
||||
Reference in new issue
Block a user