#include "PdfStream.h"

#include "PdfDictionary.h"
#include "PdfXRefTable.h"

PdfStream::PdfStream()
{
    mDictionary.AddStringItem("Length", "0");
}

void PdfStream::SetContent(const std::string& content)
{
    mContent = content;
    Update();
}

void PdfStream::Update()
{
    auto length = mContent.size();
    mDictionary.AddStringItem("Length", std::to_string(length));
}

std::string PdfStream::ToString(PdfXRefTable* xRefTable)
{
    std::string content = GetStringPrefix();

    content += mDictionary.ToString();

    content += "stream\n";
    content += mContent;
    content += "\nendstream\n";
    content += "endobj\n\n";

    xRefTable->AddRecord(content.size(), mGenerationNumber, mIsFree);
    return content;
}
