mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-11-22 14:22:05 +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)
|
* @see XMPPConnection#createStanzaCollector(StanzaFilter)
|
||||||
* @author Matt Tucker
|
* @author Matt Tucker
|
||||||
*/
|
*/
|
||||||
public class StanzaCollector {
|
public class StanzaCollector implements AutoCloseable {
|
||||||
|
|
||||||
private final StanzaFilter packetFilter;
|
private final StanzaFilter packetFilter;
|
||||||
|
|
||||||
|
@ -92,6 +92,10 @@ public class StanzaCollector {
|
||||||
cancelled = true;
|
cancelled = true;
|
||||||
connection.removeStanzaCollector(this);
|
connection.removeStanzaCollector(this);
|
||||||
notifyAll();
|
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.ExtensionElement;
|
||||||
import org.jivesoftware.smack.packet.IQ;
|
import org.jivesoftware.smack.packet.IQ;
|
||||||
import org.jivesoftware.smack.util.CloseableUtil;
|
|
||||||
|
|
||||||
import org.xmlpull.v1.XmlPullParser;
|
import org.xmlpull.v1.XmlPullParser;
|
||||||
import org.xmlpull.v1.XmlPullParserFactory;
|
import org.xmlpull.v1.XmlPullParserFactory;
|
||||||
|
@ -54,10 +53,10 @@ public class ProviderFileLoader implements ProviderLoader {
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public ProviderFileLoader(InputStream providerStream, ClassLoader classLoader) {
|
public ProviderFileLoader(InputStream providerStream, ClassLoader classLoader) {
|
||||||
// Load processing providers.
|
// Load processing providers.
|
||||||
try {
|
try (InputStream is = providerStream) {
|
||||||
XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
|
XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
|
||||||
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
|
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
|
||||||
parser.setInput(providerStream, "UTF-8");
|
parser.setInput(is, "UTF-8");
|
||||||
int eventType = parser.getEventType();
|
int eventType = parser.getEventType();
|
||||||
do {
|
do {
|
||||||
if (eventType == XmlPullParser.START_TAG) {
|
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);
|
LOGGER.log(Level.SEVERE, "Unknown error occurred while parsing provider file", e);
|
||||||
exceptions.add(e);
|
exceptions.add(e);
|
||||||
}
|
}
|
||||||
finally {
|
|
||||||
CloseableUtil.maybeClose(providerStream, LOGGER);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -113,9 +113,7 @@ public final class FileUtils {
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("DefaultCharset")
|
@SuppressWarnings("DefaultCharset")
|
||||||
public static String readFileOrThrow(File file) throws IOException {
|
public static String readFileOrThrow(File file) throws IOException {
|
||||||
Reader reader = null;
|
try (Reader reader = new FileReader(file)) {
|
||||||
try {
|
|
||||||
reader = new FileReader(file);
|
|
||||||
char[] buf = new char[8192];
|
char[] buf = new char[8192];
|
||||||
int len;
|
int len;
|
||||||
StringBuilder s = new StringBuilder();
|
StringBuilder s = new StringBuilder();
|
||||||
|
@ -124,11 +122,6 @@ public final class FileUtils {
|
||||||
}
|
}
|
||||||
return s.toString();
|
return s.toString();
|
||||||
}
|
}
|
||||||
finally {
|
|
||||||
if (reader != null) {
|
|
||||||
reader.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String readFile(File file) {
|
public static String readFile(File file) {
|
||||||
|
|
|
@ -192,9 +192,9 @@ public final class IoTDataManager extends IoTManager {
|
||||||
doneCollector.nextResult();
|
doneCollector.nextResult();
|
||||||
}
|
}
|
||||||
finally {
|
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();
|
dataCollector.cancel();
|
||||||
doneCollector.cancel();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int collectedCount = dataCollector.getCollectedCount();
|
int collectedCount = dataCollector.getCollectedCount();
|
||||||
|
|
|
@ -546,17 +546,15 @@ public final class MamManager extends Manager {
|
||||||
|
|
||||||
StanzaCollector.Configuration resultCollectorConfiguration = StanzaCollector.newConfiguration()
|
StanzaCollector.Configuration resultCollectorConfiguration = StanzaCollector.newConfiguration()
|
||||||
.setStanzaFilter(new MamResultFilter(mamQueryIq)).setCollectorToReset(mamFinIQCollector);
|
.setStanzaFilter(new MamResultFilter(mamQueryIq)).setCollectorToReset(mamFinIQCollector);
|
||||||
StanzaCollector resultCollector = connection.createStanzaCollector(resultCollectorConfiguration);
|
|
||||||
|
|
||||||
try {
|
StanzaCollector cancelledResultCollector;
|
||||||
|
try (StanzaCollector resultCollector = connection.createStanzaCollector(resultCollectorConfiguration)) {
|
||||||
connection.sendStanza(mamQueryIq);
|
connection.sendStanza(mamQueryIq);
|
||||||
mamFinIQ = mamFinIQCollector.nextResultOrThrow();
|
mamFinIQ = mamFinIQCollector.nextResultOrThrow();
|
||||||
} finally {
|
cancelledResultCollector = resultCollector;
|
||||||
mamFinIQCollector.cancel();
|
|
||||||
resultCollector.cancel();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
private static void writeInfoToFile(File file, DiscoverInfo info) throws IOException {
|
private static void writeInfoToFile(File file, DiscoverInfo info) throws IOException {
|
||||||
DataOutputStream dos = new DataOutputStream(new FileOutputStream(file));
|
try (DataOutputStream dos = new DataOutputStream(new FileOutputStream(file))) {
|
||||||
try {
|
|
||||||
dos.writeUTF(info.toXML(null).toString());
|
dos.writeUTF(info.toXML(null).toString());
|
||||||
} finally {
|
|
||||||
dos.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -146,12 +143,9 @@ public class SimpleDirectoryPersistentCache implements EntityCapsPersistentCache
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private static DiscoverInfo restoreInfoFromFile(File file) throws Exception {
|
private static DiscoverInfo restoreInfoFromFile(File file) throws Exception {
|
||||||
DataInputStream dis = new DataInputStream(new FileInputStream(file));
|
|
||||||
String fileContent;
|
String fileContent;
|
||||||
try {
|
try (DataInputStream dis = new DataInputStream(new FileInputStream(file))) {
|
||||||
fileContent = dis.readUTF();
|
fileContent = dis.readUTF();
|
||||||
} finally {
|
|
||||||
dis.close();
|
|
||||||
}
|
}
|
||||||
if (fileContent == null) {
|
if (fileContent == null) {
|
||||||
return null;
|
return null;
|
||||||
|
|
|
@ -29,7 +29,6 @@ import java.util.logging.Logger;
|
||||||
|
|
||||||
import org.jivesoftware.smack.packet.ExtensionElement;
|
import org.jivesoftware.smack.packet.ExtensionElement;
|
||||||
import org.jivesoftware.smack.packet.Message;
|
import org.jivesoftware.smack.packet.Message;
|
||||||
import org.jivesoftware.smack.util.CloseableUtil;
|
|
||||||
import org.jivesoftware.smack.util.XmlStringBuilder;
|
import org.jivesoftware.smack.util.XmlStringBuilder;
|
||||||
import org.jivesoftware.smack.util.stringencoder.Base64;
|
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
|
// a binary format, which won't work well inside of XML. Therefore, we base-64
|
||||||
// encode the binary data before adding it.
|
// encode the binary data before adding it.
|
||||||
else {
|
else {
|
||||||
ByteArrayOutputStream byteStream = null;
|
try (
|
||||||
ObjectOutputStream out = null;
|
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
|
||||||
try {
|
ObjectOutputStream out = new ObjectOutputStream(byteStream);
|
||||||
byteStream = new ByteArrayOutputStream();
|
) {
|
||||||
out = new ObjectOutputStream(byteStream);
|
|
||||||
out.writeObject(value);
|
out.writeObject(value);
|
||||||
type = "java-object";
|
type = "java-object";
|
||||||
valueStr = Base64.encodeToString(byteStream.toByteArray());
|
valueStr = Base64.encodeToString(byteStream.toByteArray());
|
||||||
|
@ -191,10 +189,6 @@ public class JivePropertiesExtension implements ExtensionElement {
|
||||||
type = "java-object";
|
type = "java-object";
|
||||||
valueStr = "Serializing error: " + e.getMessage();
|
valueStr = "Serializing error: " + e.getMessage();
|
||||||
}
|
}
|
||||||
finally {
|
|
||||||
CloseableUtil.maybeClose(out, LOGGER);
|
|
||||||
CloseableUtil.maybeClose(byteStream, LOGGER);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
xml.attribute("type", type);
|
xml.attribute("type", type);
|
||||||
xml.rightAngleBracket();
|
xml.rightAngleBracket();
|
||||||
|
|
|
@ -165,8 +165,7 @@ public class OfflineMessageManager {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
int pendingNodes = nodes.size();
|
int pendingNodes = nodes.size();
|
||||||
StanzaCollector messageCollector = connection.createStanzaCollector(messageFilter);
|
try (StanzaCollector messageCollector = connection.createStanzaCollector(messageFilter)) {
|
||||||
try {
|
|
||||||
connection.createStanzaCollectorAndSend(request).nextResultOrThrow();
|
connection.createStanzaCollectorAndSend(request).nextResultOrThrow();
|
||||||
// Collect the received offline messages
|
// Collect the received offline messages
|
||||||
Message message;
|
Message message;
|
||||||
|
@ -181,10 +180,6 @@ public class OfflineMessageManager {
|
||||||
}
|
}
|
||||||
} while (message != null && pendingNodes > 0);
|
} while (message != null && pendingNodes > 0);
|
||||||
}
|
}
|
||||||
finally {
|
|
||||||
// Stop queuing offline messages
|
|
||||||
messageCollector.cancel();
|
|
||||||
}
|
|
||||||
return messages;
|
return messages;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -206,10 +201,9 @@ public class OfflineMessageManager {
|
||||||
|
|
||||||
StanzaCollector resultCollector = connection.createStanzaCollectorAndSend(request);
|
StanzaCollector resultCollector = connection.createStanzaCollectorAndSend(request);
|
||||||
StanzaCollector.Configuration messageCollectorConfiguration = StanzaCollector.newConfiguration().setStanzaFilter(PACKET_FILTER).setCollectorToReset(resultCollector);
|
StanzaCollector.Configuration messageCollectorConfiguration = StanzaCollector.newConfiguration().setStanzaFilter(PACKET_FILTER).setCollectorToReset(resultCollector);
|
||||||
StanzaCollector messageCollector = connection.createStanzaCollector(messageCollectorConfiguration);
|
|
||||||
|
|
||||||
List<Message> messages;
|
List<Message> messages;
|
||||||
try {
|
try (StanzaCollector messageCollector = connection.createStanzaCollector(messageCollectorConfiguration)) {
|
||||||
resultCollector.nextResultOrThrow();
|
resultCollector.nextResultOrThrow();
|
||||||
// Be extra safe, cancel the message collector right here so that it does not collector
|
// 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
|
// 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);
|
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;
|
return messages;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -491,9 +491,7 @@ public final class VCard extends IQ {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static byte[] getFileBytes(File file) throws IOException {
|
private static byte[] getFileBytes(File file) throws IOException {
|
||||||
BufferedInputStream bis = null;
|
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) {
|
||||||
try {
|
|
||||||
bis = new BufferedInputStream(new FileInputStream(file));
|
|
||||||
int bytes = (int) file.length();
|
int bytes = (int) file.length();
|
||||||
byte[] buffer = new byte[bytes];
|
byte[] buffer = new byte[bytes];
|
||||||
int readBytes = bis.read(buffer);
|
int readBytes = bis.read(buffer);
|
||||||
|
@ -502,11 +500,6 @@ public final class VCard extends IQ {
|
||||||
}
|
}
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
finally {
|
|
||||||
if (bis != null) {
|
|
||||||
bis.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -55,15 +55,10 @@ public abstract class AbstractSmackIntTest {
|
||||||
throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
|
throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
|
||||||
StanzaCollector.Configuration configuration = StanzaCollector.newConfiguration().setStanzaFilter(
|
StanzaCollector.Configuration configuration = StanzaCollector.newConfiguration().setStanzaFilter(
|
||||||
filter).setSize(1);
|
filter).setSize(1);
|
||||||
StanzaCollector collector = connection.createStanzaCollector(configuration);
|
try (StanzaCollector collector = connection.createStanzaCollector(configuration)) {
|
||||||
|
|
||||||
try {
|
|
||||||
action.run();
|
action.run();
|
||||||
collector.nextResultOrThrow(timeout);
|
collector.nextResultOrThrow(timeout);
|
||||||
}
|
}
|
||||||
finally {
|
|
||||||
collector.cancel();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("ThreadPriorityCheck")
|
@SuppressWarnings("ThreadPriorityCheck")
|
||||||
|
|
Loading…
Reference in a new issue