From 33d735a42e48dc3b0aa46e9eb4b5995bf639d8af Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Thu, 25 Mar 2021 14:58:44 +0100 Subject: [PATCH] [core] Make Smack.getNoticeStream() more robust Appearently simply calling ClassLoader.getSystemResourceAsStream() works on Java SE but not on Android. But our FileUtils are able to load the resource stream on Android. --- .../java/org/jivesoftware/smack/Smack.java | 18 ++++++++++++++++-- .../org/jivesoftware/smack/util/FileUtils.java | 13 +++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/smack-core/src/main/java/org/jivesoftware/smack/Smack.java b/smack-core/src/main/java/org/jivesoftware/smack/Smack.java index 690b9bb2d..6608bba4b 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/Smack.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/Smack.java @@ -1,6 +1,6 @@ /** * - * Copyright 2020 Florian Schmaus + * Copyright 2020-2021 Florian Schmaus * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,6 +19,8 @@ package org.jivesoftware.smack; import java.io.InputStream; import java.util.logging.Logger; +import org.jivesoftware.smack.util.FileUtils; + public class Smack { private static final Logger LOGGER = Logger.getLogger(Smack.class.getName()); @@ -38,8 +40,20 @@ public class Smack { private static final String NOTICE_RESOURCE = SMACK_PACKAGE + "/NOTICE"; + /** + * Get the stream of the NOTICE file of Smack. + *

+ * This license of Smack requires that the contents of this NOTICE text file are shown "…within a display generated by + * the Derivative Works, if and wherever such third-party notices normally appear.". + *

+ * + * @return the stream of the NOTICE file of Smack. + * @since 4.4.0 + */ public static InputStream getNoticeStream() { - return ClassLoader.getSystemResourceAsStream(NOTICE_RESOURCE); + InputStream res = FileUtils.getInputStreamForClasspathFile(NOTICE_RESOURCE); + assert res != null; + return res; } public static void ensureInitialized() { diff --git a/smack-core/src/main/java/org/jivesoftware/smack/util/FileUtils.java b/smack-core/src/main/java/org/jivesoftware/smack/util/FileUtils.java index 2cdac90a4..7cb4bd0d9 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/util/FileUtils.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/util/FileUtils.java @@ -41,6 +41,19 @@ public final class FileUtils { private static final Logger LOGGER = Logger.getLogger(FileUtils.class.getName()); + public static InputStream getInputStreamForClasspathFile(String path) { + return getInputStreamForClasspathFile(path, null); + } + + public static InputStream getInputStreamForClasspathFile(String path, ClassLoader loader) { + try { + return getStreamForClasspathFile(path, loader); + } catch (IOException e) { + LOGGER.log(Level.FINE, "Suppressed IOException in getInputStreamForClasspathFile", e); + return null; + } + } + public static InputStream getStreamForClasspathFile(String path, ClassLoader loader) throws IOException { // Get an array of class loaders to try loading the providers files from. List classLoaders = getClassLoaders();