package com.atlassian.stash.internal.plugin.readme;

import com.atlassian.bitbucket.content.ContentService;
import com.atlassian.bitbucket.repository.Repository;
import com.atlassian.bitbucket.util.MoreSuppliers;

import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;

public class ContentUtil {

    /**
     * Gets the content of a given file/revision as a String.
     */
    public static String getFileContentAsString(ContentService contentService, final Repository repository, final String objectId, final String path, int maxSize) {
        final ByteArrayOutputStream out = new MaxSizeByteArrayOutputStream(512, maxSize);
        contentService.streamFile(repository, objectId, path, MoreSuppliers.newTypeAwareOutputSupplierOf(out));
        return new String(out.toByteArray(), StandardCharsets.UTF_8);
    }
}
