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

io.pkts.packet.sip.header.impl.SipHeaderImpl Maven / Gradle / Ivy

There is a newer version: 3.0.10
Show newest version
/**
 * 
 */
package io.pkts.packet.sip.header.impl;

import io.pkts.buffer.Buffer;
import io.pkts.packet.sip.SipParseException;
import io.pkts.packet.sip.header.SipHeader;
import io.pkts.packet.sip.impl.SipParser;

import java.util.function.Function;


/**
 * @author [email protected]
 */
public class SipHeaderImpl implements SipHeader {

    private final Buffer name;

    private final Buffer value;

    /**
     * 
     */
    public SipHeaderImpl(final Buffer name, final Buffer value) {
        this.name = name;
        this.value = value;
    }

    @Override
    public SipHeader.Builder copy() {
        return new SipHeaderBuilder(name, value);
    }

    /**
     * Subclasses may override this one and are in fact encourage to do so
     * 
     * {@inheritDoc}
     */
    @Override
    public Buffer getName() {
        return this.name;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Buffer getValue() {
        return this.value.slice();
    }

    @Override
    public String toString() {
        return getName().toString() + ": " + getValue();
    }

    @Override
    public void verify() throws SipParseException {
        // by default, everything is assumed to be correct.
        // Subclasses should override this method and
        // check that everything is ok...
    }

    /**
     * If this method actually gets called it means that we are the {@link SipHeaderImpl} itself
     * and that we need to frame it further. Subclasses MUST override this method and simply return
     * 'this'.
     */
    @Override
    public SipHeader ensure() {
        final Function framer = SipParser.getFramer(this.name);
        if (framer != null) {
            return framer.apply(this);
        }
        return this;
    }

    @Override
    public void getBytes(final Buffer dst) {
        this.name.getBytes(0, dst);
        dst.write(SipParser.COLON);
        dst.write(SipParser.SP);
        transferValue(dst);
    }

    /**
     * Transfer the bytes of the value into the destination. Sub-classes should
     * override this method.
     * 
     * @param dst
     */
    protected void transferValue(final Buffer dst) {
        final Buffer value = getValue();
        value.getBytes(0, dst);
    }

    @Override
    public SipHeader clone() {
        // TODO: this will be easier once everything is immutable since you just have to clone the
        // value buffer and that is it. No need to transfer the value etc.
        // final Buffer buffer = Buffers.createBuffer(1024);
        // transferValue(buffer);
        return new SipHeaderImpl(this.name.clone(), value.clone());
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy