Skip to content

Commit

Permalink
fix some dangling open file handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
Bixilon committed Aug 3, 2024
1 parent d3a7e04 commit a8f7754
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
9 changes: 9 additions & 0 deletions src/main/java/de/bixilon/minosoft/assets/util/FileUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package de.bixilon.minosoft.assets.util

import com.github.luben.zstd.ZstdInputStream
import de.bixilon.minosoft.terminal.RunConfiguration
import javafx.scene.image.Image
import java.io.*
import java.nio.file.Files
import java.nio.file.Path
Expand Down Expand Up @@ -60,4 +61,12 @@ object FileUtil {
fun createTempFile(): File {
return Files.createTempFile(RunConfiguration.TEMPORARY_FOLDER, "", "").toFile()
}

fun InputStream.readImage(close: Boolean = true): Image {
val image = Image(this)
if (close) {
close()
}
return image
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Minosoft
* Copyright (C) 2020-2023 Moritz Zwerger
* Copyright (C) 2020-2024 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
Expand All @@ -18,6 +18,7 @@ import de.bixilon.kutil.hash.HashUtil.sha256
import de.bixilon.minosoft.assets.util.FileAssetsTypes
import de.bixilon.minosoft.assets.util.FileUtil
import de.bixilon.minosoft.assets.util.FileUtil.mkdirParent
import de.bixilon.minosoft.assets.util.FileUtil.readImage
import de.bixilon.minosoft.assets.util.PathUtil
import de.bixilon.minosoft.config.profile.profiles.eros.server.entries.AbstractServer
import javafx.scene.image.Image
Expand All @@ -29,7 +30,7 @@ object FaviconManager {
get() {
val hash = this.faviconHash ?: return null
try {
return Image(FileUtil.readFile(PathUtil.getAssetsPath(hash = hash, type = FileAssetsTypes.FAVICON)))
return FileUtil.readFile(PathUtil.getAssetsPath(hash = hash, type = FileAssetsTypes.FAVICON)).readImage()
} catch (exception: Throwable) {
this.faviconHash = null
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Minosoft
* Copyright (C) 2020-2023 Moritz Zwerger
* Copyright (C) 2020-2024 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
Expand All @@ -19,14 +19,14 @@ import de.bixilon.kutil.exception.ExceptionUtil.catchAll
import de.bixilon.kutil.latch.SimpleLatch
import de.bixilon.kutil.shutdown.ShutdownManager
import de.bixilon.minosoft.assets.IntegratedAssets
import de.bixilon.minosoft.assets.util.FileUtil.readImage
import de.bixilon.minosoft.gui.eros.crash.ErosCrashReport.Companion.crash
import de.bixilon.minosoft.util.logging.Log
import de.bixilon.minosoft.util.logging.LogLevels
import de.bixilon.minosoft.util.logging.LogMessageType
import de.bixilon.minosoft.util.system.SystemUtil
import javafx.application.Application
import javafx.application.Platform
import javafx.scene.image.Image
import javafx.stage.Stage


Expand All @@ -39,7 +39,7 @@ class JavaFXInitializer internal constructor() : Application() {
JavaFXUtil.HOST_SERVICES = hostServices
SystemUtil.api = JavaFXSystemAPI()

JavaFXUtil.MINOSOFT_LOGO = Image(IntegratedAssets.DEFAULT[SystemUtil.ICON])
JavaFXUtil.MINOSOFT_LOGO = IntegratedAssets.DEFAULT[SystemUtil.ICON].readImage()

Log.log(LogMessageType.JAVAFX, LogLevels.VERBOSE) { "Initialized JavaFX Toolkit!" }
LATCH.dec()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,11 @@ interface BaseWindow {


fun setDefaultIcon(assetsManager: AssetsManager) {
val decoder = PNGDecoder(assetsManager[SystemUtil.ICON])
val stream = assetsManager[SystemUtil.ICON]
val decoder = PNGDecoder(stream)
val data = BufferUtils.createByteBuffer(decoder.width * decoder.height * PNGDecoder.Format.RGBA.numComponents)
decoder.decode(data, decoder.width * PNGDecoder.Format.RGBA.numComponents, PNGDecoder.Format.RGBA)
stream.close()
data.position(0)
data.limit(data.capacity())
setIcon(Vec2i(decoder.width, decoder.height), data)
Expand Down

0 comments on commit a8f7754

Please # to comment.