1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-09-09 17:19:39 +02:00

Introduce CloseableUtil

This commit is contained in:
Florian Schmaus 2018-08-15 17:25:22 +02:00
parent 1136e8a2e9
commit a00aa726fe
19 changed files with 109 additions and 256 deletions

View file

@ -29,11 +29,11 @@ import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.util.CloseableUtil;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHost;
@ -97,11 +97,7 @@ public class Socks5Client {
}
catch (SmackException e) {
if (!socket.isClosed()) {
try {
socket.close();
} catch (IOException e2) {
LOGGER.log(Level.WARNING, "Could not close SOCKS5 socket", e2);
}
CloseableUtil.maybeClose(socket, LOGGER);
}
throw e;
}

View file

@ -38,6 +38,7 @@ import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.util.CloseableUtil;
import org.jivesoftware.smack.util.StringUtils;
/**
@ -409,14 +410,7 @@ public final class Socks5Proxy {
*/
}
catch (Exception e) {
try {
if (socket != null) {
socket.close();
}
}
catch (IOException e1) {
/* do nothing */
}
CloseableUtil.maybeClose(socket, LOGGER);
}
}

View file

@ -27,12 +27,12 @@ import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smack.util.CloseableUtil;
/**
@ -158,20 +158,8 @@ public class IncomingFileTransfer extends FileTransfer {
if (getStatus().equals(Status.in_progress)) {
setStatus(Status.complete);
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
LOGGER.log(Level.WARNING, "Closing input stream", e);
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
LOGGER.log(Level.WARNING, "Closing output stream", e);
}
}
CloseableUtil.maybeClose(inputStream, LOGGER);
CloseableUtil.maybeClose(outputStream, LOGGER);
}
}, "File Transfer " + streamID);
transferThread.start();

View file

@ -22,7 +22,6 @@ import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.SmackException;
@ -30,6 +29,7 @@ import org.jivesoftware.smack.SmackException.IllegalStateChangeException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smack.packet.StanzaError;
import org.jivesoftware.smack.util.CloseableUtil;
import org.jxmpp.jid.Jid;
@ -258,19 +258,8 @@ public class OutgoingFileTransfer extends FileTransfer {
setStatus(FileTransfer.Status.error);
setException(e);
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
LOGGER.log(Level.WARNING, "Closing input stream", e);
}
}
try {
outputStream.close();
} catch (IOException e) {
LOGGER.log(Level.WARNING, "Closing output stream", e);
}
CloseableUtil.maybeClose(inputStream, LOGGER);
CloseableUtil.maybeClose(outputStream, LOGGER);
}
updateStatus(Status.in_progress, FileTransfer.Status.complete);
}
@ -325,16 +314,8 @@ public class OutgoingFileTransfer extends FileTransfer {
setStatus(FileTransfer.Status.error);
setException(e);
} finally {
try {
if (in != null) {
in.close();
}
outputStream.flush();
outputStream.close();
} catch (IOException e) {
/* Do Nothing */
}
CloseableUtil.maybeClose(in, LOGGER);
CloseableUtil.maybeClose(outputStream, LOGGER);
}
updateStatus(Status.in_progress, FileTransfer.Status.complete);
}

View file

@ -29,6 +29,7 @@ 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;
@ -191,22 +192,8 @@ public class JivePropertiesExtension implements ExtensionElement {
valueStr = "Serializing error: " + e.getMessage();
}
finally {
if (out != null) {
try {
out.close();
}
catch (Exception e) {
// Ignore.
}
}
if (byteStream != null) {
try {
byteStream.close();
}
catch (Exception e) {
// Ignore.
}
}
CloseableUtil.maybeClose(out, LOGGER);
CloseableUtil.maybeClose(byteStream, LOGGER);
}
}
xml.attribute("type", type);