From 9bf162922691c1f45813da6a21f2e274bfad3114 Mon Sep 17 00:00:00 2001 From: Alan Young Date: Mon, 6 Jul 2015 10:14:13 +0200 Subject: VCard.getPhoto() to return null for absent photo. Most of the time when VCard.getPhoto() is called it is only used to determine presence or absence of a photo. Thus, it makes no sense to allocate an empty ByteArray() just for this purpose. Return null instead, if absent. Change-Id: I3bae02ffbece5589c74c950bbb4c0db6c0376c76 diff --git a/README b/README deleted file mode 100644 index 0574db7..0000000 --- a/README +++ /dev/null @@ -1,24 +0,0 @@ -Stroke - -Stroke is a port of the C++ Swift library ( http://swift.im/swiften/ ) -The source is available from the Git repository at http://swift.im/git/stroke/ - -For XML parsing, Stroke depends on the Aalto XML Parser and the STAX2 API, from http://wiki.fasterxml.com/AaltoHome - -It also depends upon http://www.jcraft.com/jzlib/, which is passed to ant in the jzlib-dir parameter. The passed folder should contain a jar called jzlib.jar. - -It also depends upon icu4j from http://site.icu-project.org/ - -It also depends upon dnsjava from http://www.dnsjava.org/ - - -To build, run: -ant -Dxpp-dir=third-party/xpp -Djzlib-dir=third-party/jzlib -Dicu4j-dir=third-party/ -Dstax2-dir=third-party/stax2/ -Daalto-dir=third-party/aalto/ -Changing the paths to the relevant paths for the dependencies on your system - -Easy version: -The included Makefile should, on Unixes with make/curl installed, grab the dependencies (once only) and build. - - -For development: -If you want to commit changes to Stroke, first run `make .git/hooks/commit-msg` to download a script that will generate change-ids needed by our review system. diff --git a/README.md b/README.md new file mode 100644 index 0000000..ee28436 --- /dev/null +++ b/README.md @@ -0,0 +1,30 @@ +# Stroke + +Stroke is a port of the C++ Swift library ( http://swift.im/swiften/ ) +The source is available from the Git repository at http://swift.im/git/stroke/ + +For XML parsing, Stroke depends on the Aalto XML Parser and the STAX2 API, from http://wiki.fasterxml.com/AaltoHome + +It also depends upon http://www.jcraft.com/jzlib/, which is passed to ant in the jzlib-dir parameter. The passed folder should contain a jar called jzlib.jar. + +It also depends upon icu4j from http://site.icu-project.org/ + +It also depends upon dnsjava from http://www.dnsjava.org/ + + +To build, run: +ant -Dxpp-dir=third-party/xpp -Djzlib-dir=third-party/jzlib -Dicu4j-dir=third-party/ -Dstax2-dir=third-party/stax2/ -Daalto-dir=third-party/aalto/ +Changing the paths to the relevant paths for the dependencies on your system + +Easy version: +The included Makefile should, on Unixes with make/curl installed, grab the dependencies (once only) and build. + + +For development: +If you want to commit changes to Stroke, first run `make .git/hooks/commit-msg` to download a script that will generate change-ids needed by our review system. + +## Differences from Swiften + +Stroke tries to be a clean and accurate port of Swiften, in order to facilitate mirroring changes. Sometimes differences are either necessary or desirable. + +* `VCard.getPhoto()` returns null instead of an empty `ByteArray` when there is no photo. diff --git a/src/com/isode/stroke/avatars/VCardAvatarManager.java b/src/com/isode/stroke/avatars/VCardAvatarManager.java index 83c8c64..a924676 100755 --- a/src/com/isode/stroke/avatars/VCardAvatarManager.java +++ b/src/com/isode/stroke/avatars/VCardAvatarManager.java @@ -17,6 +17,7 @@ import com.isode.stroke.elements.VCard; import com.isode.stroke.crypto.CryptoProvider; import com.isode.stroke.stringcodecs.Hexify; import com.isode.stroke.avatars.AvatarStorage; +import com.isode.stroke.base.ByteArray; import com.isode.stroke.muc.MUCRegistry; import com.isode.stroke.vcards.VCardManager; import com.isode.stroke.signals.Slot2; @@ -62,16 +63,17 @@ public class VCardAvatarManager extends AvatarProvider { String hash = vcardManager_.getPhotoHash(avatarJID); if(hash.length() != 0) { if (!avatarStorage_.hasAvatar(hash)) { - VCard vCard = vcardManager_.getVCard(avatarJID); - if (vCard != null) { - String newHash = Hexify.hexify(crypto_.getSHA1Hash(vCard.getPhoto())); + final VCard vCard = vcardManager_.getVCard(avatarJID); + final ByteArray photo = vCard != null ? vCard.getPhoto() : null; + if (photo != null) { + String newHash = Hexify.hexify(crypto_.getSHA1Hash(photo)); if (!newHash.equals(hash)) { // Shouldn't happen, but sometimes seem to. Might be fixed if we // move to a safer backend. logger_.warning("Inconsistent vCard photo hash cache"); hash = newHash; } - avatarStorage_.addAvatar(hash, vCard.getPhoto()); + avatarStorage_.addAvatar(hash, photo); } else { // Can happen if the cache is inconsistent. diff --git a/src/com/isode/stroke/avatars/VCardUpdateAvatarManager.java b/src/com/isode/stroke/avatars/VCardUpdateAvatarManager.java index 5328fb4..681e94b 100755 --- a/src/com/isode/stroke/avatars/VCardUpdateAvatarManager.java +++ b/src/com/isode/stroke/avatars/VCardUpdateAvatarManager.java @@ -24,12 +24,14 @@ import com.isode.stroke.client.StanzaChannel; import com.isode.stroke.crypto.CryptoProvider; import com.isode.stroke.stringcodecs.Hexify; import com.isode.stroke.avatars.AvatarStorage; +import com.isode.stroke.base.ByteArray; import com.isode.stroke.muc.MUCRegistry; import com.isode.stroke.vcards.VCardManager; import com.isode.stroke.signals.Slot2; import com.isode.stroke.signals.Slot1; import java.util.logging.Logger; + import com.isode.stroke.signals.SignalConnection; public class VCardUpdateAvatarManager extends AvatarProvider { @@ -124,13 +126,14 @@ public class VCardUpdateAvatarManager extends AvatarProvider { return; } - if (vCard.getPhoto().isEmpty()) { + final ByteArray photo = vCard.getPhoto(); + if (photo == null) { setAvatarHash(from, ""); } else { - String hash = Hexify.hexify(crypto_.getSHA1Hash(vCard.getPhoto())); + String hash = Hexify.hexify(crypto_.getSHA1Hash(photo)); if (!avatarStorage_.hasAvatar(hash)) { - avatarStorage_.addAvatar(hash, vCard.getPhoto()); + avatarStorage_.addAvatar(hash, photo); } setAvatarHash(from, hash); } diff --git a/src/com/isode/stroke/elements/VCard.java b/src/com/isode/stroke/elements/VCard.java index 3bd4db2..3e64c7c 100644 --- a/src/com/isode/stroke/elements/VCard.java +++ b/src/com/isode/stroke/elements/VCard.java @@ -131,14 +131,17 @@ public class VCard extends Payload implements Serializable { public void setNickname(final String nick) { nick_ = nick; } public final String getNickname() { return nick_; } - public void setPhoto(final ByteArray photo) { photo_ = photo; } + /** + * Set the photo, but store null if the photo is empty. + */ + public void setPhoto(final ByteArray photo) { photo_ = photo != null && !photo.isEmpty() ? photo : null; } + /** + * Get the photo. + * Unlike Swiften, this will return null for empty photos, + * instead of an empty ByteArray. + */ public final ByteArray getPhoto() { - if(this.photo_ != null) { - return photo_; - } - else { - return new ByteArray(); - } + return photo_; } public void setPhotoType(final String photoType) { photoType_ = photoType; } diff --git a/src/com/isode/stroke/serializer/payloadserializers/VCardSerializer.java b/src/com/isode/stroke/serializer/payloadserializers/VCardSerializer.java index 9d460fa..9630d80 100644 --- a/src/com/isode/stroke/serializer/payloadserializers/VCardSerializer.java +++ b/src/com/isode/stroke/serializer/payloadserializers/VCardSerializer.java @@ -74,12 +74,12 @@ public class VCardSerializer extends GenericPayloadSerializer{ if (vcard.getNickname() != null && !vcard.getNickname().isEmpty()) { queryElement.addNode(new XMLElement("NICKNAME", "", vcard.getNickname())); } - if (vcard.getPhoto() != null && !vcard.getPhoto().isEmpty() || vcard.getPhotoType() != null && !vcard.getPhotoType().isEmpty()) { + if (vcard.getPhoto() != null || vcard.getPhotoType() != null && !vcard.getPhotoType().isEmpty()) { XMLElement photoElement = new XMLElement("PHOTO"); if (vcard.getPhotoType() != null && !vcard.getPhotoType().isEmpty()) { photoElement.addNode(new XMLElement("TYPE", "", vcard.getPhotoType())); } - if (vcard.getPhoto() != null && !vcard.getPhoto().isEmpty()) { + if (vcard.getPhoto() != null) { photoElement.addNode(new XMLElement("BINVAL", "", Base64.encode(vcard.getPhoto()))); } queryElement.addNode(photoElement); diff --git a/src/com/isode/stroke/vcards/VCardStorage.java b/src/com/isode/stroke/vcards/VCardStorage.java index cb732ea..5ac095c 100644 --- a/src/com/isode/stroke/vcards/VCardStorage.java +++ b/src/com/isode/stroke/vcards/VCardStorage.java @@ -4,6 +4,7 @@ */ package com.isode.stroke.vcards; +import com.isode.stroke.base.ByteArray; import com.isode.stroke.crypto.CryptoProvider; import com.isode.stroke.elements.VCard; import com.isode.stroke.jid.JID; @@ -24,12 +25,13 @@ public abstract class VCardStorage { public void delete() {}; public String getPhotoHash(final JID jid) { - VCard vCard = getVCard(jid); - if (vCard != null && vCard.getPhoto().getSize() != 0) { - return Hexify.hexify(crypto.getSHA1Hash(vCard.getPhoto())); - } - else { - return ""; + final VCard vCard = getVCard(jid); + if (vCard != null) { + final ByteArray photo = vCard.getPhoto(); + if (photo != null) { + return Hexify.hexify(crypto.getSHA1Hash(photo)); + } } + return ""; } } -- cgit v0.10.2-6-g49f6