mirror of
https://github.com/vanitasvitae/Smack.git
synced 2025-09-10 01:29:38 +02:00
Introduce CloseableUtil
This commit is contained in:
parent
1136e8a2e9
commit
a00aa726fe
19 changed files with 109 additions and 256 deletions
|
@ -18,7 +18,6 @@
|
|||
package org.jivesoftware.smack;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.Collection;
|
||||
|
@ -37,6 +36,7 @@ import org.jivesoftware.smack.sasl.core.SASLAnonymous;
|
|||
import org.jivesoftware.smack.sasl.core.SASLXOauth2Mechanism;
|
||||
import org.jivesoftware.smack.sasl.core.SCRAMSHA1Mechanism;
|
||||
import org.jivesoftware.smack.sasl.core.ScramSha1PlusMechanism;
|
||||
import org.jivesoftware.smack.util.CloseableUtil;
|
||||
import org.jivesoftware.smack.util.FileUtils;
|
||||
import org.jivesoftware.smack.util.StringUtils;
|
||||
|
||||
|
@ -61,17 +61,15 @@ public final class SmackInitialization {
|
|||
*/
|
||||
static {
|
||||
String smackVersion;
|
||||
BufferedReader reader = null;
|
||||
try {
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(FileUtils.getStreamForClasspathFile("org.jivesoftware.smack/version", null), StringUtils.UTF8));
|
||||
reader = new BufferedReader(new InputStreamReader(FileUtils.getStreamForClasspathFile("org.jivesoftware.smack/version", null), StringUtils.UTF8));
|
||||
smackVersion = reader.readLine();
|
||||
try {
|
||||
reader.close();
|
||||
} catch (IOException e) {
|
||||
LOGGER.log(Level.WARNING, "IOException closing stream", e);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOGGER.log(Level.SEVERE, "Could not determine Smack version", e);
|
||||
smackVersion = "unknown";
|
||||
} finally {
|
||||
CloseableUtil.maybeClose(reader, LOGGER);
|
||||
}
|
||||
SMACK_VERSION = smackVersion;
|
||||
|
||||
|
@ -146,12 +144,7 @@ public final class SmackInitialization {
|
|||
eventType = parser.next();
|
||||
}
|
||||
while (eventType != XmlPullParser.END_DOCUMENT);
|
||||
try {
|
||||
cfgFileStream.close();
|
||||
}
|
||||
catch (IOException e) {
|
||||
LOGGER.log(Level.SEVERE, "Error while closing config file input stream", e);
|
||||
}
|
||||
CloseableUtil.maybeClose(cfgFileStream, LOGGER);
|
||||
}
|
||||
|
||||
private static void parseClassesToLoad(XmlPullParser parser, boolean optional,
|
||||
|
|
|
@ -16,7 +16,6 @@
|
|||
*/
|
||||
package org.jivesoftware.smack.initializer;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
import java.util.LinkedList;
|
||||
|
@ -27,6 +26,7 @@ import java.util.logging.Logger;
|
|||
import org.jivesoftware.smack.SmackInitialization;
|
||||
import org.jivesoftware.smack.provider.ProviderFileLoader;
|
||||
import org.jivesoftware.smack.provider.ProviderManager;
|
||||
import org.jivesoftware.smack.util.CloseableUtil;
|
||||
import org.jivesoftware.smack.util.FileUtils;
|
||||
|
||||
/**
|
||||
|
@ -86,14 +86,6 @@ public abstract class UrlInitializer implements SmackInitializer {
|
|||
}
|
||||
|
||||
private static void maybeClose(InputStream is) {
|
||||
if (is == null) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
is.close();
|
||||
}
|
||||
catch (IOException e) {
|
||||
LOGGER.log(Level.WARNING, "Could not close input stream", e);
|
||||
}
|
||||
CloseableUtil.maybeClose(is, LOGGER);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,6 +26,7 @@ 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;
|
||||
|
@ -141,12 +142,7 @@ public class ProviderFileLoader implements ProviderLoader {
|
|||
exceptions.add(e);
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
providerStream.close();
|
||||
}
|
||||
catch (Exception e) {
|
||||
LOGGER.log(Level.WARNING, "Exception closing provider stream", e);
|
||||
}
|
||||
CloseableUtil.maybeClose(providerStream, LOGGER);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -21,7 +21,9 @@ import java.io.InputStream;
|
|||
import java.io.OutputStream;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.jivesoftware.smack.util.CloseableUtil;
|
||||
import org.jivesoftware.smack.util.StringUtils;
|
||||
|
||||
/**
|
||||
|
@ -30,6 +32,8 @@ import org.jivesoftware.smack.util.StringUtils;
|
|||
* @author Atul Aggarwal
|
||||
*/
|
||||
public class Socks5ProxySocketConnection implements ProxySocketConnection {
|
||||
private static final Logger LOGGER = Logger.getLogger(Socks5ProxySocketConnection.class.getName());
|
||||
|
||||
private final ProxyInfo proxy;
|
||||
|
||||
Socks5ProxySocketConnection(ProxyInfo proxy) {
|
||||
|
@ -164,11 +168,7 @@ public class Socks5ProxySocketConnection implements ProxySocketConnection {
|
|||
}
|
||||
|
||||
if (!check) {
|
||||
try {
|
||||
socket.close();
|
||||
}
|
||||
catch (Exception eee) {
|
||||
}
|
||||
CloseableUtil.maybeClose(socket, LOGGER);
|
||||
throw new ProxyException(ProxyInfo.ProxyType.SOCKS5,
|
||||
"fail in SOCKS5 proxy");
|
||||
}
|
||||
|
@ -253,11 +253,7 @@ public class Socks5ProxySocketConnection implements ProxySocketConnection {
|
|||
fill(in, buf, 4);
|
||||
|
||||
if (buf[1] != 0) {
|
||||
try {
|
||||
socket.close();
|
||||
}
|
||||
catch (Exception eee) {
|
||||
}
|
||||
CloseableUtil.maybeClose(socket, LOGGER);
|
||||
throw new ProxyException(ProxyInfo.ProxyType.SOCKS5,
|
||||
"server returns " + buf[1]);
|
||||
}
|
||||
|
@ -280,11 +276,7 @@ public class Socks5ProxySocketConnection implements ProxySocketConnection {
|
|||
throw e;
|
||||
}
|
||||
catch (Exception e) {
|
||||
try {
|
||||
socket.close();
|
||||
}
|
||||
catch (Exception eee) {
|
||||
}
|
||||
CloseableUtil.maybeClose(socket, LOGGER);
|
||||
// TODO convert to IOException(e) when minimum Android API level is 9 or higher
|
||||
throw new IOException(e.getLocalizedMessage());
|
||||
}
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2018 Florian Schmaus
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smack.util;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class CloseableUtil {
|
||||
|
||||
public static void maybeClose(Closeable closable, Logger logger) {
|
||||
if (closable == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
closable.close();
|
||||
} catch (IOException e) {
|
||||
logger.log(Level.WARNING, "Could not close " + closable, e);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue