summaryrefslogtreecommitdiffhomepage
path: root/app/src/main/java/com/wireguard/config
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2018-12-08 03:47:43 +0100
committerJason A. Donenfeld <Jason@zx2c4.com>2018-12-08 03:48:50 +0100
commit5aa8191cd3e93e5b128d2c865e2d542a61a770c9 (patch)
tree51868549be443d3b8aa5c4d0c7d3c9bdabcaa59f /app/src/main/java/com/wireguard/config
parentbb43804d58ac0b055b3f26dc607a1134f4714488 (diff)
Do not close zip input stream
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'app/src/main/java/com/wireguard/config')
-rw-r--r--app/src/main/java/com/wireguard/config/Config.java87
1 files changed, 48 insertions, 39 deletions
diff --git a/app/src/main/java/com/wireguard/config/Config.java b/app/src/main/java/com/wireguard/config/Config.java
index 7645583d..6c9d7ba3 100644
--- a/app/src/main/java/com/wireguard/config/Config.java
+++ b/app/src/main/java/com/wireguard/config/Config.java
@@ -43,50 +43,59 @@ public final class Config {
* @return a {@code Config} instance representing the supplied configuration
*/
public static Config parse(final InputStream stream) throws IOException, ParseException {
+ return parse(new BufferedReader(new InputStreamReader(stream)));
+ }
+
+ /**
+ * Parses an series of "Interface" and "Peer" sections into a {@code Config}. Throws
+ * {@link ParseException} if the input is not well-formed or contains unparseable sections.
+ *
+ * @param reader a BufferedReader of UTF-8 text that is interpreted as a WireGuard configuration file
+ * @return a {@code Config} instance representing the supplied configuration
+ */
+ public static Config parse(final BufferedReader reader) throws IOException, ParseException {
final Builder builder = new Builder();
- try (final BufferedReader reader = new BufferedReader(new InputStreamReader(stream))) {
- final Collection<String> interfaceLines = new ArrayList<>();
- final Collection<String> peerLines = new ArrayList<>();
- boolean inInterfaceSection = false;
- boolean inPeerSection = false;
- @Nullable String line;
- while ((line = reader.readLine()) != null) {
- final int commentIndex = line.indexOf('#');
- if (commentIndex != -1)
- line = line.substring(0, commentIndex);
- line = line.trim();
- if (line.isEmpty())
- continue;
- if (line.startsWith("[")) {
- // Consume all [Peer] lines read so far.
- if (inPeerSection) {
- builder.parsePeer(peerLines);
- peerLines.clear();
- }
- if ("[Interface]".equalsIgnoreCase(line)) {
- inInterfaceSection = true;
- inPeerSection = false;
- } else if ("[Peer]".equalsIgnoreCase(line)) {
- inInterfaceSection = false;
- inPeerSection = true;
- } else {
- throw new ParseException("top level", line, "Unknown section name");
- }
- } else if (inInterfaceSection) {
- interfaceLines.add(line);
- } else if (inPeerSection) {
- peerLines.add(line);
+ final Collection<String> interfaceLines = new ArrayList<>();
+ final Collection<String> peerLines = new ArrayList<>();
+ boolean inInterfaceSection = false;
+ boolean inPeerSection = false;
+ @Nullable String line;
+ while ((line = reader.readLine()) != null) {
+ final int commentIndex = line.indexOf('#');
+ if (commentIndex != -1)
+ line = line.substring(0, commentIndex);
+ line = line.trim();
+ if (line.isEmpty())
+ continue;
+ if (line.startsWith("[")) {
+ // Consume all [Peer] lines read so far.
+ if (inPeerSection) {
+ builder.parsePeer(peerLines);
+ peerLines.clear();
+ }
+ if ("[Interface]".equalsIgnoreCase(line)) {
+ inInterfaceSection = true;
+ inPeerSection = false;
+ } else if ("[Peer]".equalsIgnoreCase(line)) {
+ inInterfaceSection = false;
+ inPeerSection = true;
} else {
- throw new ParseException("top level", line, "Expected [Interface] or [Peer]");
+ throw new ParseException("top level", line, "Unknown section name");
}
+ } else if (inInterfaceSection) {
+ interfaceLines.add(line);
+ } else if (inPeerSection) {
+ peerLines.add(line);
+ } else {
+ throw new ParseException("top level", line, "Expected [Interface] or [Peer]");
}
- if (inPeerSection)
- builder.parsePeer(peerLines);
- else if (!inInterfaceSection)
- throw new ParseException("top level", "", "Empty configuration");
- // Combine all [Interface] sections in the file.
- builder.parseInterface(interfaceLines);
}
+ if (inPeerSection)
+ builder.parsePeer(peerLines);
+ else if (!inInterfaceSection)
+ throw new ParseException("top level", "", "Empty configuration");
+ // Combine all [Interface] sections in the file.
+ builder.parseInterface(interfaceLines);
return builder.build();
}