Serializes the contents into the content stream.
*/
public void flush(
)
{
PdfStream stream;
PdfDataObject baseDataObject = getBaseDataObject();
// Are contents just a single stream object?
if(baseDataObject instanceof PdfStream) // Single stream.
{stream = (PdfStream)baseDataObject;}
else // Array of streams.
{
PdfArray streams = (PdfArray)baseDataObject;
// No stream available?
if(streams.size() == 0) // No stream.
{
// Add first stream!
stream = new PdfStream();
streams.add( // Inserts the new stream into the content stream.
getFile().register(stream) // Inserts the new stream into the file.
);
}
else // Streams exist.
{
// Eliminating exceeding streams...
/*
NOTE: Applications that consume or produce PDF files are not required to preserve
the existing structure of the Contents array [PDF:1.6:3.6.2].
*/
while(streams.size() > 1)
{
getFile().unregister( // Removes the exceeding stream from the file.
(PdfReference)streams.remove(1) // Removes the exceeding stream from the content stream.
);
}
PdfReference streamReference = (PdfReference)streams.get(0);
File.update(streamReference); // Updates the existing stream into the file.
stream = (PdfStream)streamReference.getDataObject();
}
}
// Get the stream buffer!
IBuffer buffer = stream.getBody();
// Delete old contents from the stream buffer!
buffer.setLength(0);
// Serializing the new contents into the stream buffer...
for(ContentObject item : items)
{item.writeTo(buffer);}