Skip to content

Commit

Permalink
Merge pull request #4 from fleeksoft/develop
Browse files Browse the repository at this point in the history
merge with v 0.0.4 fix streaming source
  • Loading branch information
itboy87 authored Nov 23, 2023
2 parents e9a7f99 + ca9d6ff commit 0047737
Show file tree
Hide file tree
Showing 14 changed files with 97 additions and 179 deletions.
25 changes: 22 additions & 3 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ on:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

permissions:
contents: read
pages: write

jobs:
build:
strategy:
matrix:
config: [
# { target: android, os: ubuntu-latest, tasks: androidTest, continueOnError: false },
{ target: android, os: ubuntu-latest, tasks: testDebugUnitTest, continueOnError: false },
# { target: apple, os: macos-latest, tasks: iosX64Test iosSimulatorArm64Test macosX64Test macosArm64Test, continueOnError: true },
# { target: windows, os: windows-latest, tasks: windowsTest, continueOnError: true }, # https://github.com/square/okio/issues/951
# { target: linux, os: ubuntu-latest, tasks: linuxTest, continueOnError: false },
Expand Down Expand Up @@ -54,12 +59,14 @@ jobs:
- name: Setup gradle
uses: gradle/gradle-build-action@v2

- name: Setup Pages
uses: actions/configure-pages@v3

- name: Write secrets to local.properties
if: ${{ github.event_name != 'pull_request' }}
run: |
echo sonatypeUsername="${SONATYPE_USERNAME}" >> "local.properties"
echo sonatypePassword="${SONATYPE_PASSWORD}" >> "local.properties"
echo mavenCentralUsername="${SONATYPE_USERNAME}" >> "local.properties"
echo mavenCentralUsername="${SONATYPE_PASSWORD}" >> "local.properties"
echo gpgKeyPassword="${GPG_KEY_PASSWORD}" >> "local.properties"
echo gpgKeySecret="${GPG_KEY_SECRET}" >> "local.properties"
env:
Expand All @@ -70,3 +77,15 @@ jobs:

- name: Release to sonatype
run: ./gradlew publishAllPublicationsToMavenRepository

- name: Generate docs with dokka
run: ./gradlew dokkaHtmlMultiModule

- name: Upload artifact
uses: actions/upload-pages-artifact@v1
with:
path: ${{ github.workspace }}/build/dokka/htmlMultiModule

- name: Release to GitHub Pages
id: deployment
uses: actions/deploy-pages@v1
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ agp = "8.1.3"
kotlin = "1.9.20"
compileSdk = "34"
minSdk = "21"
libraryVersion = "0.0.3"
libraryVersion = "0.0.4"
junitJupiter = "5.9.3"
compose = "1.5.4"
compose-compiler = "1.5.4"
compose-material3 = "1.1.2"
androidx-activityCompose = "1.8.0"
androidx-activityCompose = "1.8.1"
ktor = "2.3.6"
okio = "3.6.0"
kotlinxDatetime = "0.4.1"
Expand Down
8 changes: 6 additions & 2 deletions ksoup-network/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ android {
defaultConfig {
minSdk = libs.versions.minSdk.get().toInt()
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
}

publishing {
Expand Down Expand Up @@ -116,8 +120,8 @@ publishing {

signing {
useInMemoryPgpKeys(
File(rootDir, "gpg/private.key").readText(),
// gradleLocalProperties(rootDir).getProperty("gpgKeySecret"),
// File(rootDir, "gpg/private.key").readText(),
gradleLocalProperties(rootDir).getProperty("gpgKeySecret"),
gradleLocalProperties(rootDir).getProperty("gpgKeyPassword"),
)
sign(publishing.publications)
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

4 changes: 2 additions & 2 deletions ksoup/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ publishing {

signing {
useInMemoryPgpKeys(
File(rootDir, "gpg/private.key").readText(),
// gradleLocalProperties(rootDir).getProperty("gpgKeySecret"),
// File(rootDir, "gpg/private.key").readText(),
gradleLocalProperties(rootDir).getProperty("gpgKeySecret"),
gradleLocalProperties(rootDir).getProperty("gpgKeyPassword"),
)
sign(publishing.publications)
Expand Down
54 changes: 25 additions & 29 deletions ksoup/src/commonMain/kotlin/com/fleeksoft/ksoup/helper/DataUtil.kt
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
package com.fleeksoft.ksoup.helper

import com.fleeksoft.ksoup.UncheckedIOException
import com.fleeksoft.ksoup.internal.ConstrainableSource
import com.fleeksoft.ksoup.internal.Normalizer
import com.fleeksoft.ksoup.internal.StringUtil
import com.fleeksoft.ksoup.nodes.Comment
import com.fleeksoft.ksoup.nodes.Document
import com.fleeksoft.ksoup.nodes.Node
import com.fleeksoft.ksoup.nodes.XmlDeclaration
import com.fleeksoft.ksoup.parser.Parser
import com.fleeksoft.ksoup.ported.BufferReader
import com.fleeksoft.ksoup.ported.IllegalCharsetNameException
import com.fleeksoft.ksoup.ported.*
import com.fleeksoft.ksoup.ported.canEncode
import com.fleeksoft.ksoup.ported.isCharsetSupported
import com.fleeksoft.ksoup.readFile
import com.fleeksoft.ksoup.readGzipFile
import com.fleeksoft.ksoup.select.Elements
import io.ktor.utils.io.charsets.*
import io.ktor.utils.io.core.*
import okio.IOException
import okio.Path
import okio.use
import kotlin.math.min
import okio.*
import okio.Buffer
import kotlin.random.Random

/**
Expand All @@ -30,7 +26,7 @@ import kotlin.random.Random
*/
internal object DataUtil {
private val charsetPattern: Regex =
Regex("(?i)\\bcharset=\\s*(?:[\"'])?([^\\s,;\"']*)")
Regex("(?i)\\bcharset=\\s*[\"']?([^\\s,;\"']*)")
val UTF_8: Charset =
Charsets.UTF_8 // Don't use StandardCharsets, as those only appear in Android API 19, and we target 10.
private val defaultCharsetName: String = UTF_8.name // used if not found in header or meta charset
Expand Down Expand Up @@ -98,11 +94,11 @@ internal object DataUtil {
.readByteArray()
)*/
} else {
BufferReader(bufferedSource.readByteArray())
BufferReader(bufferedSource)
}

} else {
BufferReader(bufferedSource.readByteArray())
BufferReader(bufferedSource)
}

// val charset = charsetName?.let { Charset.forName(it) } ?: Charsets.UTF_8
Expand Down Expand Up @@ -176,21 +172,19 @@ internal object DataUtil {
}
var charsetName: String? = charsetNameIn

val inputReader = ConstrainableSource.wrap(bufferReader, 0)

/*@Nullable */
var doc: Document? = null

// read the start of the stream and look for a BOM or meta charset

inputReader.mark(bufferSize.toInt())
val peekedBuffer = bufferReader.getPeek()
// -1 because we read one more to see if completed. First read is < buffer size, so can't be invalid.
val firstBytes: BufferReader = readToByteBuffer(inputReader, firstReadBufferSize - 1)
val fullyRead = inputReader.fullyRead()
inputReader.reset()
val firstBytes: ByteArray = readToByteBuffer(peekedBuffer)
val fullyRead = peekedBuffer.exhausted()
peekedBuffer.close()

// look for BOM - overrides any other header or input
val bomCharset = detectCharsetFromBom(firstBytes)
val bomCharset: BomCharset? = detectCharsetFromBom(firstBytes)
if (bomCharset != null) charsetName = bomCharset.charset
if (charsetName == null) { // determine from meta. safe first parse as UTF-8
doc = try {
Expand Down Expand Up @@ -268,7 +262,7 @@ internal object DataUtil {
// TODO: bufferSize not used here because not supported yet
val reader = BufferReader(
String(
inputReader.readByteArray(),
bufferReader.readByteArray(),
charset = Charset.forName(charsetName)
)
)
Expand Down Expand Up @@ -303,20 +297,23 @@ internal object DataUtil {
/**
* Read the input stream into a byte buffer. To deal with slow input streams, you may interrupt the thread this
* method is executing on. The data read until being interrupted will be available.
* @param source the input stream to read from
* @param bufferReader the input stream to read from
* @param maxSize the maximum size in bytes to read from the stream. Set to 0 to be unlimited.
* @return the filled byte buffer
* @throws IOException if an exception occurs whilst reading from the input stream.
*/
@Throws(IOException::class)
fun readToByteBuffer(bufferReader: BufferReader, maxSize: Int): BufferReader {
Validate.isTrue(maxSize >= 0, "maxSize must be 0 (unlimited) or larger")
fun readToByteBuffer(bufferReader: BufferedSource): ByteArray {
return bufferReader.readByteArray()
/*Validate.isTrue(maxSize >= 0, "maxSize must be 0 (unlimited) or larger")
val calculatedMaxSize: Int =
if (bufferReader.size() > 0) bufferReader.size().toInt() else maxSize
val input: ConstrainableSource =
ConstrainableSource.wrap(
bufferReader = bufferReader,
maxSize = min(maxSize.toLong(), bufferReader.getActiveBuffer().size).toInt()
maxSize = calculatedMaxSize
)
return input.readToByteBuffer(maxSize)
return input.readToByteBuffer(maxSize)*/
}

fun emptyByteBuffer(): BufferReader {
Expand Down Expand Up @@ -388,13 +385,12 @@ internal object DataUtil {
return null
}*/

private fun detectCharsetFromBom(buffer: BufferReader): BomCharset? {
private fun detectCharsetFromBom(firstByteArray: ByteArray): BomCharset? {
// .mark and rewind used to return Buffer, now ByteBuffer, so cast for backward compat
buffer.mark()
val bom = ByteArray(4)
if (buffer.remaining() >= bom.size) {
buffer[bom]
buffer.rewind()
val bom = if (firstByteArray.size >= 4) {
firstByteArray.copyOf(4)
} else {
ByteArray(4)
}
if (bom[0].toInt() == 0x00 && bom[1].toInt() == 0x00 && bom[2] == 0xFE.toByte() && bom[3] == 0xFF.toByte() || // BE
bom[0] == 0xFF.toByte() && bom[1] == 0xFE.toByte() && bom[2].toInt() == 0x00 && bom[3].toInt() == 0x00
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ package com.fleeksoft.ksoup.internal
* A com.fleeksoft.ksoup internal class (so don't use it as there is no contract API) that enables constraints on an Input Stream,
* namely a maximum read size, and the ability to Thread.interrupt() the read.
*/
import okio.Buffer
import com.fleeksoft.ksoup.ported.BufferReader
import com.fleeksoft.ksoup.ported.System
import kotlin.math.min
import okio.Buffer

internal class ConstrainableSource(
bufferReader: BufferReader,
maxSize: Int
) : BufferReader(bufferReader, maxSize) {
) : BufferReader(bufferReader) {

companion object {
private const val DEFAULT_SIZE = 1024 * 32
Expand Down Expand Up @@ -49,10 +48,11 @@ internal class ConstrainableSource(
val toRead = if (capped && byteCount > remaining) remaining else byteCount

return try {
val read = getActiveBuffer().read(
val calculatedByteCount: Int = if (this.size() > 0) this.size().toInt() else toRead
val read = getActiveSource().read(
sink = sink,
offset = 0,
byteCount = min(toRead, getActiveBuffer().size.toInt())
byteCount = calculatedByteCount
)
if (!this.exhausted()) {
remaining -= read
Expand All @@ -74,7 +74,7 @@ internal class ConstrainableSource(
val buffer = Buffer()

while (true) {
val size: Int = min(bufferSize, this.getActiveBuffer().size.toInt())
val size: Int = if (this.size() > 0) this.size().toInt() else bufferSize
val readBuffer = ByteArray(size)
read = this.read(readBuffer, 0, size)
if (read > 0) {
Expand Down
Loading

0 comments on commit 0047737

Please sign in to comment.