All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.whispersystems.signalservice.api.messages.multidevice.DeviceGroupsOutputStream Maven / Gradle / Ivy

There is a newer version: 2.15.3
Show newest version
/**
 * Copyright (C) 2014-2016 Open Whisper Systems
 *
 * Licensed according to the LICENSE file in this repository.
 */

package org.whispersystems.signalservice.api.messages.multidevice;

import com.google.protobuf.ByteString;

import org.whispersystems.signalservice.internal.push.SignalServiceProtos;

import java.io.IOException;
import java.io.OutputStream;

public class DeviceGroupsOutputStream extends ChunkedOutputStream {

  public DeviceGroupsOutputStream(OutputStream out) {
    super(out);
  }

  public void write(DeviceGroup group) throws IOException {
    writeGroupDetails(group);
    writeAvatarImage(group);
  }

  public void close() throws IOException {
    out.close();
  }

  private void writeAvatarImage(DeviceGroup contact) throws IOException {
    if (contact.getAvatar().isPresent()) {
      writeStream(contact.getAvatar().get().getInputStream());
    }
  }

  private void writeGroupDetails(DeviceGroup group) throws IOException {
    SignalServiceProtos.GroupDetails.Builder groupDetails = SignalServiceProtos.GroupDetails.newBuilder();
    groupDetails.setId(ByteString.copyFrom(group.getId()));

    if (group.getName().isPresent()) {
      groupDetails.setName(group.getName().get());
    }

    if (group.getAvatar().isPresent()) {
      SignalServiceProtos.GroupDetails.Avatar.Builder avatarBuilder = SignalServiceProtos.GroupDetails.Avatar.newBuilder();
      avatarBuilder.setContentType(group.getAvatar().get().getContentType());
      avatarBuilder.setLength((int)group.getAvatar().get().getLength());
      groupDetails.setAvatar(avatarBuilder);
    }

    groupDetails.addAllMembers(group.getMembers());
    groupDetails.setActive(group.isActive());

    byte[] serializedContactDetails = groupDetails.build().toByteArray();

    writeVarint32(serializedContactDetails.length);
    out.write(serializedContactDetails);
  }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy