package com.atlassian.bitbucket.mesh.git.hook;

import com.google.protobuf.ByteString;

import javax.annotation.Nonnull;
import java.io.InputStream;

import static java.util.Objects.requireNonNull;

public class ByteStringHookStdin extends AbstractHookStdin {

    private volatile ByteString bytes;

    ByteStringHookStdin(@Nonnull ByteString bytes, long size) {
        super(size);

        this.bytes = requireNonNull(bytes, "bytes");
    }

    @Nonnull
    public static ByteStringHookStdin of(@Nonnull String data) {
        ByteString bytes = ByteString.copyFromUtf8(data);

        return new ByteStringHookStdin(bytes, bytes.size());
    }

    @Nonnull
    @Override
    InputStream doOpen() {
        return bytes.newInput();
    }

    @Override
    void doRelease() {
        bytes = null;
    }
}
