Merge "Use CollectionUtils for array concatenation in NsdService" into main

This commit is contained in:
Yuyang Huang 2024-12-11 17:09:46 -08:00 • committed by Gerrit Code Review
commit 23ffbcd2ee
3 changed files with 7 additions and 20 deletions

View file

@ -139,11 +139,8 @@ public class EnqueueMdnsQueryCallable implements Callable<Pair<Integer, List<Str
// Base service type
questions.add(new MdnsPointerRecord(serviceTypeLabels, expectUnicastResponse));
for (String subtype : subtypes) {
final String[] labels = new String[serviceTypeLabels.length + 2];
labels[0] = MdnsConstants.SUBTYPE_PREFIX + subtype;
labels[1] = MdnsConstants.SUBTYPE_LABEL;
System.arraycopy(serviceTypeLabels, 0, labels, 2, serviceTypeLabels.length);
final String[] labels = MdnsUtils.constructFullSubtype(serviceTypeLabels,
MdnsConstants.SUBTYPE_PREFIX + subtype);
questions.add(new MdnsPointerRecord(labels, expectUnicastResponse));
}
}

View file

@ -1482,22 +1482,14 @@ public class MdnsRecordRepository {
private static String[] splitFullyQualifiedName(
@NonNull NsdServiceInfo info, @NonNull String[] serviceType) {
final String[] split = new String[serviceType.length + 1];
split[0] = info.getServiceName();
System.arraycopy(serviceType, 0, split, 1, serviceType.length);
return split;
return CollectionUtils.prependArray(String.class, serviceType, info.getServiceName());
}
private static String[] splitServiceType(@NonNull NsdServiceInfo info) {
// String.split(pattern, 0) removes trailing empty strings, which would appear when
// splitting "domain.name." (with a dot a the end), so this is what is needed here.
final String[] split = info.getServiceType().split("\\.", 0);
final String[] type = new String[split.length + 1];
System.arraycopy(split, 0, type, 0, split.length);
type[split.length] = LOCAL_TLD;
return type;
return CollectionUtils.appendArray(String.class, split, LOCAL_TLD);
}
/** Returns whether there will be an SRV record when registering the {@code info}. */

View file

@ -28,6 +28,7 @@ import android.os.SystemClock;
import android.util.ArraySet;
import android.util.Pair;
import com.android.net.module.util.CollectionUtils;
import com.android.server.connectivity.mdns.MdnsConstants;
import com.android.server.connectivity.mdns.MdnsInetAddressRecord;
import com.android.server.connectivity.mdns.MdnsPacket;
@ -273,11 +274,8 @@ public class MdnsUtils {
* of ["_printer", "_sub", "_http", "_tcp"].
*/
public static String[] constructFullSubtype(String[] serviceType, String subtype) {
String[] fullSubtype = new String[serviceType.length + 2];
fullSubtype[0] = subtype;
fullSubtype[1] = MdnsConstants.SUBTYPE_LABEL;
System.arraycopy(serviceType, 0, fullSubtype, 2, serviceType.length);
return fullSubtype;
return CollectionUtils.prependArray(String.class, serviceType, subtype,
MdnsConstants.SUBTYPE_LABEL);
}
/** A wrapper class of {@link SystemClock} to be mocked in unit tests. */