1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-09-10 01:29:38 +02:00

Use try-with-resources where possible

and make StanzaCollector implement AutoCloseable.
This commit is contained in:
Florian Schmaus 2019-02-04 09:36:37 +01:00
parent e98d42790a
commit 658fd08d20
10 changed files with 30 additions and 69 deletions

View file

@ -41,7 +41,7 @@ import org.jivesoftware.smack.packet.Stanza;
* @see XMPPConnection#createStanzaCollector(StanzaFilter)
* @author Matt Tucker
*/
public class StanzaCollector {
public class StanzaCollector implements AutoCloseable {
private final StanzaFilter packetFilter;
@ -92,6 +92,10 @@ public class StanzaCollector {
cancelled = true;
connection.removeStanzaCollector(this);
notifyAll();
if (collectorToReset != null) {
collectorToReset.cancel();
}
}
/**
@ -431,4 +435,9 @@ public class StanzaCollector {
}
}
@Override
public void close() {
cancel();
}
}

View file

@ -26,7 +26,6 @@ import java.util.logging.Logger;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.util.CloseableUtil;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
@ -54,10 +53,10 @@ public class ProviderFileLoader implements ProviderLoader {
@SuppressWarnings("unchecked")
public ProviderFileLoader(InputStream providerStream, ClassLoader classLoader) {
// Load processing providers.
try {
try (InputStream is = providerStream) {
XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
parser.setInput(providerStream, "UTF-8");
parser.setInput(is, "UTF-8");
int eventType = parser.getEventType();
do {
if (eventType == XmlPullParser.START_TAG) {
@ -141,9 +140,6 @@ public class ProviderFileLoader implements ProviderLoader {
LOGGER.log(Level.SEVERE, "Unknown error occurred while parsing provider file", e);
exceptions.add(e);
}
finally {
CloseableUtil.maybeClose(providerStream, LOGGER);
}
}
@Override

View file

@ -113,9 +113,7 @@ public final class FileUtils {
*/
@SuppressWarnings("DefaultCharset")
public static String readFileOrThrow(File file) throws IOException {
Reader reader = null;
try {
reader = new FileReader(file);
try (Reader reader = new FileReader(file)) {
char[] buf = new char[8192];
int len;
StringBuilder s = new StringBuilder();
@ -124,11 +122,6 @@ public final class FileUtils {
}
return s.toString();
}
finally {
if (reader != null) {
reader.close();
}
}
}
public static String readFile(File file) {