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

com.firefly.net.tcp.codec.AbstractByteBufferMessageHandler Maven / Gradle / Ivy

There is a newer version: 5.0.0-dev6
Show newest version
package com.firefly.net.tcp.codec;

import java.nio.ByteBuffer;

/**
 * @author Pengtao Qiu
 */
abstract public class AbstractByteBufferMessageHandler extends AbstractMessageHandler {

    protected ByteBuffer buffer;

    public ByteBuffer getBuffer() {
        return buffer;
    }

    @Override
    public void parse(ByteBuffer buf) {
        try {
            merge(buf);
            parse();
        } catch (Throwable t) {
            exception.call(t);
        }
    }

    protected void merge(ByteBuffer buf) {
        if (buffer != null) {
            if (buffer.hasRemaining()) {
                ByteBuffer tmp = ByteBuffer.allocate(buffer.remaining() + buf.remaining());
                tmp.put(buffer).put(buf).flip();
                buffer = tmp;
            } else {
                buffer = buf;
            }
        } else {
            buffer = buf;
        }
    }

    abstract protected void parse();
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy