Skip to content

Instantly share code, notes, and snippets.

@tenbits
Created May 2, 2018 15:30
Show Gist options
  • Save tenbits/ac5441e84b7983e0e17f39be172e87f9 to your computer and use it in GitHub Desktop.
Save tenbits/ac5441e84b7983e0e17f39be172e87f9 to your computer and use it in GitHub Desktop.
public class MultiStream : Stream
{
private readonly List<Stream> _streams;
public MultiStream(params Stream[] streams)
{
_streams = streams.ToList();
}
public override void Flush()
{
foreach (var stream in _streams)
{
stream.Flush();
}
}
public override long Seek(long offset, SeekOrigin origin)
{
long x = 0;
foreach (var stream in _streams)
{
x = stream.Seek(offset, origin);
}
return x;
}
public override void SetLength(long value)
{
foreach (var stream in _streams)
{
stream.SetLength(value);
}
}
public override int Read(byte[] buffer, int offset, int count)
{
return _streams.First(x => x.CanRead).Read(buffer, offset, count);
}
public override void Write(byte[] buffer, int offset, int count)
{
foreach (var stream in _streams)
{
stream.Write(buffer, offset, count);
}
}
public override bool CanRead
{
get { return _streams.Any(x => x.CanRead); }
}
public override bool CanSeek { get { return _streams.TrueForAll(x => x.CanSeek); } }
public override bool CanWrite { get { return _streams.TrueForAll(x => x.CanWrite); } }
public override long Length => _streams.Min(x => x.Length);
public override long Position
{
get => _streams.First().Position;
set
{
_streams.ForEach(x => x.Position = value);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment