config: Minor cleanup

- Stop implicitly assuming locales in String.format
- Cleanup method visibilities
- Improve uses of Integer methods
- Remove unused getToken method

Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
Harsh Shandilya 2018-04-27 22:03:39 +05:30 committed by Jason A. Donenfeld
parent ba862b166b
commit 217ab5e17f
4 changed files with 23 additions and 18 deletions

View File

@ -7,6 +7,7 @@ import java.lang.reflect.Method;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -88,17 +89,13 @@ enum Attribute {
}
public String composeWith(final int value) {
return String.format("%s = %d%n", token, value);
return String.format(Locale.getDefault(), "%s = %d%n", token, value);
}
public <T> String composeWith(final List<T> value) {
return String.format("%s = %s%n", token, listToString(value));
}
public String getToken() {
return token;
}
public String parse(final CharSequence line) {
final Matcher matcher = pattern.matcher(line);
return matcher.matches() ? matcher.group(1) : null;

View File

@ -6,10 +6,11 @@ import android.os.Parcelable;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.util.Locale;
public class IPCidr implements Parcelable {
InetAddress address;
int cidr;
private InetAddress address;
private int cidr;
public static final Parcelable.Creator<IPCidr> CREATOR = new Parcelable.Creator<IPCidr>() {
@ -24,7 +25,7 @@ public class IPCidr implements Parcelable {
}
};
public IPCidr(String in) {
IPCidr(String in) {
parse(in);
}
@ -35,7 +36,7 @@ public class IPCidr implements Parcelable {
try {
cidr = Integer.parseInt(in.substring(slash + 1), 10);
in = in.substring(0, slash);
} catch (Exception e) {
} catch (Exception ignored) {
}
}
address = Attribute.parseIPString(in);
@ -55,7 +56,7 @@ public class IPCidr implements Parcelable {
@Override
public String toString() {
return String.format("%s/%d", address.getHostAddress(), cidr);
return String.format(Locale.getDefault(), "%s/%d", address.getHostAddress(), cidr);
}
@Override
@ -71,7 +72,7 @@ public class IPCidr implements Parcelable {
private IPCidr(final Parcel in) {
try {
parse(in.readString());
} catch (Exception e) {
} catch (Exception ignored) {
}
}

View File

@ -151,7 +151,7 @@ public class Interface implements Parcelable {
for (int i = 0; i < dnsItems; ++i) {
try {
dnsList.add(InetAddress.getByAddress(in.createByteArray()));
} catch (Exception e) {
} catch (Exception ignored) {
}
}
listenPort = in.readInt();
@ -198,7 +198,7 @@ public class Interface implements Parcelable {
private String getListenPortString() {
if (listenPort == 0)
return null;
return new Integer(listenPort).toString();
return Integer.valueOf(listenPort).toString();
}
public int getMtu() {
@ -208,7 +208,7 @@ public class Interface implements Parcelable {
private String getMtuString() {
if (mtu == 0)
return null;
return new Integer(mtu).toString();
return Integer.toString(mtu);
}
public String getPrivateKey() {

View File

@ -15,6 +15,7 @@ import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
/**
* Represents the configuration for a WireGuard peer (a [Peer] block).
@ -167,7 +168,7 @@ public class Peer implements Parcelable {
private String getEndpointString() {
if (endpoint == null)
return null;
return String.format("%s:%d", endpoint.getHostString(), endpoint.getPort());
return String.format(Locale.getDefault(), "%s:%d", endpoint.getHostString(), endpoint.getPort());
}
public String getResolvedEndpointString() throws UnknownHostException {
@ -178,8 +179,14 @@ public class Peer implements Parcelable {
if (endpoint.isUnresolved())
throw new UnknownHostException(endpoint.getHostString());
if (endpoint.getAddress() instanceof Inet6Address)
return String.format("[%s]:%d", endpoint.getAddress().getHostAddress(), endpoint.getPort());
return String.format("%s:%d", endpoint.getAddress().getHostAddress(), endpoint.getPort());
return String.format(Locale.getDefault(),
"[%s]:%d",
endpoint.getAddress().getHostAddress(),
endpoint.getPort());
return String.format(Locale.getDefault(),
"%s:%d",
endpoint.getAddress().getHostAddress(),
endpoint.getPort());
}
public int getPersistentKeepalive() {
@ -189,7 +196,7 @@ public class Peer implements Parcelable {
private String getPersistentKeepaliveString() {
if (persistentKeepalive == 0)
return null;
return new Integer(persistentKeepalive).toString();
return Integer.valueOf(persistentKeepalive).toString();
}
public String getPreSharedKey() {