Skip to content

Commit

Permalink
Merge pull request #52 from fleeksoft/develop
Browse files Browse the repository at this point in the history
fix build configurations
  • Loading branch information
itboy87 authored Aug 27, 2024
2 parents dca8d13 + f2f2536 commit 4b456a6
Show file tree
Hide file tree
Showing 23 changed files with 475 additions and 99 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ jobs:
{ target: windows, os: windows-latest, tasks: mingwX64Test, continueOnError: false },
{ target: linux, os: windows-latest, tasks: linuxX64Test, continueOnError: false },
]
isKorlibs: [ "true", "false" ]
libBuildType: [ "korlibs", "kotlinx" ]
runs-on: ${{ matrix.config.os }}
name: Build ${{ matrix.config.target }} with isKorlibs=${{ matrix.isKorlibs }}
name: Build ${{ matrix.config.target }} with libBuildType=${{ matrix.libBuildType }}
steps:
- uses: actions/checkout@v3
- name: Setup JDK 17
Expand All @@ -34,9 +34,9 @@ jobs:
- name: Setup gradle
uses: gradle/gradle-build-action@v2

- name: Test ${{ matrix.config.target }} targets with isKorlibs=${{ matrix.isKorlibs }}
- name: Test ${{ matrix.config.target }} targets with libBuildType=${{ matrix.libBuildType }}
continue-on-error: ${{ matrix.config.continueOnError }}
run: ./gradlew ${{ matrix.config.tasks }} -PisKorlibs=${{ matrix.isKorlibs }} --info
run: ./gradlew ${{ matrix.config.tasks }} -PlibBuildType=${{ matrix.libBuildType }} --info
working-directory: ${{ github.workspace }}

# deploy:
Expand Down
7 changes: 4 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#Gradle
org.gradle.jvmargs=-Xmx2048M -Dfile.encoding=UTF-8 -Dkotlin.daemon.jvm.options\="-Xmx2048M"
org.gradle.caching=true

#Kotlin
kotlin.code.style=official

#Android
android.useAndroidX=true
android.nonTransitiveRClass=true
kotlin.native.ignoreIncorrectDependencies=true

isKorlibs = false
# dev, kotlinx, korlibs, okio
# dev will include all modules in project
libBuildType=korlibs
40 changes: 38 additions & 2 deletions ksoup-engine-common/src/com/fleeksoft/ksoup/io/KByteBuffer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,26 @@ class KByteBuffer(capacity: Int) {
return readAvailable <= 0
}

fun skip(n: Int) {
position = min(position + n, buffer.size)
}

fun clone(): KByteBuffer {
return KByteBuffer(buffer.size).apply {
position = this@KByteBuffer.position
readAvailable = this@KByteBuffer.readAvailable
offset = this@KByteBuffer.offset
this@KByteBuffer.buffer.copyInto(buffer)
}
}

fun readText(charset: Charset, maxBytes: Int): String {
val endIndex = min(position + maxBytes, position + readAvailable)

val byteArray = if (position == 0 && endIndex == buffer.size) {
buffer
} else {
buffer.slice(position until endIndex).toByteArray()
buffer.sliceArray(position until endIndex)
}

val stringBuilder = StringBuilder()
Expand All @@ -60,7 +73,30 @@ class KByteBuffer(capacity: Int) {
return string
}

fun writeBytes(byteArray: ByteArray, length: Int) {
fun readBytes(count: Int): ByteArray {
val byteArray = buffer.sliceArray(position until min(position + count, position + readAvailable))
position += byteArray.size
readAvailable -= byteArray.size
return byteArray
}

fun readAll(): ByteArray {
return buffer.sliceArray(position until position + readAvailable).also {
position += readAvailable
readAvailable = 0
}
}

fun writeByte(byte: Byte) {
buffer[position++] = byte
readAvailable++
offset++
if (offset >= buffer.size) {
offset = 0
}
}

fun writeBytes(byteArray: ByteArray, length: Int = byteArray.size) {
// println("writeBytes: $length")
require(byteArray.size <= size)
byteArray.copyInto(buffer, destinationOffset = offset, endIndex = length)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ interface SourceReader {

public fun readBytes(count: Int): ByteArray

public fun read(): Byte

public fun read(bytes: ByteArray, offset: Int = 0, length: Int = bytes.size): Int

public fun readAllBytes(): ByteArray
Expand All @@ -20,6 +18,4 @@ interface SourceReader {
public fun close()

public fun readAtMostTo(sink: KByteBuffer, byteCount: Int): Int

public fun peek(): SourceReader
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ object KsoupEngineImpl : KsoupEngine {
return URL.resolveOrNull(base = base, access = relUrl)
}

override fun openSourceReader(
content: String,
charset: Charset?
): SourceReader {
override fun openSourceReader(content: String, charset: Charset?): SourceReader {
return SourceReaderImpl(charset?.toByteArray(content) ?: content.encodeToByteArray())
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package com.fleeksoft.ksoup.io

import korlibs.io.stream.MarkableSyncInputStream
import korlibs.io.stream.SyncInputStream
import korlibs.io.stream.markable
import korlibs.io.stream.openSync
import korlibs.memory.buildByteArray
import kotlin.math.min


class SourceReaderAsyncImpl : SourceReader {
private val stream: MarkableSyncInputStream
private val buffer: KByteBuffer = KByteBuffer(8192)
private var markBuffer: KByteBuffer? = null
private var sourceEmpty = false

constructor(stream: SyncInputStream) {
this.stream = stream.markable()
}

constructor(bytes: ByteArray) : this(bytes.openSync())

override fun skip(count: Long) {
var skippedBuffer = 0
if (buffer().available() > 0) {
skippedBuffer = min(buffer().available(), count.toInt())
buffer().skip(skippedBuffer)
}
if (skippedBuffer < count) {
stream.skip(count.toInt() - skippedBuffer)
}
}

override fun mark(readLimit: Long) {
markBuffer = buffer.clone()
stream.mark(readLimit.toInt())
}

override fun reset() {
markBuffer = null
stream.reset()
}

private fun MarkableSyncInputStream.safeReadBytes(len: Int): ByteArray {
var i = 0
return buildByteArray {
while (i < len) {
var byte = this@safeReadBytes.read()
if (byte == -1) byte = this@safeReadBytes.read()
if (byte == -1) {
break
} else {
this.append(byte)
}
i++
}
}
}

override fun readBytes(count: Int): ByteArray {
return buildByteArray {
if (buffer().available() > 0) {
append(buffer().readBytes(count))
buffer().compact()
}
if (!sourceEmpty && this.size < count) {
append(stream.safeReadBytes(count - this.size))
}
}
}

override fun read(bytes: ByteArray, offset: Int, length: Int): Int {
val byteArray = readBytes(length)
if (byteArray.isNotEmpty()) {
byteArray.copyInto(bytes, offset, 0, offset + length)
}
return byteArray.size
}

private fun buffer() = markBuffer ?: buffer

override fun readAllBytes(): ByteArray {
return buildByteArray {
if (buffer().available() > 0) {
append(buffer().readAll())
buffer().compact()
}
while (true) {
val byte = stream.read()
if (byte != -1) {
this.append(byte.toByte())
} else {
break;
}
}
}
}

override fun exhausted(): Boolean {
if (buffer().available() > 0) return false
if (sourceEmpty) return true
val len = buffer().size
val bytes = stream.safeReadBytes(len)
if (bytes.size < len) {
sourceEmpty = true
}
buffer().writeBytes(bytes, bytes.size)
return (buffer().available() > 0).not()
}

override fun close() {
stream.close()
}

override fun readAtMostTo(sink: KByteBuffer, byteCount: Int): Int {
val bytes = this.readBytes(byteCount)
sink.writeBytes(bytes, bytes.size)
return bytes.size
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ class SourceReaderImpl : SourceReader {
return syncStream.readBytes(count)
}

override fun read(): Byte {
return syncStream.read().toByte()
}

override fun read(bytes: ByteArray, offset: Int, length: Int): Int {
return syncStream.read(bytes, offset, length)
}
Expand All @@ -61,8 +57,4 @@ class SourceReaderImpl : SourceReader {
return bytes.size
}

override fun peek(): SourceReader {
return SourceReaderImpl(syncStream.clone())
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.fleeksoft.ksoup

import com.fleeksoft.ksoup.io.*
import korlibs.io.file.std.toVfs
import korlibs.io.stream.SyncInputStream
import korlibs.io.stream.toAsync
import korlibs.io.stream.toAsyncStream
import korlibs.io.stream.toSyncInputStream
import java.io.File
import java.io.InputStream

object JvmKotlinxMapper {
fun jvmFileToFileSource(file: File): FileSource {
return FileSourceImpl(file.toVfs())
}

fun jvmInputStreamToSourceReader(inputStream: InputStream): SourceReader {
return SourceReaderImpl(inputStream.readAllBytes())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class CharsetImpl(override val name: String) : Charset {
}

val decodedBytes = if (isUtf8) {
stringBuilder.append(byteArray.slice(start until toDecodeSize).toByteArray().decodeToString())
stringBuilder.append(byteArray.sliceArray(start until toDecodeSize).decodeToString())
toDecodeSize - start
} else {
val buffer = Buffer().apply { write(byteArray, start, toDecodeSize) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
@file:OptIn(InternalIoApi::class)

package com.fleeksoft.ksoup.io

import io.ktor.utils.io.core.*
import kotlinx.io.Buffer
import kotlinx.io.InternalIoApi
import kotlinx.io.Source
import kotlinx.io.readByteArray
import kotlin.math.min

class SourceReaderImpl : SourceReader {
private val source: Source
Expand Down Expand Up @@ -52,10 +47,6 @@ class SourceReaderImpl : SourceReader {
}
}

override fun read(): Byte {
return source().readByte()
}

override fun read(bytes: ByteArray, offset: Int, length: Int): Int {
return source().readAtMostTo(bytes, offset, endIndex = offset + length)
}
Expand All @@ -77,8 +68,4 @@ class SourceReaderImpl : SourceReader {
sink.writeBytes(bytes, bytes.size)
return bytes.size
}

override fun peek(): SourceReader {
return SourceReaderImpl(source().peek())
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.fleeksoft.ksoup

import com.fleeksoft.ksoup.io.FileSource
import com.fleeksoft.ksoup.io.FileSourceImpl
import com.fleeksoft.ksoup.io.SourceReader
import com.fleeksoft.ksoup.io.SourceReaderImpl
import kotlinx.io.asSource
import kotlinx.io.buffered
import java.io.File
import java.io.InputStream

object JvmKotlinxMapper {
fun jvmFileToFileSource(file: File): FileSource {
return FileSourceImpl(file.absolutePath)
}
fun jvmInputStreamToSourceReader(inputStream: InputStream): SourceReader {
return SourceReaderImpl(inputStream.asSource().buffered())
}
}
Loading

0 comments on commit 4b456a6

Please sign in to comment.