Merge "Fix empty value problem in the TextEntry"
This commit is contained in:
@@ -270,7 +270,8 @@ public class MdnsServiceInfo implements Parcelable {
|
|||||||
public Map<String, String> getAttributes() {
|
public Map<String, String> getAttributes() {
|
||||||
Map<String, String> map = new HashMap<>(attributes.size());
|
Map<String, String> map = new HashMap<>(attributes.size());
|
||||||
for (Map.Entry<String, byte[]> kv : attributes.entrySet()) {
|
for (Map.Entry<String, byte[]> kv : attributes.entrySet()) {
|
||||||
map.put(kv.getKey(), new String(kv.getValue(), UTF_8));
|
final byte[] value = kv.getValue();
|
||||||
|
map.put(kv.getKey(), value == null ? null : new String(value, UTF_8));
|
||||||
}
|
}
|
||||||
return Collections.unmodifiableMap(map);
|
return Collections.unmodifiableMap(map);
|
||||||
}
|
}
|
||||||
@@ -342,7 +343,7 @@ public class MdnsServiceInfo implements Parcelable {
|
|||||||
// 2. If there is no '=' in a DNS-SD TXT record string, then it is a
|
// 2. If there is no '=' in a DNS-SD TXT record string, then it is a
|
||||||
// boolean attribute, simply identified as being present, with no value.
|
// boolean attribute, simply identified as being present, with no value.
|
||||||
if (delimitPos < 0) {
|
if (delimitPos < 0) {
|
||||||
return new TextEntry(new String(textBytes, US_ASCII), "");
|
return new TextEntry(new String(textBytes, US_ASCII), (byte[]) null);
|
||||||
} else if (delimitPos == 0) {
|
} else if (delimitPos == 0) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -353,13 +354,13 @@ public class MdnsServiceInfo implements Parcelable {
|
|||||||
|
|
||||||
/** Creates a new {@link TextEntry} with given key and value of a UTF-8 string. */
|
/** Creates a new {@link TextEntry} with given key and value of a UTF-8 string. */
|
||||||
public TextEntry(String key, String value) {
|
public TextEntry(String key, String value) {
|
||||||
this(key, value.getBytes(UTF_8));
|
this(key, value == null ? null : value.getBytes(UTF_8));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Creates a new {@link TextEntry} with given key and value of a byte array. */
|
/** Creates a new {@link TextEntry} with given key and value of a byte array. */
|
||||||
public TextEntry(String key, byte[] value) {
|
public TextEntry(String key, byte[] value) {
|
||||||
this.key = key;
|
this.key = key;
|
||||||
this.value = value.clone();
|
this.value = value == null ? null : value.clone();
|
||||||
}
|
}
|
||||||
|
|
||||||
private TextEntry(Parcel in) {
|
private TextEntry(Parcel in) {
|
||||||
@@ -372,17 +373,24 @@ public class MdnsServiceInfo implements Parcelable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public byte[] getValue() {
|
public byte[] getValue() {
|
||||||
return value.clone();
|
return value == null ? null : value.clone();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Converts this {@link TextEntry} instance to '=' separated byte array. */
|
/** Converts this {@link TextEntry} instance to '=' separated byte array. */
|
||||||
public byte[] toBytes() {
|
public byte[] toBytes() {
|
||||||
return ByteUtils.concat(key.getBytes(US_ASCII), new byte[]{'='}, value);
|
final byte[] keyBytes = key.getBytes(US_ASCII);
|
||||||
|
if (value == null) {
|
||||||
|
return keyBytes;
|
||||||
|
}
|
||||||
|
return ByteUtils.concat(keyBytes, new byte[]{'='}, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Converts this {@link TextEntry} instance to '=' separated string. */
|
/** Converts this {@link TextEntry} instance to '=' separated string. */
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
|
if (value == null) {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
return key + "=" + new String(value, UTF_8);
|
return key + "=" + new String(value, UTF_8);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -164,7 +164,8 @@ public class MdnsServiceInfoTest {
|
|||||||
List.of("vn=Alphabet Inc.", "mn=Google Nest Hub Max", "id=12345"),
|
List.of("vn=Alphabet Inc.", "mn=Google Nest Hub Max", "id=12345"),
|
||||||
List.of(
|
List.of(
|
||||||
MdnsServiceInfo.TextEntry.fromString("vn=Google Inc."),
|
MdnsServiceInfo.TextEntry.fromString("vn=Google Inc."),
|
||||||
MdnsServiceInfo.TextEntry.fromString("mn=Google Nest Hub Max")));
|
MdnsServiceInfo.TextEntry.fromString("mn=Google Nest Hub Max"),
|
||||||
|
MdnsServiceInfo.TextEntry.fromString("test=")));
|
||||||
|
|
||||||
beforeParcel.writeToParcel(parcel, 0);
|
beforeParcel.writeToParcel(parcel, 0);
|
||||||
parcel.setDataPosition(0);
|
parcel.setDataPosition(0);
|
||||||
@@ -208,11 +209,11 @@ public class MdnsServiceInfoTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void textEntry_fromStringWithoutAssignPunc_valueisEmpty() {
|
public void textEntry_fromStringWithoutAssignPunc_noValue() {
|
||||||
TextEntry entry = TextEntry.fromString("AA");
|
TextEntry entry = TextEntry.fromString("AA");
|
||||||
|
|
||||||
assertEquals("AA", entry.getKey());
|
assertEquals("AA", entry.getKey());
|
||||||
assertArrayEquals(new byte[] {}, entry.getValue());
|
assertNull(entry.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -241,11 +242,11 @@ public class MdnsServiceInfoTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void textEntry_fromBytesWithoutAssignPunc_valueisEmpty() {
|
public void textEntry_fromBytesWithoutAssignPunc_noValue() {
|
||||||
TextEntry entry = TextEntry.fromBytes(new byte[] {'A', 'A'});
|
TextEntry entry = TextEntry.fromBytes(new byte[] {'A', 'A'});
|
||||||
|
|
||||||
assertEquals("AA", entry.getKey());
|
assertEquals("AA", entry.getKey());
|
||||||
assertArrayEquals(new byte[] {}, entry.getValue());
|
assertNull(entry.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -270,4 +271,12 @@ public class MdnsServiceInfoTest {
|
|||||||
assertEquals(new TextEntry("BB", "xxyyzz"), new TextEntry("BB", "xxyyzz"));
|
assertEquals(new TextEntry("BB", "xxyyzz"), new TextEntry("BB", "xxyyzz"));
|
||||||
assertEquals(new TextEntry("AA", "XXYYZZ"), new TextEntry("AA", "XXYYZZ"));
|
assertEquals(new TextEntry("AA", "XXYYZZ"), new TextEntry("AA", "XXYYZZ"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void textEntry_fromString_valueIsEmpty() {
|
||||||
|
TextEntry entry = TextEntry.fromString("AA=");
|
||||||
|
|
||||||
|
assertEquals("AA", entry.getKey());
|
||||||
|
assertArrayEquals(new byte[] {}, entry.getValue());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user