mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-11-22 14:22:05 +01:00
Reworked compression-jzlib and compressionHandlers
- There is now no longer the need to use reflection for compression-jzlib. - compressionHandlers are a global configuration property and therefore belong in SmackConfiguration.
This commit is contained in:
parent
489816c61f
commit
a3ab886896
5 changed files with 50 additions and 55 deletions
|
@ -1,6 +1,6 @@
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Copyright 2013 Florian Schmaus
|
* Copyright 2013-2014 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.
|
||||||
|
@ -16,31 +16,28 @@
|
||||||
*/
|
*/
|
||||||
package org.jivesoftware.smack.compression;
|
package org.jivesoftware.smack.compression;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.lang.reflect.Constructor;
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import org.jivesoftware.smack.SmackConfiguration;
|
||||||
import java.lang.reflect.Method;
|
|
||||||
|
import com.jcraft.jzlib.JZlib;
|
||||||
|
import com.jcraft.jzlib.ZInputStream;
|
||||||
|
import com.jcraft.jzlib.ZOutputStream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class provides XMPP "zlib" compression with the help of JZLib. Note that jzlib-1.0.7 must be used (i.e. in the
|
* This class provides XMPP "zlib" compression with the help of JZLib.
|
||||||
* classpath), newer versions won't work!
|
|
||||||
*
|
*
|
||||||
* @author Florian Schmaus
|
* @author Florian Schmaus
|
||||||
* @see <a href="http://www.jcraft.com/jzlib/">JZLib</a>
|
* @see <a href="http://www.jcraft.com/jzlib/">JZLib</a>
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
public class JzlibInputOutputStream extends XMPPInputOutputStream {
|
public class JzlibInputOutputStream extends XMPPInputOutputStream {
|
||||||
|
|
||||||
private static Class<?> zoClass = null;
|
|
||||||
private static Class<?> ziClass = null;
|
|
||||||
|
|
||||||
static {
|
static {
|
||||||
try {
|
SmackConfiguration.addCompressionHandler(new JzlibInputOutputStream());
|
||||||
zoClass = Class.forName("com.jcraft.jzlib.ZOutputStream");
|
|
||||||
ziClass = Class.forName("com.jcraft.jzlib.ZInputStream");
|
|
||||||
} catch (ClassNotFoundException e) {
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public JzlibInputOutputStream() {
|
public JzlibInputOutputStream() {
|
||||||
|
@ -49,28 +46,22 @@ public class JzlibInputOutputStream extends XMPPInputOutputStream {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isSupported() {
|
public boolean isSupported() {
|
||||||
return (zoClass != null && ziClass != null);
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public InputStream getInputStream(InputStream inputStream) throws SecurityException, NoSuchMethodException,
|
public InputStream getInputStream(InputStream inputStream) throws IOException {
|
||||||
IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException {
|
ZInputStream is = new ZInputStream(inputStream);
|
||||||
Constructor<?> constructor = ziClass.getConstructor(InputStream.class);
|
is.setFlushMode(JZlib.Z_SYNC_FLUSH);
|
||||||
Object in = constructor.newInstance(inputStream);
|
|
||||||
|
|
||||||
Method method = ziClass.getMethod("setFlushMode", Integer.TYPE);
|
return is;
|
||||||
method.invoke(in, 2);
|
|
||||||
return (InputStream) in;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public OutputStream getOutputStream(OutputStream outputStream) throws SecurityException, NoSuchMethodException,
|
public OutputStream getOutputStream(OutputStream outputStream) throws IOException {
|
||||||
IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
|
ZOutputStream os = new ZOutputStream(outputStream);
|
||||||
Constructor<?> constructor = zoClass.getConstructor(OutputStream.class, Integer.TYPE);
|
os.setFlushMode(JZlib.Z_SYNC_FLUSH);
|
||||||
Object out = constructor.newInstance(outputStream, 9);
|
|
||||||
|
|
||||||
Method method = zoClass.getMethod("setFlushMode", Integer.TYPE);
|
return os;
|
||||||
method.invoke(out, 2);
|
|
||||||
return (OutputStream) out;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,6 +24,7 @@ task compressionJar(type: Jar) {
|
||||||
dependsOn classes
|
dependsOn classes
|
||||||
from sourceSets.main.output
|
from sourceSets.main.output
|
||||||
include('org/jivesoftware/smack/compression/**')
|
include('org/jivesoftware/smack/compression/**')
|
||||||
|
include('org/jivesoftware/smack/SmackConfiguration.class')
|
||||||
}
|
}
|
||||||
task dnsJar(type: Jar) {
|
task dnsJar(type: Jar) {
|
||||||
appendix += '-dns'
|
appendix += '-dns'
|
||||||
|
|
|
@ -28,6 +28,8 @@ import java.util.Set;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import org.jivesoftware.smack.compression.Java7ZlibInputOutputStream;
|
||||||
|
import org.jivesoftware.smack.compression.XMPPInputOutputStream;
|
||||||
import org.jivesoftware.smack.initializer.SmackInitializer;
|
import org.jivesoftware.smack.initializer.SmackInitializer;
|
||||||
import org.jivesoftware.smack.parsing.ExceptionThrowingCallback;
|
import org.jivesoftware.smack.parsing.ExceptionThrowingCallback;
|
||||||
import org.jivesoftware.smack.parsing.ParsingExceptionCallback;
|
import org.jivesoftware.smack.parsing.ParsingExceptionCallback;
|
||||||
|
@ -64,6 +66,15 @@ public final class SmackConfiguration {
|
||||||
|
|
||||||
private static Set<String> disabledSmackClasses = new HashSet<String>();
|
private static Set<String> disabledSmackClasses = new HashSet<String>();
|
||||||
|
|
||||||
|
private final static List<XMPPInputOutputStream> compressionHandlers = new ArrayList<XMPPInputOutputStream>(2);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads the configuration from the smack-config.xml file.<p>
|
||||||
|
*
|
||||||
|
* So far this means that:
|
||||||
|
* 1) a set of classes will be loaded in order to execute their static init block
|
||||||
|
* 2) retrieve and set the current Smack release
|
||||||
|
*/
|
||||||
static {
|
static {
|
||||||
String smackVersion;
|
String smackVersion;
|
||||||
try {
|
try {
|
||||||
|
@ -104,6 +115,9 @@ public final class SmackConfiguration {
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
throw new IllegalStateException(e);
|
throw new IllegalStateException(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add the Java7 compression handler first, since it's preferred
|
||||||
|
compressionHandlers.add(new Java7ZlibInputOutputStream());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -112,17 +126,6 @@ public final class SmackConfiguration {
|
||||||
*/
|
*/
|
||||||
private static ParsingExceptionCallback defaultCallback = new ExceptionThrowingCallback();
|
private static ParsingExceptionCallback defaultCallback = new ExceptionThrowingCallback();
|
||||||
|
|
||||||
private SmackConfiguration() {
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Loads the configuration from the smack-config.xml file.<p>
|
|
||||||
*
|
|
||||||
* So far this means that:
|
|
||||||
* 1) a set of classes will be loaded in order to execute their static init block
|
|
||||||
* 2) retrieve and set the current Smack release
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the Smack version information, eg "1.3.0".
|
* Returns the Smack version information, eg "1.3.0".
|
||||||
*
|
*
|
||||||
|
@ -250,6 +253,20 @@ public final class SmackConfiguration {
|
||||||
return defaultCallback;
|
return defaultCallback;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void addCompressionHandler(XMPPInputOutputStream xmppInputOutputStream) {
|
||||||
|
compressionHandlers.add(xmppInputOutputStream);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<XMPPInputOutputStream> getCompresionHandlers() {
|
||||||
|
List<XMPPInputOutputStream> res = new ArrayList<XMPPInputOutputStream>(compressionHandlers.size());
|
||||||
|
for (XMPPInputOutputStream ios : compressionHandlers) {
|
||||||
|
if (ios.isSupported()) {
|
||||||
|
res.add(ios);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
public static void processConfigFile(InputStream cfgFileStream, Collection<Exception> exceptions) throws Exception {
|
public static void processConfigFile(InputStream cfgFileStream, Collection<Exception> exceptions) throws Exception {
|
||||||
XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
|
XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
|
||||||
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
|
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
|
||||||
|
|
|
@ -19,10 +19,8 @@ package org.jivesoftware.smack;
|
||||||
import java.io.Reader;
|
import java.io.Reader;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
import java.lang.reflect.Constructor;
|
import java.lang.reflect.Constructor;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
@ -36,7 +34,6 @@ import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import org.jivesoftware.smack.compression.Java7ZlibInputOutputStream;
|
|
||||||
import org.jivesoftware.smack.compression.XMPPInputOutputStream;
|
import org.jivesoftware.smack.compression.XMPPInputOutputStream;
|
||||||
import org.jivesoftware.smack.debugger.SmackDebugger;
|
import org.jivesoftware.smack.debugger.SmackDebugger;
|
||||||
import org.jivesoftware.smack.filter.IQReplyFilter;
|
import org.jivesoftware.smack.filter.IQReplyFilter;
|
||||||
|
@ -97,8 +94,6 @@ public abstract class XMPPConnection {
|
||||||
private final static Set<ConnectionCreationListener> connectionEstablishedListeners =
|
private final static Set<ConnectionCreationListener> connectionEstablishedListeners =
|
||||||
new CopyOnWriteArraySet<ConnectionCreationListener>();
|
new CopyOnWriteArraySet<ConnectionCreationListener>();
|
||||||
|
|
||||||
protected final static List<XMPPInputOutputStream> compressionHandlers = new ArrayList<XMPPInputOutputStream>(2);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Value that indicates whether debugging is enabled. When enabled, a debug
|
* Value that indicates whether debugging is enabled. When enabled, a debug
|
||||||
* window will apear for each new connection that will contain the following
|
* window will apear for each new connection that will contain the following
|
||||||
|
@ -125,12 +120,6 @@ public abstract class XMPPConnection {
|
||||||
}
|
}
|
||||||
// Ensure the SmackConfiguration class is loaded by calling a method in it.
|
// Ensure the SmackConfiguration class is loaded by calling a method in it.
|
||||||
SmackConfiguration.getVersion();
|
SmackConfiguration.getVersion();
|
||||||
// Add the Java7 compression handler first, since it's preferred
|
|
||||||
compressionHandlers.add(new Java7ZlibInputOutputStream());
|
|
||||||
// If we don't have access to the Java7 API use the JZlib compression handler
|
|
||||||
|
|
||||||
// TODO gradle migration
|
|
||||||
//compressionHandlers.add(new JzlibInputOutputStream());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -827,10 +827,7 @@ public class TCPConnection extends XMPPConnection {
|
||||||
*/
|
*/
|
||||||
private XMPPInputOutputStream maybeGetCompressionHandler() {
|
private XMPPInputOutputStream maybeGetCompressionHandler() {
|
||||||
if (compressionMethods != null) {
|
if (compressionMethods != null) {
|
||||||
for (XMPPInputOutputStream handler : compressionHandlers) {
|
for (XMPPInputOutputStream handler : SmackConfiguration.getCompresionHandlers()) {
|
||||||
if (!handler.isSupported())
|
|
||||||
continue;
|
|
||||||
|
|
||||||
String method = handler.getCompressionMethod();
|
String method = handler.getCompressionMethod();
|
||||||
if (compressionMethods.contains(method))
|
if (compressionMethods.contains(method))
|
||||||
return handler;
|
return handler;
|
||||||
|
|
Loading…
Reference in a new issue