diff --git a/smack-core/src/main/java/org/jivesoftware/smack/StanzaCollector.java b/smack-core/src/main/java/org/jivesoftware/smack/StanzaCollector.java index 3a2f9a414..800c84406 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/StanzaCollector.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/StanzaCollector.java @@ -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(); + } + } diff --git a/smack-core/src/main/java/org/jivesoftware/smack/provider/ProviderFileLoader.java b/smack-core/src/main/java/org/jivesoftware/smack/provider/ProviderFileLoader.java index a3c9fec17..c8b879306 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/provider/ProviderFileLoader.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/provider/ProviderFileLoader.java @@ -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 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 a470a6005..8fc4bcd7e 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 @@ -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) { diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/iot/data/IoTDataManager.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/iot/data/IoTDataManager.java index abcb1a649..f686847e2 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/iot/data/IoTDataManager.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/iot/data/IoTDataManager.java @@ -192,9 +192,9 @@ public final class IoTDataManager extends IoTManager { doneCollector.nextResult(); } finally { - // Ensure that the two collectors are canceled in any case. + // Canceling dataCollector will also cancel the doneCollector since it is configured as dataCollector's + // collector to reset. dataCollector.cancel(); - doneCollector.cancel(); } int collectedCount = dataCollector.getCollectedCount(); diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/MamManager.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/MamManager.java index 76c1ee442..3e3e52a22 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/MamManager.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/MamManager.java @@ -546,17 +546,15 @@ public final class MamManager extends Manager { StanzaCollector.Configuration resultCollectorConfiguration = StanzaCollector.newConfiguration() .setStanzaFilter(new MamResultFilter(mamQueryIq)).setCollectorToReset(mamFinIQCollector); - StanzaCollector resultCollector = connection.createStanzaCollector(resultCollectorConfiguration); - try { + StanzaCollector cancelledResultCollector; + try (StanzaCollector resultCollector = connection.createStanzaCollector(resultCollectorConfiguration)) { connection.sendStanza(mamQueryIq); mamFinIQ = mamFinIQCollector.nextResultOrThrow(); - } finally { - mamFinIQCollector.cancel(); - resultCollector.cancel(); + cancelledResultCollector = resultCollector; } - return new MamQueryPage(resultCollector, mamFinIQ); + return new MamQueryPage(cancelledResultCollector, mamFinIQ); } /** diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/caps/cache/SimpleDirectoryPersistentCache.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/caps/cache/SimpleDirectoryPersistentCache.java index da9ff22c9..18ce9b8ac 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/caps/cache/SimpleDirectoryPersistentCache.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/caps/cache/SimpleDirectoryPersistentCache.java @@ -1,6 +1,6 @@ /** * - * Copyright © 2011-2018 Florian Schmaus + * Copyright © 2011-2019 Florian Schmaus * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -130,11 +130,8 @@ public class SimpleDirectoryPersistentCache implements EntityCapsPersistentCache * @throws IOException */ private static void writeInfoToFile(File file, DiscoverInfo info) throws IOException { - DataOutputStream dos = new DataOutputStream(new FileOutputStream(file)); - try { + try (DataOutputStream dos = new DataOutputStream(new FileOutputStream(file))) { dos.writeUTF(info.toXML(null).toString()); - } finally { - dos.close(); } } @@ -146,12 +143,9 @@ public class SimpleDirectoryPersistentCache implements EntityCapsPersistentCache * @throws Exception */ private static DiscoverInfo restoreInfoFromFile(File file) throws Exception { - DataInputStream dis = new DataInputStream(new FileInputStream(file)); String fileContent; - try { + try (DataInputStream dis = new DataInputStream(new FileInputStream(file))) { fileContent = dis.readUTF(); - } finally { - dis.close(); } if (fileContent == null) { return null; diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/jiveproperties/packet/JivePropertiesExtension.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/jiveproperties/packet/JivePropertiesExtension.java index 2d4f02242..6c80456d6 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/jiveproperties/packet/JivePropertiesExtension.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/jiveproperties/packet/JivePropertiesExtension.java @@ -29,7 +29,6 @@ import java.util.logging.Logger; import org.jivesoftware.smack.packet.ExtensionElement; import org.jivesoftware.smack.packet.Message; -import org.jivesoftware.smack.util.CloseableUtil; import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smack.util.stringencoder.Base64; @@ -177,11 +176,10 @@ public class JivePropertiesExtension implements ExtensionElement { // a binary format, which won't work well inside of XML. Therefore, we base-64 // encode the binary data before adding it. else { - ByteArrayOutputStream byteStream = null; - ObjectOutputStream out = null; - try { - byteStream = new ByteArrayOutputStream(); - out = new ObjectOutputStream(byteStream); + try ( + ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); + ObjectOutputStream out = new ObjectOutputStream(byteStream); + ) { out.writeObject(value); type = "java-object"; valueStr = Base64.encodeToString(byteStream.toByteArray()); @@ -191,10 +189,6 @@ public class JivePropertiesExtension implements ExtensionElement { type = "java-object"; valueStr = "Serializing error: " + e.getMessage(); } - finally { - CloseableUtil.maybeClose(out, LOGGER); - CloseableUtil.maybeClose(byteStream, LOGGER); - } } xml.attribute("type", type); xml.rightAngleBracket(); diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/offline/OfflineMessageManager.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/offline/OfflineMessageManager.java index 9616df194..8b579bb11 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/offline/OfflineMessageManager.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/offline/OfflineMessageManager.java @@ -165,8 +165,7 @@ public class OfflineMessageManager { } }); int pendingNodes = nodes.size(); - StanzaCollector messageCollector = connection.createStanzaCollector(messageFilter); - try { + try (StanzaCollector messageCollector = connection.createStanzaCollector(messageFilter)) { connection.createStanzaCollectorAndSend(request).nextResultOrThrow(); // Collect the received offline messages Message message; @@ -181,10 +180,6 @@ public class OfflineMessageManager { } } while (message != null && pendingNodes > 0); } - finally { - // Stop queuing offline messages - messageCollector.cancel(); - } return messages; } @@ -206,10 +201,9 @@ public class OfflineMessageManager { StanzaCollector resultCollector = connection.createStanzaCollectorAndSend(request); StanzaCollector.Configuration messageCollectorConfiguration = StanzaCollector.newConfiguration().setStanzaFilter(PACKET_FILTER).setCollectorToReset(resultCollector); - StanzaCollector messageCollector = connection.createStanzaCollector(messageCollectorConfiguration); List messages; - try { + try (StanzaCollector messageCollector = connection.createStanzaCollector(messageCollectorConfiguration)) { resultCollector.nextResultOrThrow(); // Be extra safe, cancel the message collector right here so that it does not collector // other messages that eventually match (although I've no idea how this could happen in @@ -221,11 +215,6 @@ public class OfflineMessageManager { messages.add(message); } } - finally { - // Ensure that the message collector is canceled even if nextResultOrThrow threw. It - // doesn't matter if we cancel the message collector twice - messageCollector.cancel(); - } return messages; } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/vcardtemp/packet/VCard.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/vcardtemp/packet/VCard.java index 014e7d366..452b5a529 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/vcardtemp/packet/VCard.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/vcardtemp/packet/VCard.java @@ -491,9 +491,7 @@ public final class VCard extends IQ { } private static byte[] getFileBytes(File file) throws IOException { - BufferedInputStream bis = null; - try { - bis = new BufferedInputStream(new FileInputStream(file)); + try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) { int bytes = (int) file.length(); byte[] buffer = new byte[bytes]; int readBytes = bis.read(buffer); @@ -502,11 +500,6 @@ public final class VCard extends IQ { } return buffer; } - finally { - if (bis != null) { - bis.close(); - } - } } /** diff --git a/smack-integration-test/src/main/java/org/igniterealtime/smack/inttest/AbstractSmackIntTest.java b/smack-integration-test/src/main/java/org/igniterealtime/smack/inttest/AbstractSmackIntTest.java index c96f090a8..dd1a7b194 100644 --- a/smack-integration-test/src/main/java/org/igniterealtime/smack/inttest/AbstractSmackIntTest.java +++ b/smack-integration-test/src/main/java/org/igniterealtime/smack/inttest/AbstractSmackIntTest.java @@ -55,15 +55,10 @@ public abstract class AbstractSmackIntTest { throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { StanzaCollector.Configuration configuration = StanzaCollector.newConfiguration().setStanzaFilter( filter).setSize(1); - StanzaCollector collector = connection.createStanzaCollector(configuration); - - try { + try (StanzaCollector collector = connection.createStanzaCollector(configuration)) { action.run(); collector.nextResultOrThrow(timeout); } - finally { - collector.cancel(); - } } @SuppressWarnings("ThreadPriorityCheck")