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

com.firefly.net.tcp.codec.common.decode.DelimiterParser Maven / Gradle / Ivy

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

import com.firefly.net.tcp.codec.AbstractMessageHandler;

public class DelimiterParser extends AbstractMessageHandler {

    private StringBuilder buffer = new StringBuilder();
    private String delimiter;

    public DelimiterParser(String delimiter) {
        this.delimiter = delimiter;
    }

    @Override
    public void parse(String s) {
        try {
            buffer.append(s);
            int cursor = 0;
            int start;
            while ((start = buffer.indexOf(delimiter, cursor)) != -1) {
                action.call(buffer.substring(cursor, start));
                cursor = start + delimiter.length();
            }
            if (cursor < s.length()) {
                String remain = s.substring(cursor, s.length());
                buffer.delete(0, buffer.length());
                buffer.append(remain);
            } else {
                buffer.delete(0, buffer.length());
            }
        } catch (Throwable t) {
            exception.call(t);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy