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

pcap.codec.AbstractPacket Maven / Gradle / Ivy

There is a newer version: 1.5.1
Show newest version
/** This code is licenced under the GPL version 2. */
package pcap.codec;

import java.io.Serializable;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import pcap.common.annotation.Inclubating;
import pcap.common.memory.Memory;
import pcap.common.memory.MemoryAllocator;

/** @author Ardika Rommy Sanjaya */
@Inclubating
public abstract class AbstractPacket implements Packet {

  protected static final String ILLEGAL_HEADER_EXCEPTION = "Missing required header field(s).";

  protected Memory payloadBuffer;

  /**
   * Returns the {@link Memory} object representing this packet's payload.
   *
   * @return returns empty buffer if a payload doesn't exits, {@link Memory} object otherwise.
   */
  public Memory payloadBuffer() {
    if (payloadBuffer == null) {
      payloadBuffer = Properties.BYTE_BUF_ALLOCATOR.allocate(0);
    }
    return payloadBuffer;
  }

  @Override
  public  boolean contains(Class clazz) {
    return !get(clazz).isEmpty();
  }

  @Override
  public  List get(Class clazz) {
    List packets = new ArrayList();
    Iterator iterator = this.iterator();
    while (iterator.hasNext()) {
      Packet packet = iterator.next();
      if (clazz.isInstance(packet)) {
        packets.add(packet);
      }
    }
    return (List) packets;
  }

  @Override
  public  T getFirst(Class clazz) {
    return this.iterator().hasNext() ? (T) this.iterator().next() : null;
  }

  @Override
  public  T getLast(Class clazz) {
    Iterator iterator = this.iterator();
    Packet packet = null;
    while (iterator.hasNext()) {
      packet = iterator.next();
    }
    return (T) packet;
  }

  @Override
  public PacketIterator iterator() {
    return new PacketIterator(this);
  }

  @Override
  public void forEach(Consumer action) throws NullPointerException {
    PacketIterator iterator = iterator();
    while (iterator.hasNext()) {
      try {
        action.accept(iterator.next());
      } catch (Exception e) {
        // do nothing
      }
    }
  }

  public abstract static class Header implements Packet.Header {

    protected static final MemoryAllocator ALLOCATOR = Properties.BYTE_BUF_ALLOCATOR;

    protected Memory buffer;

    /**
     * Get reminder of buffer.
     *
     * @param buffer buffer.
     * @param length lenght.
     * @return returns {@link Memory}.
     */
    protected Memory slice(Memory buffer, int length) {
      if (buffer == null) {
        return null;
      }
      if (buffer.readableBytes() <= length && buffer.readerIndex() - length > 0) {
        return buffer.slice(buffer.readerIndex() - length, length);
      } else {
        return buffer.slice();
      }
    }

    /**
     * Returns header as byte buffer.
     *
     * @return return byte buffer.
     */
    public Memory buffer() {
      if (buffer == null) {
        buffer = ALLOCATOR.allocate(0);
      }
      return buffer;
    }

    public abstract Builder builder();
  }

  public  T map(Function function) {
    return (T) this;
  }

  public  List collectList() {
    List list = new ArrayList<>();
    PacketIterator iterator = iterator();
    while (iterator.hasNext()) {
      list.add((T) iterator.next());
    }
    return list;
  }

  public  Set collectSet() {
    Set set = new HashSet<>();
    PacketIterator iterator = iterator();
    while (iterator.hasNext()) {
      set.add((T) iterator.next());
    }
    return set;
  }

  /** Packet builder. */
  public abstract static class Builder
      implements pcap.common.util.Builder, Serializable {

    public void reset() {
      reset(-1, -1);
    }

    public void reset(int offset, int length) {
      throw new UnsupportedOperationException("Not implemented yet.");
    }
  }

  /** Packet factory. */
  public abstract static class Factory implements pcap.common.util.Factory {}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy