namespace Jellyfin.Plugin.SmartPlaylist.Util { public interface IStream { int available(); T get(); int commit(); int rewind(); int rewind(int n); } public class Stream : IStream { private readonly IList _items; private int _cursor; private int _ephemeral_cursor; protected Stream(IList items) { _items = items; _cursor = 0; _ephemeral_cursor = 0; } public int available() { return _items.Count - _ephemeral_cursor; } public T get() { return _items[_ephemeral_cursor++]; } public int commit() { int diff = _ephemeral_cursor - _cursor; _cursor = _ephemeral_cursor; return diff; } public int rewind() { int diff = _ephemeral_cursor - _cursor; _ephemeral_cursor = _cursor; return diff; } public int rewind(int n) { int diff = _ephemeral_cursor - _cursor; if (diff < n) { n = diff; } _ephemeral_cursor -= n; return n; } } }