1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-09-09 17:19:39 +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

@ -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;

View file

@ -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();

View file

@ -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;
}

View file

@ -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();
}
}
}
/**