From f4e462fabd366873cf0f1d6927469d6bac3bec0d Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Sat, 28 Apr 2018 18:35:12 +0200 Subject: Allow importing from zip file Signed-off-by: Jason A. Donenfeld --- app/src/main/java/com/wireguard/config/Config.java | 54 +++++++++++----------- 1 file changed, 27 insertions(+), 27 deletions(-) (limited to 'app/src/main/java/com/wireguard/config/Config.java') diff --git a/app/src/main/java/com/wireguard/config/Config.java b/app/src/main/java/com/wireguard/config/Config.java index e4731a0c..b38d2a13 100644 --- a/app/src/main/java/com/wireguard/config/Config.java +++ b/app/src/main/java/com/wireguard/config/Config.java @@ -97,36 +97,36 @@ public class Config implements Parcelable { in.readTypedList(peers, Peer.CREATOR); } - public static Config from(final InputStream stream) - throws IOException { + public static Config from(final InputStream stream) throws IOException { + return from(new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))); + } + + public static Config from(final BufferedReader reader) throws IOException { final Config config = new Config(); - try (BufferedReader reader = new BufferedReader( - new InputStreamReader(stream, StandardCharsets.UTF_8))) { - Peer currentPeer = null; - String line; - boolean inInterfaceSection = false; - while ((line = reader.readLine()) != null) { - if (line.isEmpty() || line.startsWith("#")) - continue; - if ("[Interface]".equals(line)) { - currentPeer = null; - inInterfaceSection = true; - } else if ("[Peer]".equals(line)) { - currentPeer = new Peer(); - config.peers.add(currentPeer); - inInterfaceSection = false; - } else if (inInterfaceSection) { - config.interfaceSection.parse(line); - } else if (currentPeer != null) { - currentPeer.parse(line); - } else { - throw new IllegalArgumentException("Invalid configuration line: " + line); - } - } - if (!inInterfaceSection && currentPeer == null) { - throw new IllegalArgumentException("Could not find any config information"); + Peer currentPeer = null; + String line; + boolean inInterfaceSection = false; + while ((line = reader.readLine()) != null) { + if (line.isEmpty() || line.startsWith("#")) + continue; + if ("[Interface]".equals(line)) { + currentPeer = null; + inInterfaceSection = true; + } else if ("[Peer]".equals(line)) { + currentPeer = new Peer(); + config.peers.add(currentPeer); + inInterfaceSection = false; + } else if (inInterfaceSection) { + config.interfaceSection.parse(line); + } else if (currentPeer != null) { + currentPeer.parse(line); + } else { + throw new IllegalArgumentException("Invalid configuration line: " + line); } } + if (!inInterfaceSection && currentPeer == null) { + throw new IllegalArgumentException("Could not find any config information"); + } return config; } -- cgit v1.2.3-59-g8ed1b