Commit 24f1cecb authored by Alexey Tsvetkov's avatar Alexey Tsvetkov

Convert org.jetbrains.skiko.Library to Kotlin

parent 691a0f58
package org.jetbrains.skiko; package org.jetbrains.skiko
import java.io.File; import java.io.File
import java.io.InputStream; import java.io.IOException
import java.io.IOException; import java.net.URISyntaxException
import java.net.URL; import java.nio.file.Files
import java.net.URISyntaxException; import java.nio.file.StandardCopyOption
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
// based on https://github.com/JetBrains/skija/blob/bc6e0531021e4ff839c07196e0336d6456ed7520/src/main/java/org/jetbrains/skija/Library.java // based on https://github.com/JetBrains/skija/blob/bc6e0531021e4ff839c07196e0336d6456ed7520/src/main/java/org/jetbrains/skija/Library.java
public class Library { object Library {
public static boolean _loaded = false; var _loaded = false
// https://github.com/adamheinrich/native-utils/blob/e6a39489662846a77504634b6fafa4995ede3b1d/src/main/java/cz/adamh/utils/NativeUtils.java // https://github.com/adamheinrich/native-utils/blob/e6a39489662846a77504634b6fafa4995ede3b1d/src/main/java/cz/adamh/utils/NativeUtils.java
public static void load(String resourcePath, String name) { fun load(resourcePath: String, name: String) {
if (_loaded) return; if (_loaded) return
try {
File file;
String fileName = _getPrefix() + name + "." + _getExtension();
URL url = Library.class.getResource(resourcePath + fileName);
if (url == null)
throw new IllegalArgumentException("Library " + fileName + " is not found in " + resourcePath);
else if (url.getProtocol() == "file")
try { try {
var file: File
val fileName = _getPrefix() + name + "." + _getExtension()
val url = Library::class.java.getResource(resourcePath + fileName)
// System.out.println("Loading " + url); // System.out.println("Loading " + url);
file = new File(url.toURI()); requireNotNull(url) { "Library $fileName is not found in $resourcePath" }
} catch (URISyntaxException e){ if (url.protocol === "file") file = try {
throw new RuntimeException(e); // System.out.println("Loading " + url);
} File(url.toURI())
else } catch (e: URISyntaxException) {
try (InputStream is = url.openStream()) { throw RuntimeException(e)
File tempDir = new File(System.getProperty("java.io.tmpdir"), "skija_" + System.nanoTime()); } else url.openStream().use { `is` ->
tempDir.mkdirs(); val tempDir = File(System.getProperty("java.io.tmpdir"), "skija_" + System.nanoTime())
tempDir.deleteOnExit(); tempDir.mkdirs()
file = new File(tempDir, fileName); tempDir.deleteOnExit()
file.deleteOnExit(); file = File(tempDir, fileName)
Files.copy(is, file.toPath(), StandardCopyOption.REPLACE_EXISTING); file.deleteOnExit()
// System.out.println("Loading " + url + " from " + file); Files.copy(`is`, file.toPath(), StandardCopyOption.REPLACE_EXISTING)
} }
System.load(file.absolutePath)
System.load(file.getAbsolutePath()); _loaded = true
_loaded = true; } catch (e: IOException) {
} catch (IOException e) { throw RuntimeException(e)
throw new RuntimeException(e);
} }
} }
public static String _getPrefix() { fun _getPrefix(): String {
String os = System.getProperty("os.name"); val os = System.getProperty("os.name")
String lowerCaseOs = os.toLowerCase(); val lowerCaseOs = os.toLowerCase()
if (lowerCaseOs.contains("windows")) return ""; return if (lowerCaseOs.contains("windows")) "" else "lib"
return "lib";
} }
public static String _getExtension() { fun _getExtension(): String {
String os = System.getProperty("os.name"); val os = System.getProperty("os.name") ?: throw RuntimeException("Unknown operation system")
if (os == null) throw new RuntimeException("Unknown operation system"); val lowerCaseOs = os.toLowerCase()
String lowerCaseOs = os.toLowerCase(); if (lowerCaseOs.contains("mac") || lowerCaseOs.contains("darwin")) return "dylib"
if (lowerCaseOs.contains("mac") || lowerCaseOs.contains("darwin")) return "dylib"; if (lowerCaseOs.contains("windows")) return "dll"
if (lowerCaseOs.contains("windows")) return "dll"; if (lowerCaseOs.contains("nux")) return "so"
if (lowerCaseOs.contains("nux")) return "so"; throw RuntimeException("Unknown operation system: $os")
throw new RuntimeException("Unknown operation system: " + os);
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment