wireguard-android/app/src/main/java/com/wireguard/config/Profile.java
Samuel Holland ca708ba382 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.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2017-07-31 21:14:42 -05:00

100 lines
2.8 KiB
Java

package com.wireguard.config;
import android.databinding.BaseObservable;
import android.databinding.Bindable;
import android.databinding.Observable;
import android.databinding.ObservableArrayList;
import android.databinding.ObservableList;
import com.wireguard.android.BR;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
/**
* Represents a wg-quick profile.
*/
public class Profile extends BaseObservable implements Copyable<Profile>, Observable {
private final Interface iface = new Interface();
private boolean isConnected;
private final String name;
private final ObservableList<Peer> peers = new ObservableArrayList<>();
public Profile(String name) {
super();
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;
}
@Bindable
public boolean getIsConnected() {
return isConnected;
}
public String getName() {
return name;
}
public ObservableList<Peer> getPeers() {
return peers;
}
public void parseFrom(InputStream stream)
throws IOException {
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(stream, StandardCharsets.UTF_8))) {
Peer currentPeer = null;
String line;
while ((line = reader.readLine()) != null) {
if (line.equals("[Interface]")) {
currentPeer = null;
} else if (line.equals("[Peer]")) {
currentPeer = new Peer();
peers.add(currentPeer);
} else if (currentPeer == null) {
iface.parseFrom(line);
} else {
currentPeer.parseFrom(line);
}
}
}
}
public void setIsConnected(boolean isConnected) {
this.isConnected = isConnected;
notifyPropertyChanged(BR.isConnected);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder().append(iface.toString());
for (Peer peer : peers)
sb.append('\n').append(peer.toString());
return sb.toString();
}
}