namespace Jellyfin.Plugin.SmartPlaylist.Util { public interface IStream { int Available(); T Get(); int Commit(); int Rewind(); int Rewind(int n); int Consumed(); IStream Copy(); } public class Stream : IStream { private readonly IList _items; private int _cursor; private int _ephemeralCursor; protected Stream(IList items) { _items = items; _cursor = 0; _ephemeralCursor = 0; } private Stream(IList items, int cursor, int ephemeralCursor) { _items = items; _cursor = cursor; _ephemeralCursor = ephemeralCursor; } public int Available() { return _items.Count - _ephemeralCursor; } public T Get() { return _items[_ephemeralCursor++]; } public int Commit() { int diff = Consumed(); _cursor = _ephemeralCursor; return diff; } public int Rewind() { int diff = Consumed(); _ephemeralCursor = _cursor; return diff; } public int Rewind(int n) { int diff = Consumed(); if (diff < n) { n = diff; } _ephemeralCursor -= n; return n; } public int Consumed() { return _ephemeralCursor - _cursor; } public IStream Copy() { return new Stream(_items, _cursor, _ephemeralCursor); } } }