#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;
}
