diff --git a/bundles/org.openhab.core.audio/src/main/java/org/openhab/core/audio/AudioFormat.java b/bundles/org.openhab.core.audio/src/main/java/org/openhab/core/audio/AudioFormat.java index 5f05b28cb..14d725966 100644 --- a/bundles/org.openhab.core.audio/src/main/java/org/openhab/core/audio/AudioFormat.java +++ b/bundles/org.openhab.core.audio/src/main/java/org/openhab/core/audio/AudioFormat.java @@ -287,22 +287,22 @@ public class AudioFormat { if (audioFormat == null) { return false; } - if ((null != getContainer()) && (!getContainer().equals(audioFormat.getContainer()))) { + if (getContainer() instanceof String container && !container.equals(audioFormat.getContainer())) { return false; } - if ((null != getCodec()) && (!getCodec().equals(audioFormat.getCodec()))) { + if (getCodec() instanceof String codec && !codec.equals(audioFormat.getCodec())) { return false; } - if ((null != isBigEndian()) && (!isBigEndian().equals(audioFormat.isBigEndian()))) { + if (isBigEndian() instanceof Boolean bigEndian && !bigEndian.equals(audioFormat.isBigEndian())) { return false; } - if ((null != getBitDepth()) && (!getBitDepth().equals(audioFormat.getBitDepth()))) { + if (getBitDepth() instanceof Integer bitDepth && !bitDepth.equals(audioFormat.getBitDepth())) { return false; } - if ((null != getBitRate()) && (!getBitRate().equals(audioFormat.getBitRate()))) { + if (getBitRate() instanceof Integer bitRate && !bitRate.equals(audioFormat.getBitRate())) { return false; } - if ((null != getFrequency()) && (!getFrequency().equals(audioFormat.getFrequency()))) { + if (getFrequency() instanceof Long frequency && !frequency.equals(audioFormat.getFrequency())) { return false; } return true; @@ -457,13 +457,13 @@ public class AudioFormat { public int hashCode() { final int prime = 31; int result = 1; - result = prime * result + ((bigEndian == null) ? 0 : bigEndian.hashCode()); - result = prime * result + ((bitDepth == null) ? 0 : bitDepth.hashCode()); - result = prime * result + ((bitRate == null) ? 0 : bitRate.hashCode()); - result = prime * result + ((codec == null) ? 0 : codec.hashCode()); - result = prime * result + ((container == null) ? 0 : container.hashCode()); - result = prime * result + ((frequency == null) ? 0 : frequency.hashCode()); - result = prime * result + ((channels == null) ? 0 : channels.hashCode()); + result = prime * result + (bigEndian instanceof Object localBigEndian ? localBigEndian.hashCode() : 0); + result = prime * result + (bitDepth instanceof Object localBitDepth ? localBitDepth.hashCode() : 0); + result = prime * result + (bitRate instanceof Object localBitRate ? localBitRate.hashCode() : 0); + result = prime * result + (codec instanceof Object localCodec ? localCodec.hashCode() : 0); + result = prime * result + (container instanceof Object localContainer ? localContainer.hashCode() : 0); + result = prime * result + (frequency instanceof Object localFrequency ? localFrequency.hashCode() : 0); + result = prime * result + (channels instanceof Object localChannels ? localChannels.hashCode() : 0); return result; } diff --git a/bundles/org.openhab.core.audio/src/main/java/org/openhab/core/audio/AudioHTTPServer.java b/bundles/org.openhab.core.audio/src/main/java/org/openhab/core/audio/AudioHTTPServer.java index 75900be83..8dd980635 100644 --- a/bundles/org.openhab.core.audio/src/main/java/org/openhab/core/audio/AudioHTTPServer.java +++ b/bundles/org.openhab.core.audio/src/main/java/org/openhab/core/audio/AudioHTTPServer.java @@ -13,14 +13,12 @@ package org.openhab.core.audio; import java.io.IOException; -import java.util.concurrent.CompletableFuture; import org.eclipse.jdt.annotation.NonNullByDefault; -import org.openhab.core.audio.internal.AudioServlet; /** - * This is an interface that is implemented by {@link AudioServlet} and which allows exposing audio streams through - * HTTP. + * This is an interface that is implemented by {@link org.openhab.core.audio.internal.AudioServlet} and which allows + * exposing audio streams through HTTP. * Streams are only served a single time and then discarded. * * @author Kai Kreuzer - Initial contribution @@ -67,7 +65,8 @@ public interface AudioHTTPServer { * to add the Clonable capability by storing it in a small memory buffer, e.g {@link ByteArrayAudioStream}, or in a * cached file if the stream reached the buffer capacity, or fails to render the sound completely if the stream is * too long. - * A {@link CompletableFuture} is used to inform the caller that the playback ends in order to clean + * A {@link java.util.concurrent.CompletableFuture} is used to inform the caller that the playback ends in order to + * clean * resources and run delayed task, such as restoring volume. * Streams are closed, once they expire. * diff --git a/bundles/org.openhab.core.audio/src/main/java/org/openhab/core/audio/URLAudioStream.java b/bundles/org.openhab.core.audio/src/main/java/org/openhab/core/audio/URLAudioStream.java index 2f5effda0..40a0d86ca 100644 --- a/bundles/org.openhab.core.audio/src/main/java/org/openhab/core/audio/URLAudioStream.java +++ b/bundles/org.openhab.core.audio/src/main/java/org/openhab/core/audio/URLAudioStream.java @@ -17,6 +17,8 @@ import java.io.InputStream; import java.io.OutputStream; import java.net.MalformedURLException; import java.net.Socket; +import java.net.URI; +import java.net.URISyntaxException; import java.net.URL; import java.net.URLConnection; import java.nio.charset.StandardCharsets; @@ -65,9 +67,10 @@ public class URLAudioStream extends AudioStream implements ClonableAudioStream { final String filename = url.toLowerCase(); final String extension = AudioStreamUtils.getExtension(filename); try { + URL streamUrl = new URI(url).toURL(); switch (extension) { case M3U_EXTENSION: - try (Scanner scanner = new Scanner(new URL(url).openStream(), StandardCharsets.UTF_8.name())) { + try (Scanner scanner = new Scanner(streamUrl.openStream(), StandardCharsets.UTF_8.name())) { while (true) { String line = scanner.nextLine(); if (!line.isEmpty() && !line.startsWith("#")) { @@ -80,7 +83,7 @@ public class URLAudioStream extends AudioStream implements ClonableAudioStream { } break; case PLS_EXTENSION: - try (Scanner scanner = new Scanner(new URL(url).openStream(), StandardCharsets.UTF_8.name())) { + try (Scanner scanner = new Scanner(streamUrl.openStream(), StandardCharsets.UTF_8.name())) { while (true) { String line = scanner.nextLine(); if (!line.isEmpty() && line.startsWith("File")) { @@ -98,7 +101,6 @@ public class URLAudioStream extends AudioStream implements ClonableAudioStream { default: break; } - URL streamUrl = new URL(url); URLConnection connection = streamUrl.openConnection(); if ("unknown/unknown".equals(connection.getContentType())) { // Java does not parse non-standard headers used by SHOUTCast @@ -119,7 +121,7 @@ public class URLAudioStream extends AudioStream implements ClonableAudioStream { // which opens a new connection and does not reuse the old one. return connection.getInputStream(); } - } catch (MalformedURLException e) { + } catch (MalformedURLException | URISyntaxException e) { logger.error("URL '{}' is not a valid url: {}", url, e.getMessage(), e); throw new AudioException("URL not valid"); } catch (IOException e) { @@ -145,8 +147,8 @@ public class URLAudioStream extends AudioStream implements ClonableAudioStream { @Override public void close() throws IOException { super.close(); - if (shoutCastSocket != null) { - shoutCastSocket.close(); + if (shoutCastSocket instanceof Socket socket) { + socket.close(); } } diff --git a/bundles/org.openhab.core.audio/src/main/java/org/openhab/core/audio/internal/AudioManagerImpl.java b/bundles/org.openhab.core.audio/src/main/java/org/openhab/core/audio/internal/AudioManagerImpl.java index d3300ad73..30d17b268 100644 --- a/bundles/org.openhab.core.audio/src/main/java/org/openhab/core/audio/internal/AudioManagerImpl.java +++ b/bundles/org.openhab.core.audio/src/main/java/org/openhab/core/audio/internal/AudioManagerImpl.java @@ -107,11 +107,8 @@ public class AudioManagerImpl implements AudioManager, ConfigOptionProvider { @Modified void modified(@Nullable Map config) { if (config != null) { - this.defaultSource = config.containsKey(CONFIG_DEFAULT_SOURCE) - ? config.get(CONFIG_DEFAULT_SOURCE).toString() - : null; - this.defaultSink = config.containsKey(CONFIG_DEFAULT_SINK) ? config.get(CONFIG_DEFAULT_SINK).toString() - : null; + this.defaultSource = config.get(CONFIG_DEFAULT_SOURCE) instanceof Object source ? source.toString() : null; + this.defaultSink = config.get(CONFIG_DEFAULT_SINK) instanceof Object sink ? sink.toString() : null; } } diff --git a/bundles/org.openhab.core.audio/src/main/java/org/openhab/core/audio/internal/javasound/JavaSoundAudioSink.java b/bundles/org.openhab.core.audio/src/main/java/org/openhab/core/audio/internal/javasound/JavaSoundAudioSink.java index ae173752e..301d0cdb8 100644 --- a/bundles/org.openhab.core.audio/src/main/java/org/openhab/core/audio/internal/javasound/JavaSoundAudioSink.java +++ b/bundles/org.openhab.core.audio/src/main/java/org/openhab/core/audio/internal/javasound/JavaSoundAudioSink.java @@ -106,9 +106,9 @@ public class JavaSoundAudioSink extends AudioSinkAsync { } else { if (audioStream == null || audioStream instanceof URLAudioStream) { // we are dealing with an infinite stream here - if (streamPlayer != null) { + if (streamPlayer instanceof Player player) { // if we are already playing a stream, stop it first - streamPlayer.close(); + player.close(); streamPlayer = null; } if (audioStream == null) { @@ -153,9 +153,9 @@ public class JavaSoundAudioSink extends AudioSinkAsync { } protected synchronized void deactivate() { - if (streamPlayer != null) { + if (streamPlayer instanceof Player player) { // stop playing streams on shutdown - streamPlayer.close(); + player.close(); streamPlayer = null; } }