package com.atlassian.bitbucket.scm.git.protocol;

import javax.annotation.Nonnull;

/**
 * {@link StreamAnalyzer} implementation for git packet streams. The implementation forwards all provided bytes to a
 * {@link GitPacketStream}. Implement your analyzing logic in a git packet listener and
 * {@link #addListener(GitPacketListener) register} it to receive git packets for analysis.
 */
public abstract class GitPacketStreamAnalyzer implements StreamAnalyzer {

    private final GitPacketStream stream;

    public GitPacketStreamAnalyzer() {
        this.stream = new GitPacketStream();
    }

    public void process(@Nonnull byte[] b) {
        process(b, 0, b.length);
    }

    public void process(@Nonnull byte[] b, int offset, int len) {
        if (!isDone()) {
            stream.append(b, offset, len);
        }
    }

    public void addListener(@Nonnull GitPacketListener listener) {
        stream.addListener(listener);
    }
}
