FileDocCategorySizeDatePackage
DnsSdTxtRecord.javaAPI DocAndroid 5.1 API10002Thu Mar 12 22:22:10 GMT 2015android.net.nsd

DnsSdTxtRecord

public class DnsSdTxtRecord extends Object implements android.os.Parcelable
This class handles TXT record data for DNS based service discovery as specified at http://tools.ietf.org/html/draft-cheshire-dnsext-dns-sd-11 DNS-SD specifies that a TXT record corresponding to an SRV record consist of a packed array of bytes, each preceded by a length byte. Each string is an attribute-value pair. The DnsSdTxtRecord object stores the entire TXT data as a single byte array, traversing it as need be to implement its various methods.
hide

Fields Summary
private static final byte
mSeperator
private byte[]
mData
public static final Creator
CREATOR
Implement the Parcelable interface
Constructors Summary
public DnsSdTxtRecord()
Constructs a new, empty TXT record.


           
       
        mData = new byte[0];
    
public DnsSdTxtRecord(byte[] data)
Constructs a new TXT record from a byte array in the standard format.

        mData = (byte[]) data.clone();
    
public DnsSdTxtRecord(DnsSdTxtRecord src)
Copy constructor

        if (src != null && src.mData != null) {
            mData = (byte[]) src.mData.clone();
        }
    
Methods Summary
public booleancontains(java.lang.String key)
Return true if key is present, false if not.

        String s = null;
        for (int i = 0; null != (s = this.getKey(i)); i++) {
            if (0 == key.compareToIgnoreCase(s)) return true;
        }
        return false;
    
public intdescribeContents()
Implement the Parcelable interface

        return 0;
    
public booleanequals(java.lang.Object o)

        if (o == this) {
            return true;
        }
        if (!(o instanceof DnsSdTxtRecord)) {
            return false;
        }

        DnsSdTxtRecord record = (DnsSdTxtRecord)o;
        return  Arrays.equals(record.mData, mData);
    
public java.lang.Stringget(java.lang.String key)
Get a value for a key

param
key
return
The value associated with the key

        byte[] val = this.getValue(key);
        return val != null ? new String(val) : null;
    
private java.lang.StringgetKey(int index)
Return a key in the TXT record by zero-based index. Returns null if index exceeds the total number of keys.

        int avStart = 0;

        for (int i=0; i < index && avStart < mData.length; i++) {
            avStart += mData[avStart] + 1;
        }

        if (avStart < mData.length) {
            int avLen = mData[avStart];
            int aLen = 0;

            for (aLen=0; aLen < avLen; aLen++) {
                if (mData[avStart + aLen + 1] == mSeperator) break;
            }
            return new String(mData, avStart + 1, aLen);
        }
        return null;
    
public byte[]getRawData()

        return (byte[]) mData.clone();
    
private byte[]getValue(int index)
Look up a key in the TXT record by zero-based index and return its value. Returns null if index exceeds the total number of keys. Returns null if the key is present with no value.

        int avStart = 0;
        byte[] value = null;

        for (int i=0; i < index && avStart < mData.length; i++) {
            avStart += mData[avStart] + 1;
        }

        if (avStart < mData.length) {
            int avLen = mData[avStart];
            int aLen = 0;

            for (aLen=0; aLen < avLen; aLen++) {
                if (mData[avStart + aLen + 1] == mSeperator) {
                    value = new byte[avLen - aLen - 1];
                    System.arraycopy(mData, avStart + aLen + 2, value, 0, avLen - aLen - 1);
                    break;
                }
            }
        }
        return value;
    
private byte[]getValue(java.lang.String forKey)

        String s = null;
        int i;

        for (i = 0; null != (s = this.getKey(i)); i++) {
            if (0 == forKey.compareToIgnoreCase(s)) {
                return this.getValue(i);
            }
        }

        return null;
    
private java.lang.StringgetValueAsString(int index)

        byte[] value = this.getValue(index);
        return value != null ? new String(value) : null;
    
public inthashCode()

        return Arrays.hashCode(mData);
    
private voidinsert(byte[] keyBytes, byte[] value, int index)

        byte[] oldBytes = mData;
        int valLen = (value != null) ? value.length : 0;
        int insertion = 0;
        int newLen, avLen;

        for (int i = 0; i < index && insertion < mData.length; i++) {
            insertion += (0xFF & (mData[insertion] + 1));
        }

        avLen = keyBytes.length + valLen + (value != null ? 1 : 0);
        newLen = avLen + oldBytes.length + 1;

        mData = new byte[newLen];
        System.arraycopy(oldBytes, 0, mData, 0, insertion);
        int secondHalfLen = oldBytes.length - insertion;
        System.arraycopy(oldBytes, insertion, mData, newLen - secondHalfLen, secondHalfLen);
        mData[insertion] = (byte) avLen;
        System.arraycopy(keyBytes, 0, mData, insertion + 1, keyBytes.length);
        if (value != null) {
            mData[insertion + 1 + keyBytes.length] = mSeperator;
            System.arraycopy(value, 0, mData, insertion + keyBytes.length + 2, valLen);
        }
    
public intkeyCount()
Return the count of keys

        int count = 0, nextKey;
        for (nextKey = 0; nextKey < mData.length; count++) {
            nextKey += (0xFF & (mData[nextKey] + 1));
        }
        return count;
    
public intremove(java.lang.String key)
Remove a key/value pair. If found, returns the index or -1 if not found

        int avStart = 0;

        for (int i=0; avStart < mData.length; i++) {
            int avLen = mData[avStart];
            if (key.length() <= avLen &&
                    (key.length() == avLen || mData[avStart + key.length() + 1] == mSeperator)) {
                String s = new String(mData, avStart + 1, key.length());
                if (0 == key.compareToIgnoreCase(s)) {
                    byte[] oldBytes = mData;
                    mData = new byte[oldBytes.length - avLen - 1];
                    System.arraycopy(oldBytes, 0, mData, 0, avStart);
                    System.arraycopy(oldBytes, avStart + avLen + 1, mData, avStart,
                            oldBytes.length - avStart - avLen - 1);
                    return i;
                }
            }
            avStart += (0xFF & (avLen + 1));
        }
        return -1;
    
public voidset(java.lang.String key, java.lang.String value)
Set a key/value pair. Setting an existing key will replace its value.

param
key Must be ascii with no '='
param
value matching value to key

        byte[] keyBytes;
        byte[] valBytes;
        int valLen;

        if (value != null) {
            valBytes = value.getBytes();
            valLen = valBytes.length;
        } else {
            valBytes = null;
            valLen = 0;
        }

        try {
            keyBytes = key.getBytes("US-ASCII");
        }
        catch (java.io.UnsupportedEncodingException e) {
            throw new IllegalArgumentException("key should be US-ASCII");
        }

        for (int i = 0; i < keyBytes.length; i++) {
            if (keyBytes[i] == '=") {
                throw new IllegalArgumentException("= is not a valid character in key");
            }
        }

        if (keyBytes.length + valLen >= 255) {
            throw new IllegalArgumentException("Key and Value length cannot exceed 255 bytes");
        }

        int currentLoc = remove(key);
        if (currentLoc == -1)
            currentLoc = keyCount();

        insert(keyBytes, valBytes, currentLoc);
    
public intsize()

        return mData.length;
    
public java.lang.StringtoString()
Return a string representation. Example : {key1=value1},{key2=value2}.. For a key say like "key3" with null value {key1=value1},{key2=value2}{key3}

        String a, result = null;

        for (int i = 0; null != (a = this.getKey(i)); i++) {
            String av =  "{" + a;
            String val = this.getValueAsString(i);
            if (val != null)
                av += "=" + val + "}";
            else
                av += "}";
            if (result == null)
                result = av;
            else
                result = result + ", " + av;
        }
        return result != null ? result : "";
    
public voidwriteToParcel(android.os.Parcel dest, int flags)
Implement the Parcelable interface

        dest.writeByteArray(mData);