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

org.snapscript.agent.event.ExitEventMarshaller Maven / Gradle / Ivy

package org.snapscript.agent.event;

import static org.snapscript.agent.event.ProcessEventType.EXIT;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import org.snapscript.agent.ProcessMode;

public class ExitEventMarshaller implements ProcessEventMarshaller {

   @Override
   public ExitEvent fromMessage(MessageEnvelope message) throws IOException {
      byte[] array = message.getData();
      int length = message.getLength();
      int offset = message.getOffset();
      ByteArrayInputStream buffer = new ByteArrayInputStream(array, offset, length);
      DataInputStream input = new DataInputStream(buffer);
      String process = input.readUTF();
      String type = input.readUTF();
      ProcessMode mode = ProcessMode.resolveMode(type);
      long duration = input.readLong();
      
      return new ExitEvent.Builder(process)
         .withDuration(duration)
         .withMode(mode)
         .build();
      
   }

   @Override
   public MessageEnvelope toMessage(ExitEvent value) throws IOException {
      ByteArrayOutputStream buffer = new ByteArrayOutputStream();
      DataOutputStream output = new DataOutputStream(buffer);
      ProcessMode mode = value.getMode();
      String process = value.getProcess();
      String type = mode.name();
      long duration = value.getDuration();
      
      output.writeUTF(process);
      output.writeUTF(type);
      output.writeLong(duration);
      output.flush();
      
      byte[] array = buffer.toByteArray();
      return new MessageEnvelope(EXIT.code, array, 0, array.length);
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy