summaryrefslogtreecommitdiffhomepage
path: root/app
diff options
context:
space:
mode:
authorSamuel Holland <samuel@sholland.org>2017-07-31 21:14:42 -0500
committerSamuel Holland <samuel@sholland.org>2017-07-31 21:14:42 -0500
commite0ed0b56133f1fe4efe939751def9ac1f922847d (patch)
tree8cbeac7533e851fd68377363177fe6ba4b7615e6 /app
parent392673f027f34c3fdd9ecdea8967d3425363e780 (diff)
Profile: Implement deep copying
This is a simple, naive implementation that {,de}serializes the profile's state, but it does not depend on the internal representation of Profile or its contained classes.
Diffstat (limited to 'app')
-rw-r--r--app/src/main/java/com/wireguard/config/Profile.java18
1 files changed, 17 insertions, 1 deletions
diff --git a/app/src/main/java/com/wireguard/config/Profile.java b/app/src/main/java/com/wireguard/config/Profile.java
index e6506c45..70781195 100644
--- a/app/src/main/java/com/wireguard/config/Profile.java
+++ b/app/src/main/java/com/wireguard/config/Profile.java
@@ -19,7 +19,7 @@ import java.nio.charset.StandardCharsets;
* Represents a wg-quick profile.
*/
-public class Profile extends BaseObservable implements Observable {
+public class Profile extends BaseObservable implements Copyable<Profile>, Observable {
private final Interface iface = new Interface();
private boolean isConnected;
private final String name;
@@ -30,6 +30,22 @@ public class Profile extends BaseObservable implements Observable {
this.name = name;
}
+ private Profile(Profile original)
+ throws IOException {
+ this(original.getName());
+ final byte configBytes[] = original.toString().getBytes(StandardCharsets.UTF_8);
+ final ByteArrayInputStream configStream = new ByteArrayInputStream(configBytes);
+ parseFrom(configStream);
+ }
+
+ public Profile copy() {
+ try {
+ return new Profile(this);
+ } catch (IOException e) {
+ return null;
+ }
+ }
+
public Interface getInterface() {
return iface;
}