51 lines
1.2 KiB
C#
51 lines
1.2 KiB
C#
using System;
|
|
using System.Text;
|
|
namespace ln.templates.streams
|
|
{
|
|
public class PeekAbleStream<T>
|
|
{
|
|
private T[] buffer;
|
|
private int position;
|
|
|
|
public int Position => position;
|
|
public bool EndOfBuffer => (position >= buffer.Length);
|
|
|
|
public int MarkedPosition { get; set; }
|
|
|
|
public PeekAbleStream(T[] buffer)
|
|
{
|
|
this.buffer = buffer;
|
|
}
|
|
|
|
public T Read(){
|
|
if (position < buffer.Length)
|
|
return buffer[position++];
|
|
|
|
throw new IndexOutOfRangeException("Tried to read after end of buffer");
|
|
}
|
|
|
|
public T Peek(int shift = 0)
|
|
{
|
|
if ((position + shift) < buffer.Length)
|
|
return buffer[position + shift];
|
|
|
|
throw new IndexOutOfRangeException("Tried to peek after end of buffer");
|
|
}
|
|
|
|
public void Mark()
|
|
{
|
|
MarkedPosition = position;
|
|
}
|
|
|
|
public T[] GetMarkedIntervall()
|
|
{
|
|
T[] intervall = new T[position - MarkedPosition];
|
|
Array.Copy(buffer, MarkedPosition, intervall, 0, intervall.Length);
|
|
return intervall;
|
|
}
|
|
|
|
public int Remaining => buffer.Length - position;
|
|
|
|
}
|
|
}
|