All Downloads are FREE. Search and download functionalities are using the official Maven repository.

rocks.xmpp.dns.TxtRecord Maven / Gradle / Ivy

There is a newer version: 0.9.1
Show newest version
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014-2016 Christian Schudt
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package rocks.xmpp.dns;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * A DNS TXT resource record.
 *
 * @author Christian Schudt
 */
public final class TxtRecord implements Comparable {

    private final String txt;

    TxtRecord(final ByteBuffer data, final int length) {
        final StringBuilder sb = new StringBuilder();
        short i = 0;
        // http://tools.ietf.org/html/rfc7208#section-3.3
        // If a published record contains multiple strings, then the
        // record MUST be treated as if those strings are concatenated together
        // without adding spaces.
        while (i < length) {
            //  is a single
            // length octet followed by that number of characters.
            final int l = data.get() & 0xFF;
            final byte[] characterString = new byte[l];
            data.get(characterString);
            sb.append(new String(characterString, StandardCharsets.UTF_8));
            i += l + 1;
        }
        this.txt = sb.toString();
    }

    /**
     * Gets the text.
     *
     * @return The text.
     */
    public final String getText() {
        return txt;
    }

    /**
     * Gets the attributes of this record.
     *
     * @return The attributes or an empty map if there are no attributes.
     * @see RFC 1464
     */
    public final Map asAttributes() {

        // In other words, the
        // first unquoted equals sign in the TXT record is the name/value
        // delimiter.  All subsequent characters are part of the value.
        final Matcher matcher = Pattern.compile("(?




© 2015 - 2025 Weber Informatics LLC | Privacy Policy