- private SignalServiceEnvelope loadEnvelope(File file) throws IOException {
- try (FileInputStream f = new FileInputStream(file)) {
- DataInputStream in = new DataInputStream(f);
- int version = in.readInt();
- if (version > 2) {
- return null;
- }
- int type = in.readInt();
- String source = in.readUTF();
- int sourceDevice = in.readInt();
- if (version == 1) {
- // read legacy relay field
- in.readUTF();
- }
- long timestamp = in.readLong();
- byte[] content = null;
- int contentLen = in.readInt();
- if (contentLen > 0) {
- content = new byte[contentLen];
- in.readFully(content);
- }
- byte[] legacyMessage = null;
- int legacyMessageLen = in.readInt();
- if (legacyMessageLen > 0) {
- legacyMessage = new byte[legacyMessageLen];
- in.readFully(legacyMessage);
- }
- long serverTimestamp = 0;
- String uuid = null;
- if (version == 2) {
- serverTimestamp = in.readLong();
- uuid = in.readUTF();
- if ("".equals(uuid)) {
- uuid = null;
- }
- }
- return new SignalServiceEnvelope(type, source, sourceDevice, timestamp, legacyMessage, content, serverTimestamp, uuid);
- }
- }
-
- private void storeEnvelope(SignalServiceEnvelope envelope, File file) throws IOException {
- try (FileOutputStream f = new FileOutputStream(file)) {
- try (DataOutputStream out = new DataOutputStream(f)) {
- out.writeInt(2); // version
- out.writeInt(envelope.getType());
- out.writeUTF(envelope.getSource());
- out.writeInt(envelope.getSourceDevice());
- out.writeLong(envelope.getTimestamp());
- if (envelope.hasContent()) {
- out.writeInt(envelope.getContent().length);
- out.write(envelope.getContent());
- } else {
- out.writeInt(0);
- }
- if (envelope.hasLegacyMessage()) {
- out.writeInt(envelope.getLegacyMessage().length);
- out.write(envelope.getLegacyMessage());
- } else {
- out.writeInt(0);
- }
- out.writeLong(envelope.getServerTimestamp());
- String uuid = envelope.getUuid();
- out.writeUTF(uuid == null ? "" : uuid);
- }
- }
- }
-