Methods Summary |
---|
public int | describeContents()Implement the Parcelable interface
return 0;
|
public java.util.Map | getAttributes()Retrive attributes as a map of String keys to byte[] values.
The returned map is unmodifiable; changes must be made through {@link #setAttribute} and
{@link #removeAttribute}.
return Collections.unmodifiableMap(mTxtRecord);
|
public java.net.InetAddress | getHost()Get the host address. The host address is valid for a resolved service.
return mHost;
|
public int | getPort()Get port number. The port number is valid for a resolved service.
return mPort;
|
public java.lang.String | getServiceName()Get the service name
return mServiceName;
|
public java.lang.String | getServiceType()Get the service type
return mServiceType;
|
public byte[] | getTxtRecord()
int txtRecordSize = getTxtRecordSize();
if (txtRecordSize == 0) {
return null;
}
byte[] txtRecord = new byte[txtRecordSize];
int ptr = 0;
for (Map.Entry<String, byte[]> entry : mTxtRecord.entrySet()) {
String key = entry.getKey();
byte[] value = entry.getValue();
// One byte to record the length of this key/value pair.
txtRecord[ptr++] = (byte) (key.length() + (value == null ? 0 : value.length) + 1);
// The key, in US-ASCII.
// Note: use the StandardCharsets const here because it doesn't raise exceptions and we
// already know the key is ASCII at this point.
System.arraycopy(key.getBytes(StandardCharsets.US_ASCII), 0, txtRecord, ptr,
key.length());
ptr += key.length();
// US-ASCII '=' character.
txtRecord[ptr++] = (byte)'=";
// The value, as any raw bytes.
if (value != null) {
System.arraycopy(value, 0, txtRecord, ptr, value.length);
ptr += value.length;
}
}
return txtRecord;
|
private int | getTxtRecordSize()
int txtRecordSize = 0;
for (Map.Entry<String, byte[]> entry : mTxtRecord.entrySet()) {
txtRecordSize += 2; // One for the length byte, one for the = between key and value.
txtRecordSize += entry.getKey().length();
byte[] value = entry.getValue();
txtRecordSize += value == null ? 0 : value.length;
}
return txtRecordSize;
|
public void | removeAttribute(java.lang.String key)Remove an attribute by key
mTxtRecord.remove(key);
|
public void | setAttribute(java.lang.String key, byte[] value)
// Key must be printable US-ASCII, excluding =.
for (int i = 0; i < key.length(); ++i) {
char character = key.charAt(i);
if (character < 0x20 || character > 0x7E) {
throw new IllegalArgumentException("Key strings must be printable US-ASCII");
} else if (character == 0x3D) {
throw new IllegalArgumentException("Key strings must not include '='");
}
}
// Key length + value length must be < 255.
if (key.length() + (value == null ? 0 : value.length) >= 255) {
throw new IllegalArgumentException("Key length + value length must be < 255 bytes");
}
// Warn if key is > 9 characters, as recommended by RFC 6763 section 6.4.
if (key.length() > 9) {
Log.w(TAG, "Key lengths > 9 are discouraged: " + key);
}
// Check against total TXT record size limits.
// Arbitrary 400 / 1300 byte limits taken from RFC 6763 section 6.2.
int txtRecordSize = getTxtRecordSize();
int futureSize = txtRecordSize + key.length() + (value == null ? 0 : value.length) + 2;
if (futureSize > 1300) {
throw new IllegalArgumentException("Total length of attributes must be < 1300 bytes");
} else if (futureSize > 400) {
Log.w(TAG, "Total length of all attributes exceeds 400 bytes; truncation may occur");
}
mTxtRecord.put(key, value);
|
public void | setAttribute(java.lang.String key, java.lang.String value)Add a service attribute as a key/value pair.
Service attributes are included as DNS-SD TXT record pairs.
The key must be US-ASCII printable characters, excluding the '=' character. Values may
be UTF-8 strings or null. The total length of key + value must be less than 255 bytes.
Keys should be short, ideally no more than 9 characters, and unique per instance of
{@link NsdServiceInfo}. Calling {@link #setAttribute} twice with the same key will overwrite
first value.
try {
setAttribute(key, value == null ? (byte []) null : value.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException("Value must be UTF-8");
}
|
public void | setHost(java.net.InetAddress s)Set the host address
mHost = s;
|
public void | setPort(int p)Set port number
mPort = p;
|
public void | setServiceName(java.lang.String s)Set the service name
mServiceName = s;
|
public void | setServiceType(java.lang.String s)Set the service type
mServiceType = s;
|
public java.lang.String | toString()
StringBuffer sb = new StringBuffer();
sb.append("name: ").append(mServiceName)
.append(", type: ").append(mServiceType)
.append(", host: ").append(mHost)
.append(", port: ").append(mPort);
byte[] txtRecord = getTxtRecord();
if (txtRecord != null) {
sb.append(", txtRecord: ").append(new String(txtRecord, StandardCharsets.UTF_8));
}
return sb.toString();
|
public void | writeToParcel(android.os.Parcel dest, int flags)Implement the Parcelable interface
dest.writeString(mServiceName);
dest.writeString(mServiceType);
if (mHost != null) {
dest.writeInt(1);
dest.writeByteArray(mHost.getAddress());
} else {
dest.writeInt(0);
}
dest.writeInt(mPort);
// TXT record key/value pairs.
dest.writeInt(mTxtRecord.size());
for (String key : mTxtRecord.keySet()) {
byte[] value = mTxtRecord.get(key);
if (value != null) {
dest.writeInt(1);
dest.writeInt(value.length);
dest.writeByteArray(value);
} else {
dest.writeInt(0);
}
dest.writeString(key);
}
|