Skip to content

Commit

Permalink
Merge branch 'master' into renovate/macos-13.x
Browse files Browse the repository at this point in the history
  • Loading branch information
oldergod authored Sep 21, 2023
2 parents a4b1afd + 792fba4 commit 3548029
Show file tree
Hide file tree
Showing 13 changed files with 175 additions and 72 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Validate Gradle Wrapper
uses: gradle/wrapper-validation-action@v1
Expand All @@ -45,8 +45,8 @@ jobs:
emulator:
runs-on: macos-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-java@v3.12.0
- uses: actions/checkout@v4
- uses: actions/setup-java@v3.13.0
with:
distribution: 'zulu'
java-version: 19
Expand All @@ -66,7 +66,7 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Validate Gradle Wrapper
uses: gradle/wrapper-validation-action@v1
Expand All @@ -91,7 +91,7 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Validate Gradle Wrapper
uses: gradle/wrapper-validation-action@v1
Expand Down Expand Up @@ -120,7 +120,7 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Configure JDK
uses: actions/setup-java@v3
Expand All @@ -143,7 +143,7 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
fetch-depth: 0

Expand Down
2 changes: 1 addition & 1 deletion build-support/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ gradlePlugin {
}

dependencies {
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.0")
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.10")
}
4 changes: 3 additions & 1 deletion build-support/src/main/kotlin/platforms.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ fun KotlinMultiplatformExtension.configureOrCreateNativePlatforms() {
watchosSimulatorArm64()
// Required to generate tests tasks: https://youtrack.jetbrains.com/issue/KT-26547
linuxX64()
linuxArm64()
macosX64()
macosArm64()
mingwX64()
Expand All @@ -72,7 +73,8 @@ val mingwTargets = listOf(
)

val linuxTargets = listOf(
"linuxX64"
"linuxX64",
"linuxArm64",
)

val nativeTargets = appleTargets + linuxTargets + mingwTargets
Expand Down
8 changes: 4 additions & 4 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ androidx-test-runner = { module = "androidx.test:runner", version = "1.5.2" }
binaryCompatibilityValidator = { module = "org.jetbrains.kotlinx.binary-compatibility-validator:org.jetbrains.kotlinx.binary-compatibility-validator.gradle.plugin", version = "0.13.2" }
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test" }
kotlin-test-junit = { module = "org.jetbrains.kotlin:kotlin-test-junit" }
kotlin-time = { module = "org.jetbrains.kotlinx:kotlinx-datetime", version = "0.4.0" }
kotlin-time = { module = "org.jetbrains.kotlinx:kotlinx-datetime", version = "0.4.1" }
jmh-gradle-plugin = { module = "me.champeau.jmh:jmh-gradle-plugin", version = "0.7.1" }
jmh-core = { module = "org.openjdk.jmh:jmh-core", version.ref = "jmh" }
jmh-generator = { module = "org.openjdk.jmh:jmh-generator-annprocess", version.ref = "jmh" }
dokka = { module = "org.jetbrains.dokka:dokka-gradle-plugin", version = "1.8.20" }
spotless = { module = "com.diffplug.spotless:spotless-plugin-gradle", version = "6.20.0" }
dokka = { module = "org.jetbrains.dokka:dokka-gradle-plugin", version = "1.9.0" }
spotless = { module = "com.diffplug.spotless:spotless-plugin-gradle", version = "6.21.0" }
bnd = { module = "biz.aQute.bnd:biz.aQute.bnd.gradle", version = "6.4.0" }
vanniktech-publish-plugin = { module = "com.vanniktech:gradle-maven-publish-plugin", version = "0.25.3" }
test-junit = { module = "junit:junit", version = "4.13.2" }
test-assertj = { module = "org.assertj:assertj-core", version = "3.24.2" }
test-assertk = "com.willowtreeapps.assertk:assertk:0.26.1"
test-assertk = "com.willowtreeapps.assertk:assertk:0.27.0"
test-jimfs = "com.google.jimfs:jimfs:1.3.0"
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
3 changes: 2 additions & 1 deletion gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ done
# This is normally unused
# shellcheck disable=SC2034
APP_BASE_NAME=${0##*/}
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
Expand Down
3 changes: 2 additions & 1 deletion okio/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ plugins {
* | | |-- watchosArm64
* | | '-- watchosX86
* | '-- linux
* | '-- linuxX64
* | |-- linuxX64
* | '-- linuxArm64
* '-- wasm
* ```
*
Expand Down
13 changes: 8 additions & 5 deletions okio/src/jvmMain/kotlin/okio/DeflaterSink.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

package okio

import java.io.IOException
import java.util.zip.Deflater

/**
Expand Down Expand Up @@ -83,10 +82,14 @@ internal constructor(private val sink: BufferedSink, private val deflater: Defla
// Java 1.7, and is public (although with @hide) on Android since 2.3.
// The @hide tag means that this code won't compile against the Android
// 2.3 SDK, but it will run fine there.
val deflated = if (syncFlush) {
deflater.deflate(s.data, s.limit, Segment.SIZE - s.limit, Deflater.SYNC_FLUSH)
} else {
deflater.deflate(s.data, s.limit, Segment.SIZE - s.limit)
val deflated = try {
if (syncFlush) {
deflater.deflate(s.data, s.limit, Segment.SIZE - s.limit, Deflater.SYNC_FLUSH)
} else {
deflater.deflate(s.data, s.limit, Segment.SIZE - s.limit)
}
} catch (npe: NullPointerException) {
throw IOException("Deflater already closed", npe)
}

if (deflated > 0) {
Expand Down
98 changes: 48 additions & 50 deletions okio/src/jvmMain/kotlin/okio/internal/ResourceFileSystem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import okio.Path.Companion.toOkioPath
import okio.Path.Companion.toPath
import okio.Sink
import okio.Source
import okio.source

/**
* A file system exposing Java classpath resources. It is equivalent to the files returned by
Expand All @@ -41,8 +42,9 @@ import okio.Source
* This file system is read-only.
*/
internal class ResourceFileSystem internal constructor(
classLoader: ClassLoader,
private val classLoader: ClassLoader,
indexEagerly: Boolean,
private val systemFileSystem: FileSystem = SYSTEM,
) : FileSystem() {
private val roots: List<Pair<FileSystem, Path>> by lazy { classLoader.toClasspathRoots() }

Expand Down Expand Up @@ -122,14 +124,10 @@ internal class ResourceFileSystem internal constructor(

override fun source(file: Path): Source {
if (!keepPath(file)) throw FileNotFoundException("file not found: $file")
val relativePath = file.toRelativePath()
for ((fileSystem, base) in roots) {
try {
return fileSystem.source(base / relativePath)
} catch (_: FileNotFoundException) {
}
}
throw FileNotFoundException("file not found: $file")
// Make sure we have a path that doesn't start with '/'.
val relativePath = ROOT.resolve(file).relativeTo(ROOT)
return classLoader.getResourceAsStream(relativePath.toString())?.source()
?: throw FileNotFoundException("file not found: $file")
}

override fun sink(file: Path, mustCreate: Boolean): Sink {
Expand Down Expand Up @@ -157,6 +155,47 @@ internal class ResourceFileSystem internal constructor(
return canonicalThis.relativeTo(ROOT).toString()
}

/**
* Returns a search path of classpath roots. Each element contains a file system to use, and
* the base directory of that file system to search from.
*/
private fun ClassLoader.toClasspathRoots(): List<Pair<FileSystem, Path>> {
// We'd like to build this upon an API like ClassLoader.getURLs() but unfortunately that
// API exists only on URLClassLoader (and that isn't the default class loader implementation).
//
// The closest we have is `ClassLoader.getResources("")`. It returns all classpath roots that
// are directories but none that are .jar files. To mitigate that we also search for all
// `META-INF/MANIFEST.MF` files, hastily assuming that every .jar file will have such an
// entry.
//
// Classpath entries that aren't directories and don't have a META-INF/MANIFEST.MF file will
// not be visible in this file system.
return getResources("").toList().mapNotNull { it.toFileRoot() } +
getResources("META-INF/MANIFEST.MF").toList().mapNotNull { it.toJarRoot() }
}

private fun URL.toFileRoot(): Pair<FileSystem, Path>? {
if (protocol != "file") return null // Ignore unexpected URLs.
return systemFileSystem to File(toURI()).toOkioPath()
}

private fun URL.toJarRoot(): Pair<FileSystem, Path>? {
val urlString = toString()
if (!urlString.startsWith("jar:file:")) return null // Ignore unexpected URLs.

// Given a URL like `jar:file:/tmp/foo.jar!/META-INF/MANIFEST.MF`, get the path to the archive
// file, like `/tmp/foo.jar`.
val suffixStart = urlString.lastIndexOf("!")
if (suffixStart == -1) return null
val path = File(URI.create(urlString.substring("jar:".length, suffixStart))).toOkioPath()
val zip = openZip(
zipPath = path,
fileSystem = systemFileSystem,
predicate = { entry -> keepPath(entry.canonicalPath) },
)
return zip to ROOT
}

private companion object {
val ROOT = "/".toPath()

Expand All @@ -165,47 +204,6 @@ internal class ResourceFileSystem internal constructor(
return ROOT / (toString().removePrefix(prefix).replace('\\', '/'))
}

/**
* Returns a search path of classpath roots. Each element contains a file system to use, and
* the base directory of that file system to search from.
*/
fun ClassLoader.toClasspathRoots(): List<Pair<FileSystem, Path>> {
// We'd like to build this upon an API like ClassLoader.getURLs() but unfortunately that
// API exists only on URLClassLoader (and that isn't the default class loader implementation).
//
// The closest we have is `ClassLoader.getResources("")`. It returns all classpath roots that
// are directories but none that are .jar files. To mitigate that we also search for all
// `META-INF/MANIFEST.MF` files, hastily assuming that every .jar file will have such an
// entry.
//
// Classpath entries that aren't directories and don't have a META-INF/MANIFEST.MF file will
// not be visible in this file system.
return getResources("").toList().mapNotNull { it.toFileRoot() } +
getResources("META-INF/MANIFEST.MF").toList().mapNotNull { it.toJarRoot() }
}

fun URL.toFileRoot(): Pair<FileSystem, Path>? {
if (protocol != "file") return null // Ignore unexpected URLs.
return SYSTEM to File(toURI()).toOkioPath()
}

fun URL.toJarRoot(): Pair<FileSystem, Path>? {
val urlString = toString()
if (!urlString.startsWith("jar:file:")) return null // Ignore unexpected URLs.

// Given a URL like `jar:file:/tmp/foo.jar!/META-INF/MANIFEST.MF`, get the path to the archive
// file, like `/tmp/foo.jar`.
val suffixStart = urlString.lastIndexOf("!")
if (suffixStart == -1) return null
val path = File(URI.create(urlString.substring("jar:".length, suffixStart))).toOkioPath()
val zip = openZip(
zipPath = path,
fileSystem = SYSTEM,
predicate = { entry -> keepPath(entry.canonicalPath) },
)
return zip to ROOT
}

private fun keepPath(path: Path) = !path.name.endsWith(".class", ignoreCase = true)
}
}
23 changes: 22 additions & 1 deletion okio/src/jvmTest/kotlin/okio/DeflaterSinkTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package okio

import java.io.IOException
import java.util.zip.Deflater
import java.util.zip.Inflater
import java.util.zip.InflaterInputStream
Expand Down Expand Up @@ -129,6 +128,28 @@ class DeflaterSinkTest {
mockSink.assertLogContains("close()")
}

/**
* This test confirms that we swallow NullPointerException from Deflater and
* rethrow as an IOException.
*/
@Test
fun rethrowNullPointerAsIOException() {
val deflater = Deflater()
// Close to cause a NullPointerException
deflater.end()

val data = Buffer().apply {
writeUtf8("They're moving in herds. They do move in herds.")
}
val deflaterSink = DeflaterSink(Buffer(), deflater)

val ioe = Assert.assertThrows("", IOException::class.java) {
deflaterSink.write(data, data.size)
}

Assert.assertTrue(ioe.cause is NullPointerException)
}

/**
* Uses streaming decompression to inflate `deflated`. The input must
* either be finished or have a trailing sync flush.
Expand Down
Loading

0 comments on commit 3548029

Please sign in to comment.