From 71e8fd2e17ffdd8d63bfbd88441bd73be9562454 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Thu, 18 Jan 2024 10:41:34 +0100 Subject: [PATCH] [jid] Avoid creating a StringBuilder if possible JidCreate.from(String) would previously always invoke XmppStringUtils.completeJidFrom() which internally creates a new StringBuilder. This can be avoided. --- .../java/org/jxmpp/jid/impl/JidCreate.java | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/jxmpp-jid/src/main/java/org/jxmpp/jid/impl/JidCreate.java b/jxmpp-jid/src/main/java/org/jxmpp/jid/impl/JidCreate.java index cf11446..ff0af5d 100644 --- a/jxmpp-jid/src/main/java/org/jxmpp/jid/impl/JidCreate.java +++ b/jxmpp-jid/src/main/java/org/jxmpp/jid/impl/JidCreate.java @@ -152,12 +152,31 @@ public static Jid from(String localpart, String domainpart, String resource) thr * @throws XmppStringprepException if an error occurs. */ public static Jid from(String localpart, String domainpart, String resource, JxmppContext context) throws XmppStringprepException { + String jid = XmppStringUtils.completeJidFrom(localpart, domainpart, resource); + return from(localpart, domainpart, resource, jid, context); + } + + /** + * Get a {@link Jid} from the given parts. + + * + * @param localpart a optional localpart. + * @param domainpart a required domainpart. + * @param resource a optional resourcepart. + * @param jidString the raw String of the as parsed. + * @param context the JXMPP context. + * @return a JID which consists of the given parts. + * @throws XmppStringprepException if an error occurs. + */ + private static Jid from(String localpart, String domainpart, String resource, String jidString, JxmppContext context) throws XmppStringprepException { // Every JID must come with an domainpart. if (domainpart.isEmpty()) { throw XmppStringprepException.MissingDomainpart.from(localpart, resource); } - String jidString = XmppStringUtils.completeJidFrom(localpart, domainpart, resource); + // The provided jidString must be equal to the assembled parts. + assert jidString.equals(XmppStringUtils.completeJidFrom(localpart, domainpart, resource)); + Jid jid; JidStringAndStringprep jidStringAndStringprep = null; @@ -245,7 +264,7 @@ public static Jid from(String jidString, JxmppContext context) throws XmppString String domainpart = XmppStringUtils.parseDomain(jidString); String resource = XmppStringUtils.parseResource(jidString); try { - return from(localpart, domainpart, resource, context); + return from(localpart, domainpart, resource, jidString, context); } catch (XmppStringprepException e) { throw new XmppStringprepException(jidString, e); }