-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWalletV2Serializer.java
50 lines (42 loc) · 2.06 KB
/
WalletV2Serializer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import com.google.gson.*;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.io.*;
import java.lang.reflect.Type;
import java.util.Base64;
public class WalletV2Serializer implements WalletSerializer {
@Override
public StoredWallet read(InputStream inputStream) {
try (InputStreamReader inReader = new InputStreamReader(inputStream, "UTF-8");
JsonReader reader = new JsonReader(inReader)) {
Gson gson = new GsonBuilder().registerTypeHierarchyAdapter(byte[].class,
new ByteArrayToBase64TypeAdapter()).create();
return gson.fromJson(reader, StoredWallet.class);
} catch (IOException e) {
System.err.println("Unable to read wallet file");
e.printStackTrace();
return null;
} catch (JsonSyntaxException e) {
System.err.println("Wallet file is not in the format expected by the serializer");
e.printStackTrace();
return null;
}
}
@Override
public void write(StoredWallet wallet, OutputStream outputStream) throws IOException {
try (OutputStreamWriter outWriter = new OutputStreamWriter(outputStream, "UTF-8");
JsonWriter writer = new JsonWriter(outWriter)) {
Gson gson = new GsonBuilder().registerTypeHierarchyAdapter(byte[].class,
new ByteArrayToBase64TypeAdapter()).create();
gson.toJson(wallet, StoredWallet.class, writer);
}
}
private static class ByteArrayToBase64TypeAdapter implements JsonSerializer<byte[]>, JsonDeserializer<byte[]> {
public byte[] deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
return Base64.getDecoder().decode(json.getAsString());
}
public JsonElement serialize(byte[] src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(Base64.getEncoder().encodeToString(src));
}
}
}