From 3827872c2051275ecdeab1e0ba1eabb140027f0d Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Sat, 11 Jan 2025 14:21:35 +0100 Subject: [PATCH] Refactor usages of deprecated methods (#18082) Signed-off-by: Jacob Laursen --- .../binding/lgwebos/internal/WakeOnLanUtility.java | 14 +++++++++++++- .../lgwebos/internal/action/LGWebOSActions.java | 12 +++++++----- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/WakeOnLanUtility.java b/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/WakeOnLanUtility.java index 14fd4930ce89d..7bb9753bf3ea8 100644 --- a/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/WakeOnLanUtility.java +++ b/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/WakeOnLanUtility.java @@ -12,7 +12,9 @@ */ package org.openhab.binding.lgwebos.internal; +import java.io.BufferedReader; import java.io.IOException; +import java.io.InputStreamReader; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; @@ -23,6 +25,7 @@ import java.util.Objects; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Collectors; import java.util.stream.Stream; import org.eclipse.jdt.annotation.NonNullByDefault; @@ -170,7 +173,16 @@ private static byte[] getWOLPackage(String macStr) throws IllegalArgumentExcepti private static boolean checkIfLinuxCommandExists(String cmd) { try { - return 0 == Runtime.getRuntime().exec(String.format("which %s", cmd)).waitFor(); + Process process = new ProcessBuilder("which", cmd).redirectErrorStream(true).start(); + + if (LOGGER.isDebugEnabled()) { + try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) { + String output = reader.lines().collect(Collectors.joining("\n")); + LOGGER.debug("Command 'which {}' returned {}", cmd, output); + } + } + + return process.waitFor() == 0; } catch (InterruptedException | IOException e) { LOGGER.debug("Error trying to check if command {} exists: {}", cmd, e.getMessage()); } diff --git a/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/action/LGWebOSActions.java b/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/action/LGWebOSActions.java index 033d585adbb67..b1a5e17b85b76 100644 --- a/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/action/LGWebOSActions.java +++ b/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/action/LGWebOSActions.java @@ -16,7 +16,8 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; -import java.net.URL; +import java.net.URI; +import java.net.URISyntaxException; import java.nio.charset.StandardCharsets; import java.util.Base64; import java.util.Collections; @@ -100,11 +101,11 @@ public void showToast( public void showToast( @ActionInput(name = "icon", label = "@text/actionShowToastInputIconLabel", description = "@text/actionShowToastInputIconDesc") String icon, @ActionInput(name = "text", label = "@text/actionShowToastInputTextLabel", description = "@text/actionShowToastInputTextDesc") String text) - throws IOException { - BufferedImage bi = ImageIO.read(new URL(icon)); + throws IOException, URISyntaxException { + BufferedImage bi = ImageIO.read(new URI(icon).toURL()); try (ByteArrayOutputStream os = new ByteArrayOutputStream(); OutputStream b64 = Base64.getEncoder().wrap(os)) { ImageIO.write(bi, "png", b64); - String string = os.toString(StandardCharsets.UTF_8.name()); + String string = os.toString(StandardCharsets.UTF_8); getConnectedSocket().ifPresent(control -> control.showToast(text, string, "png", createResponseListener())); } } @@ -261,7 +262,8 @@ public static void showToast(ThingActions actions, String text) throws IOExcepti ((LGWebOSActions) actions).showToast(text); } - public static void showToast(ThingActions actions, String icon, String text) throws IOException { + public static void showToast(ThingActions actions, String icon, String text) + throws IOException, URISyntaxException { ((LGWebOSActions) actions).showToast(icon, text); }