namespace UglyToad.PdfPig.Tokens
{
using System;
using System.Collections.Generic;
using Filters;
using Util.JetBrains.Annotations;
///
/// A stream consists of a dictionary followed by zero or more bytes bracketed between the keywords stream and endstream.
/// The bytes may be compressed by application of zero or more filters which are run in the order specified in the .
///
public class StreamToken : IDataToken>
{
private readonly object lockObject = new object();
private IReadOnlyList decodedBytes;
///
/// The dictionary specifying the length of the stream, any applied compression filters and additional information.
///
[NotNull]
public DictionaryToken StreamDictionary { get; }
///
/// The compressed byte data of the stream.
///
[NotNull]
public IReadOnlyList Data { get; }
///
/// Create a new .
///
/// The stream dictionary.
/// The stream data.
public StreamToken([NotNull] DictionaryToken streamDictionary, [NotNull] IReadOnlyList data)
{
StreamDictionary = streamDictionary ?? throw new ArgumentNullException(nameof(streamDictionary));
Data = data ?? throw new ArgumentNullException(nameof(data));
}
internal IReadOnlyList Decode(IFilterProvider filterProvider)
{
lock (lockObject)
{
if (decodedBytes != null)
{
return decodedBytes;
}
var filters = filterProvider.GetFilters(StreamDictionary);
var transform = Data;
for (var i = 0; i < filters.Count; i++)
{
transform = filters[i].Decode(transform, StreamDictionary, i);
}
decodedBytes = transform;
return transform;
}
}
///
public override string ToString()
{
return $"Length: {Data.Count}, Dictionary: {StreamDictionary}";
}
}
}