Skip to content

Instantly share code, notes, and snippets.

@JKamsker
Last active August 25, 2017 17:10
Show Gist options
  • Save JKamsker/a488b60d8ff5b0b22033589dec2be074 to your computer and use it in GitHub Desktop.
Save JKamsker/a488b60d8ff5b0b22033589dec2be074 to your computer and use it in GitHub Desktop.
public class FifoStream : Stream
{
private readonly Queue<byte[]> _localBuffer = new Queue<byte[]>();
private int _currentStreamOffset = 0;
private long _length;
public override void Flush()
{
throw new NotImplementedException();
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new NotImplementedException();
}
public override void SetLength(long value)
{
throw new NotImplementedException();
}
public override int Read(byte[] buffer, int offset, int count)
{
var currentReadBytes = 0;
while (currentReadBytes < count && _localBuffer.Any())
{
var enqueuedByteArray = _localBuffer.Peek();
var amo = enqueuedByteArray.Length - _currentStreamOffset;
if (amo > count)
{
amo = count;
}
Array.Copy(enqueuedByteArray, _currentStreamOffset, buffer, currentReadBytes, amo);
currentReadBytes += amo;
_currentStreamOffset += amo;
if (_currentStreamOffset >= enqueuedByteArray.Length)
{
_localBuffer.Dequeue();
_length -= count;
_currentStreamOffset = 0;
}
}
return currentReadBytes;
}
public override void Write(byte[] buffer, int offset, int count)
{
var currentWriteBytes = new byte[count];
Array.Copy(buffer, offset, currentWriteBytes, 0, count);
_localBuffer.Enqueue(currentWriteBytes);
_length += count;
}
public override bool CanRead { get; }
public override bool CanSeek { get; }
public override bool CanWrite { get; }
public override long Length => _length;
// _locBuf.Sum(m=>m.Length);
public override long Position { get; set; }
}
///usage
private static void Main(string[] args)
{
using (var ms = new FifoStream())
using (var fs = File.OpenRead(@"G:\FileArchive\Filme\BigMovie.mkv"))
using (var fd = File.Create(@"F:\bd\BigMovie-cop.mkv"))
{
int fsr = 0;
int msr = 0;
var buf = new byte[10 * 1024 * 1024];
var buf2 = new byte[10 * 1024 * 1024];
while ((fsr = fs.Read(buf, 0, buf.Length)) != 0)
{
ms.Write(buf, 0, fsr);
while ((msr = ms.Read(buf2, 0, buf2.Length)) != 0)
{
fd.Write(buf2, 0, msr);
}
}
Console.Read();
}
Console.Read();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment