From 9502ff25dd6c32c92d356baaaa1640fe26314595 Mon Sep 17 00:00:00 2001 From: Rene Treffer Date: Thu, 3 Apr 2014 18:16:14 +0200 Subject: [PATCH] Add compression support --- res/layout/edit_account_dialog.xml | 8 +- .../siacs/conversations/entities/Account.java | 1 + .../siacs/conversations/ui/EditAccount.java | 13 +-- .../utils/zlib/ZLibInputStream.java | 52 +++++++++++ .../utils/zlib/ZLibOutputStream.java | 89 +++++++++++++++++++ src/eu/siacs/conversations/xml/TagWriter.java | 8 +- src/eu/siacs/conversations/xml/XmlReader.java | 6 +- .../conversations/xmpp/XmppConnection.java | 60 ++++++++++++- 8 files changed, 227 insertions(+), 10 deletions(-) create mode 100644 src/eu/siacs/conversations/utils/zlib/ZLibInputStream.java create mode 100644 src/eu/siacs/conversations/utils/zlib/ZLibOutputStream.java diff --git a/res/layout/edit_account_dialog.xml b/res/layout/edit_account_dialog.xml index 2b1f95ab5..cc29b6f91 100644 --- a/res/layout/edit_account_dialog.xml +++ b/res/layout/edit_account_dialog.xml @@ -50,7 +50,13 @@ android:layout_height="wrap_content" android:text="Use Transport Layer Security (TLS)" android:checked="true"/> - + + Android 2.2 includes Java7 FLUSH_SYNC option, which will be used by this + * Implementation, preferable via reflection. The @hide was remove in API level + * 19. This class might thus go away in the future.

+ *

Please use {@link ZLibOutputStream#SUPPORTED} to check for flush + * compatibility.

+ */ +public class ZLibOutputStream extends DeflaterOutputStream { + + /** + * The reflection based flush method. + */ + + private final static Method method; + /** + * SUPPORTED is true if a flush compatible method exists. + */ + public final static boolean SUPPORTED; + + /** + * Static block to initialize {@link #SUPPORTED} and {@link #method}. + */ + static { + Method m = null; + try { + m = Deflater.class.getMethod("deflate", byte[].class, int.class, int.class, int.class); + } catch (SecurityException e) { + } catch (NoSuchMethodException e) { + } + method = m; + SUPPORTED = (method != null); + } + + /** + * Create a new ZLib compatible output stream wrapping the given low level + * stream. ZLib compatiblity means we will send a zlib header. + * @param os OutputStream The underlying stream. + * @throws IOException In case of a lowlevel transfer problem. + * @throws NoSuchAlgorithmException In case of a {@link Deflater} error. + */ + public ZLibOutputStream(OutputStream os) throws IOException, + NoSuchAlgorithmException { + super(os, new Deflater(Deflater.BEST_COMPRESSION)); + } + + /** + * Flush the given stream, preferring Java7 FLUSH_SYNC if available. + * @throws IOException In case of a lowlevel exception. + */ + @Override + public void flush() throws IOException { + if (!SUPPORTED) { + super.flush(); + return; + } + int count = 0; + if (!def.needsInput()) { + do { + count = def.deflate(buf, 0, buf.length); + out.write(buf, 0, count); + } while (count > 0); + out.flush(); + } + try { + do { + count = (Integer) method.invoke(def, buf, 0, buf.length, 2); + out.write(buf, 0, count); + } while (count > 0); + } catch (IllegalArgumentException e) { + throw new IOException("Can't flush"); + } catch (IllegalAccessException e) { + throw new IOException("Can't flush"); + } catch (InvocationTargetException e) { + throw new IOException("Can't flush"); + } + super.flush(); + } + +} diff --git a/src/eu/siacs/conversations/xml/TagWriter.java b/src/eu/siacs/conversations/xml/TagWriter.java index f06664fdd..d945b47b5 100644 --- a/src/eu/siacs/conversations/xml/TagWriter.java +++ b/src/eu/siacs/conversations/xml/TagWriter.java @@ -9,6 +9,7 @@ import eu.siacs.conversations.xmpp.stanzas.AbstractStanza; public class TagWriter { + private OutputStream plainOutputStream; private OutputStreamWriter outputStream; private boolean finshed = false; private LinkedBlockingQueue writeQueue = new LinkedBlockingQueue(); @@ -37,9 +38,14 @@ public class TagWriter { } public void setOutputStream(OutputStream out) { + this.plainOutputStream = out; this.outputStream = new OutputStreamWriter(out); } - + + public OutputStream getOutputStream() { + return this.plainOutputStream; + } + public TagWriter beginDocument() throws IOException { outputStream.write(""); outputStream.flush(); diff --git a/src/eu/siacs/conversations/xml/XmlReader.java b/src/eu/siacs/conversations/xml/XmlReader.java index 7ae9a7b38..71e86cf9c 100644 --- a/src/eu/siacs/conversations/xml/XmlReader.java +++ b/src/eu/siacs/conversations/xml/XmlReader.java @@ -36,7 +36,11 @@ public class XmlReader { Log.d(LOGTAG,"error setting input stream"); } } - + + public InputStream getInputStream() { + return is; + } + public void reset() { try { parser.setInput(new InputStreamReader(this.is)); diff --git a/src/eu/siacs/conversations/xmpp/XmppConnection.java b/src/eu/siacs/conversations/xmpp/XmppConnection.java index 1fd42bcaf..3702e66b7 100644 --- a/src/eu/siacs/conversations/xmpp/XmppConnection.java +++ b/src/eu/siacs/conversations/xmpp/XmppConnection.java @@ -38,6 +38,8 @@ import android.util.Log; import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.utils.CryptoHelper; import eu.siacs.conversations.utils.DNSHelper; +import eu.siacs.conversations.utils.zlib.ZLibOutputStream; +import eu.siacs.conversations.utils.zlib.ZLibInputStream; import eu.siacs.conversations.xml.Element; import eu.siacs.conversations.xml.Tag; import eu.siacs.conversations.xml.TagWriter; @@ -177,6 +179,13 @@ public class XmppConnection implements Runnable { wakeLock.release(); } return; + } catch (NoSuchAlgorithmException e) { + this.changeStatus(Account.STATUS_OFFLINE); + Log.d(LOGTAG, "compression exception " + e.getMessage()); + if (wakeLock.isHeld()) { + wakeLock.release(); + } + return; } catch (XmlPullParserException e) { this.changeStatus(Account.STATUS_OFFLINE); Log.d(LOGTAG, "xml exception " + e.getMessage()); @@ -194,7 +203,7 @@ public class XmppConnection implements Runnable { } private void processStream(Tag currentTag) throws XmlPullParserException, - IOException { + IOException, NoSuchAlgorithmException { Tag nextTag = tagReader.readTag(); while ((nextTag != null) && (!nextTag.isEnd("stream"))) { if (nextTag.isStart("error")) { @@ -208,6 +217,8 @@ public class XmppConnection implements Runnable { } } else if (nextTag.isStart("proceed")) { switchOverToTls(nextTag); + } else if (nextTag.isStart("compressed")) { + switchOverToZLib(nextTag); } else if (nextTag.isStart("success")) { Log.d(LOGTAG, account.getJid() + ": logged in"); @@ -375,6 +386,33 @@ public class XmppConnection implements Runnable { } } + private void sendCompressionZlib() throws IOException { + tagWriter.writeElement(new Element("compress") { + public String toString() { + return + "" + + "zlib" + + ""; + } + }); + } + + private void switchOverToZLib(Tag currentTag) throws XmlPullParserException, + IOException, NoSuchAlgorithmException { + + Log.d(LOGTAG,account.getJid()+": Starting zlib compressed stream"); + + tagReader.readTag(); // read tag close + + tagWriter.setOutputStream(new ZLibOutputStream(tagWriter.getOutputStream())); + tagReader.setInputStream(new ZLibInputStream(tagReader.getInputStream())); + + sendStartStream(); + processStream(tagReader.readTag()); + + Log.d(LOGTAG,account.getJid()+": zlib compressed stream established"); + } + private void sendStartTLS() throws IOException { Tag startTLS = Tag.empty("starttls"); startTLS.setAttribute("xmlns", "urn:ietf:params:xml:ns:xmpp-tls"); @@ -486,6 +524,8 @@ public class XmppConnection implements Runnable { if (this.streamFeatures.hasChild("starttls") && account.isOptionSet(Account.OPTION_USETLS)) { sendStartTLS(); + } else if (compressionAvailable()) { + sendCompressionZlib(); } else if (this.streamFeatures.hasChild("register")&&(account.isOptionSet(Account.OPTION_REGISTER))) { sendRegistryRequest(); } else if (!this.streamFeatures.hasChild("register")&&(account.isOptionSet(Account.OPTION_REGISTER))) { @@ -514,6 +554,24 @@ public class XmppConnection implements Runnable { } } + private boolean compressionAvailable() { + if (!this.streamFeatures.hasChild("compression", "http://jabber.org/features/compress")) return false; + if (!ZLibOutputStream.SUPPORTED) return false; + if (!account.isOptionSet(Account.OPTION_USECOMPRESSION)) return false; + + Element compression = this.streamFeatures.findChild("compression", "http://jabber.org/features/compress"); + for (Element child : compression.getChildren()) { + if (!"method".equals(child.getName())) continue; + + if ("zlib".equalsIgnoreCase(child.getContent())) { + Log.d(LOGTAG, account.getJid() + ": compression available"); + return true; + } + } + + return false; + } + private List extractMechanisms(Element stream) { ArrayList mechanisms = new ArrayList(stream.getChildren().size()); for(Element child : stream.getChildren()) {