mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-12-22 20:47:57 +01:00
Use try-with-resources where possible
and make StanzaCollector implement AutoCloseable.
This commit is contained in:
parent
e98d42790a
commit
658fd08d20
10 changed files with 30 additions and 69 deletions
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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<Message> 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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Reference in a new issue