mirakle icon indicating copy to clipboard operation
mirakle copied to clipboard

fix for windows: refactored loadproperties function

Open LuigiVampa92 opened this issue 1 year ago • 0 comments

What problem does it solve?

Function load properties in utils.kt is used to read .properties files and acquire values from there into Properties object. The current implementation opens file stream, get the data from it but does not manually close it.

This is completely not a problem in Unix-like operation systems (Linux, MacOS), however Windows uses approach with blocking file handles, which means that while the process that requested an access to the file hadn't close the handle manually, no other process can perform any modifying operations with the file. And since Mirakle plugin is used by the gradle daemon that runs in background, file got blocked and the only way to get access to it again is to manually kill the gradle daemon process. In case you would like to delete some previously opened properties file or write something to it you will constantly receive an exception, and even commands like "del /f file" that are used to force-delete files will be able to do anything with it.

The current implementation, again, does not face any of these problems because it does not write anything to properties files multiple times, however I've got a fork of Mirakle that relies on properties files and sometimes needs to delete or create a new one, check if any of previous ones exist and delete them in this case. And also it is just a more decent approach to work with files - releasing the stream objects after the work is done with them.

Solution

The fix that I provided in this pull request does its job and properties files will now work properly in any OS including Windows. I know it looks ugly as a smoking pile, with all this try-try-try-check-if-null approach from good old Java 6 times, but it is actually the only solution that worked in my test environment (I tested it on Windows 10). For some reason when I tried to simplify the code in Kotlin it still wasn't able to close the file handle on Windows, so I did it the boomer-way

LuigiVampa92 avatar Apr 13 '23 17:04 LuigiVampa92