[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.
This commit is contained in:
Florian Schmaus 2021-03-25 14:58:44 +01:00
parent b58511d624
commit 33d735a42e
2 changed files with 29 additions and 2 deletions

View File

@ -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.
* <p>
* 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.".
* </p>
*
* @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() {

View File

@ -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<ClassLoader> classLoaders = getClassLoaders();