Browse Source

[resources] Replace skipNBytes with custom skipBytes function (#5125)

The skipNBytes function requires API level 12.

Fixes [CMP-6543](https://youtrack.jetbrains.com/issue/CMP-6543)

## Release Notes
### Fixes - Resources
- _(prerelease fix)_ Fix resource reading on Java 11
pull/5126/head
Konstantin 3 months ago committed by GitHub
parent
commit
987968dcc2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 14
      components/resources/library/src/desktopMain/kotlin/org/jetbrains/compose/resources/ResourceReader.desktop.kt

14
components/resources/library/src/desktopMain/kotlin/org/jetbrains/compose/resources/ResourceReader.desktop.kt

@ -1,5 +1,7 @@
package org.jetbrains.compose.resources package org.jetbrains.compose.resources
import java.io.EOFException
import java.io.IOException
import java.io.InputStream import java.io.InputStream
internal actual fun getPlatformResourceReader(): ResourceReader = object : ResourceReader { internal actual fun getPlatformResourceReader(): ResourceReader = object : ResourceReader {
@ -12,12 +14,22 @@ internal actual fun getPlatformResourceReader(): ResourceReader = object : Resou
val resource = getResourceAsStream(path) val resource = getResourceAsStream(path)
val result = ByteArray(size.toInt()) val result = ByteArray(size.toInt())
resource.use { input -> resource.use { input ->
input.skipNBytes(offset) input.skipBytes(offset)
input.readNBytes(result, 0, size.toInt()) input.readNBytes(result, 0, size.toInt())
} }
return result return result
} }
//skipNBytes requires API 12
private fun InputStream.skipBytes(offset: Long) {
var skippedBytes = 0L
while (skippedBytes < offset) {
val count = skip(offset - skippedBytes)
if (count == 0L) break
skippedBytes += count
}
}
override fun getUri(path: String): String { override fun getUri(path: String): String {
val classLoader = getClassLoader() val classLoader = getClassLoader()
val resource = classLoader.getResource(path) ?: throw MissingResourceException(path) val resource = classLoader.getResource(path) ?: throw MissingResourceException(path)

Loading…
Cancel
Save