c# - How do I concatenate two System.Io.Stream instances into one? -
let's imagine want stream 3 files user in row, instead of him handing me stream
object push bytes down, have hand him stream
object he'll pull bytes from. i'd take 3 filestream
objects (or cleverer, ienumerable<stream>
) , return new concatenatedstream
object pull source streams on demand.
class concatenatedstream : stream { queue<stream> streams; public concatenatedstream(ienumerable<stream> streams) { this.streams = new queue<stream>(streams); } public override bool canread { { return true; } } public override int read(byte[] buffer, int offset, int count) { if (streams.count == 0) return 0; int bytesread = streams.peek().read(buffer, offset, count); if (bytesread == 0) { streams.dequeue().dispose(); bytesread += read(buffer, offset + bytesread, count - bytesread); } return bytesread; } public override bool canseek { { return false; } } public override bool canwrite { { return false; } } public override void flush() { throw new notimplementedexception(); } public override long length { { throw new notimplementedexception(); } } public override long position { { throw new notimplementedexception(); } set { 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 void write(byte[] buffer, int offset, int count) { throw new notimplementedexception(); } }
Comments
Post a Comment