Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将两个System.Io.Stream实例连接成一个?_C#_.net_Stream - Fatal编程技术网

C# 如何将两个System.Io.Stream实例连接成一个?

C# 如何将两个System.Io.Stream实例连接成一个?,c#,.net,stream,C#,.net,Stream,让我们想象一下,我想将三个文件流式传输给一个用户,所有文件都排成一行,但是他没有给我一个流对象来向下推字节,我必须给他一个流对象,他将从中提取字节。我想使用我的三个FileStream对象(或者更聪明的一个IEnumerable)并返回一个新的连接流对象,该对象将根据需要从源流中提取。只要您只需要阅读,下面是我对此类流的实现: 注意!位置和搜索已损坏,需要修复它 使用系统; 使用System.Collections.Generic; 使用系统诊断; 使用System.IO; 名称空间LVK.IO

让我们想象一下,我想将三个文件流式传输给一个用户,所有文件都排成一行,但是他没有给我一个
对象来向下推字节,我必须给他一个
对象,他将从中提取字节。我想使用我的三个
FileStream
对象(或者更聪明的一个
IEnumerable
)并返回一个新的
连接流
对象,该对象将根据需要从源流中提取。

只要您只需要阅读,下面是我对此类流的实现:

注意!位置和搜索已损坏,需要修复它

使用系统;
使用System.Collections.Generic;
使用系统诊断;
使用System.IO;
名称空间LVK.IO
{
/// 
///此类是管理多个基础类的子类
///被认为链接到一个大流中的流。仅用于读取
///如果允许查找,则写入将抛出异常。
/// 
公共类CombinedStream:Stream
{
私有只读流[]_underyingstreams;
私有只读Int64[]\u基础起始位置;
私人Int64_职位;
私有只读Int64_总长度;
私有整数指数;
/// 
///在指定数组的顶部构造新数组
///溪流。
/// 
/// 
///将链接在一起并
///被认为是一条大河。
/// 
公共组合流(params Stream[]underyingstreams)
{
if(underlyngstreams==null)
抛出新的ArgumentNullException(“underlyingStreams”);
foreach(下伏溪流中的溪流)
{
if(流==null)
抛出新ArgumentNullException(“underlyingStreams[]”);
如果(!stream.CanRead)
抛出新的InvalidOperationException(“CanRead not true for all streams”);
if(!stream.CanSeek)
抛出新的InvalidOperationException(“CanSeek not true for all streams”);
}
_underyingstreams=新流[underyingstreams.Length];
_underyingstartingpositions=new Int64[underyingstreams.Length];
数组.Copy(underyingstreams,_underyingstreams,underyingstreams.Length);
_位置=0;
_指数=0;
_基线起始位置[0]=0;
对于(int index=1;index<\u underyingstartingpositions.Length;index++)
{
_基础起始位置[索引]=
_基础起始位置[索引-1]+
_UnderlyingStreams[索引-1]。长度;
}
_总长度=
_underyingstartingpositions[\u underyingstartingpositions.Length-1]+
_UnderlyngStreams[_underlyngStreams.Length-1]。长度;
}
/// 
///获取一个值,该值指示当前流是否支持读取。
/// 
/// 
///对。
/// 
/// 
///对我来说永远是真的。
/// 
公共覆盖布尔值可读取
{
得到
{
返回true;
}
}
/// 
///获取一个值,该值指示当前流是否支持查找。
/// 
/// 
///对。
/// 
/// 
///对我来说永远是真的。
/// 
公共覆盖布尔搜索
{
得到
{
返回true;
}
}
/// 
///获取一个值,该值指示当前流是否支持写入。
/// 
/// 
///错。
/// 
/// 
///对我来说总是错误的。
/// 
公共重写布尔可写
{
得到
{
返回false;
}
}
/// 
///在派生类中重写时,清除此流的所有缓冲区,并将所有缓冲数据写入基础设备。
/// 
///发生I/O错误。
公共覆盖无效刷新()
{
foreach(溪流中的溪流)
{
stream.Flush();
}
}
/// 
///获取基础流的总长度(以字节为单位)。
/// 
/// 
///基础流的总长度。
/// 
/// 
///一个长值,表示基础流的总长度(字节)。
/// 
///从流派生的类不支持查找。
///方法在流关闭后被调用。
公共覆盖Int64长度
{
得到
{
返回总长度;
}
}
/// 
///获取或设置当前流中的位置。
/// 
/// 
///流中的当前位置。
///发生I/O错误。
///流不支持查找。
///方法在流关闭后被调用。
公共覆盖Int64位置
{
得到
{
返回位置;
}
设置
{
如果(值<0 | |值>_总长度)
抛出新ArgumentOutOfRangeException(“位置”);
_位置=值;
如果(值==_总长度)
{
_索引=_underyingstreams.Length-1;
_位置=_underyingstreams[_Index]。长度;
}
其他的
{
而(_Index>0&&u Position<_underyingstartingpositions[_Index])
{
_索引--;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;

namespace LVK.IO
{
    /// <summary>
    /// This class is a <see cref="Stream"/> descendant that manages multiple underlying
    /// streams which are considered to be chained together to one large stream. Only reading
    /// and seeking is allowed, writing will throw exceptions.
    /// </summary>
    public class CombinedStream : Stream
    {
        private readonly Stream[] _UnderlyingStreams;
        private readonly Int64[] _UnderlyingStartingPositions;
        private Int64 _Position;
        private readonly Int64 _TotalLength;
        private int _Index;

        /// <summary>
        /// Constructs a new <see cref="CombinedStream"/> on top of the specified array
        /// of streams.
        /// </summary>
        /// <param name="underlyingStreams">
        /// An array of <see cref="Stream"/> objects that will be chained together and
        /// considered to be one big stream.
        /// </param>
        public CombinedStream(params Stream[] underlyingStreams)
        {
            if (underlyingStreams == null)
                throw new ArgumentNullException("underlyingStreams");
            foreach (Stream stream in underlyingStreams)
            {
                if (stream == null)
                    throw new ArgumentNullException("underlyingStreams[]");
                if (!stream.CanRead)
                    throw new InvalidOperationException("CanRead not true for all streams");
                if (!stream.CanSeek)
                    throw new InvalidOperationException("CanSeek not true for all streams");
            }

            _UnderlyingStreams = new Stream[underlyingStreams.Length];
            _UnderlyingStartingPositions = new Int64[underlyingStreams.Length];
            Array.Copy(underlyingStreams, _UnderlyingStreams, underlyingStreams.Length);

            _Position = 0;
            _Index = 0;

            _UnderlyingStartingPositions[0] = 0;
            for (int index = 1; index < _UnderlyingStartingPositions.Length; index++)
            {
                _UnderlyingStartingPositions[index] =
                    _UnderlyingStartingPositions[index - 1] +
                    _UnderlyingStreams[index - 1].Length;
            }

            _TotalLength =
                _UnderlyingStartingPositions[_UnderlyingStartingPositions.Length - 1] +
                _UnderlyingStreams[_UnderlyingStreams.Length - 1].Length;
        }

        /// <summary>
        /// Gets a value indicating whether the current stream supports reading.
        /// </summary>
        /// <value>
        /// <c>true</c>.
        /// </value>
        /// <returns>
        /// Always <c>true</c> for <see cref="CombinedStream"/>.
        /// </returns>
        public override Boolean CanRead
        {
            get
            {
                return true;
            }
        }

        /// <summary>
        /// Gets a value indicating whether the current stream supports seeking.
        /// </summary>
        /// <value>
        /// <c>true</c>.
        /// </value>
        /// <returns>
        /// Always <c>true</c> for <see cref="CombinedStream"/>.
        /// </returns>
        public override Boolean CanSeek
        {
            get
            {
                return true;
            }
        }

        /// <summary>
        /// Gets a value indicating whether the current stream supports writing.
        /// </summary>
        /// <value>
        /// <c>false</c>.
        /// </value>
        /// <returns>
        /// Always <c>false</c> for <see cref="CombinedStream"/>.
        /// </returns>
        public override Boolean CanWrite
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// When overridden in a derived class, clears all buffers for this stream and causes any buffered data to be written to the underlying device.
        /// </summary>
        /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
        public override void Flush()
        {
            foreach (Stream stream in _UnderlyingStreams)
            {
                stream.Flush();
            }
        }

        /// <summary>
        /// Gets the total length in bytes of the underlying streams.
        /// </summary>
        /// <value>
        /// The total length of the underlying streams.
        /// </value>
        /// <returns>
        /// A long value representing the total length of the underlying streams in bytes.
        /// </returns>
        /// <exception cref="T:System.NotSupportedException">A class derived from Stream does not support seeking. </exception>
        /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception>
        public override Int64 Length
        {
            get
            {
                return _TotalLength;
            }
        }

        /// <summary>
        /// Gets or sets the position within the current stream.
        /// </summary>
        /// <value></value>
        /// <returns>The current position within the stream.</returns>
        /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
        /// <exception cref="T:System.NotSupportedException">The stream does not support seeking. </exception>
        /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception>
        public override Int64 Position
        {
            get
            {
                return _Position;
            }

            set
            {
                if (value < 0 || value > _TotalLength)
                    throw new ArgumentOutOfRangeException("Position");

                _Position = value;
                if (value == _TotalLength)
                {
                    _Index = _UnderlyingStreams.Length - 1;
                    _Position = _UnderlyingStreams[_Index].Length;
                }

                else
                {
                    while (_Index > 0 && _Position < _UnderlyingStartingPositions[_Index])
                    {
                        _Index--;
                    }

                    while (_Index < _UnderlyingStreams.Length - 1 &&
                           _Position >= _UnderlyingStartingPositions[_Index] + _UnderlyingStreams[_Index].Length)
                    {
                        _Index++;
                    }
                }
            }
        }

        /// <summary>
        /// Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
        /// </summary>
        /// <param name="buffer">An array of bytes. When this method returns, the buffer contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source.</param>
        /// <param name="offset">The zero-based byte offset in buffer at which to begin storing the data read from the current stream.</param>
        /// <param name="count">The maximum number of bytes to be read from the current stream.</param>
        /// <returns>
        /// The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.
        /// </returns>
        /// <exception cref="T:System.ArgumentException">The sum of offset and count is larger than the buffer length. </exception>
        /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception>
        /// <exception cref="T:System.NotSupportedException">The stream does not support reading. </exception>
        /// <exception cref="T:System.ArgumentNullException">buffer is null. </exception>
        /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
        /// <exception cref="T:System.ArgumentOutOfRangeException">offset or count is negative. </exception>
        public override int Read(Byte[] buffer, int offset, int count)
        {
            int result = 0;
            while (count > 0)
            {
                _UnderlyingStreams[_Index].Position = _Position - _UnderlyingStartingPositions[_Index];
                int bytesRead = _UnderlyingStreams[_Index].Read(buffer, offset, count);
                result += bytesRead;
                offset += bytesRead;
                count -= bytesRead;
                _Position += bytesRead;

                if (count > 0)
                {
                    if (_Index < _UnderlyingStreams.Length - 1)
                        _Index++;
                    else
                        break;
                }
            }

            return result;
        }

        /// <summary>
        /// Sets the position within the current stream.
        /// </summary>
        /// <param name="offset">A byte offset relative to the origin parameter.</param>
        /// <param name="origin">A value of type <see cref="T:System.IO.SeekOrigin"></see> indicating the reference point used to obtain the new position.</param>
        /// <returns>
        /// The new position within the current stream.
        /// </returns>
        /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
        /// <exception cref="T:System.NotSupportedException">The stream does not support seeking, such as if the stream is constructed from a pipe or console output. </exception>
        /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception>
        public override long Seek(long offset, SeekOrigin origin)
        {
            switch (origin)
            {
                case SeekOrigin.Begin:
                    Position = offset;
                    break;

                case SeekOrigin.Current:
                    Position += offset;
                    break;

                case SeekOrigin.End:
                    Position = Length + offset;
                    break;
            }

            return Position;
        }

        /// <summary>
        /// Throws <see cref="NotSupportedException"/> since the <see cref="CombinedStream"/>
        /// class does not supports changing the length.
        /// </summary>
        /// <param name="value">The desired length of the current stream in bytes.</param>
        /// <exception cref="T:System.NotSupportedException">
        /// <see cref="CombinedStream"/> does not support this operation.
        /// </exception>
        public override void SetLength(long value)
        {
            throw new NotSupportedException("The method or operation is not supported by CombinedStream.");
        }

        /// <summary>
        /// Throws <see cref="NotSupportedException"/> since the <see cref="CombinedStream"/>
        /// class does not supports writing to the underlying streams.
        /// </summary>
        /// <param name="buffer">An array of bytes.  This method copies count bytes from buffer to the current stream.</param>
        /// <param name="offset">The zero-based byte offset in buffer at which to begin copying bytes to the current stream.</param>
        /// <param name="count">The number of bytes to be written to the current stream.</param>
        /// <exception cref="T:System.NotSupportedException">
        /// <see cref="CombinedStream"/> does not support this operation.
        /// </exception>
        public override void Write(byte[] buffer, int offset, int count)
        {
            throw new NotSupportedException("The method or operation is not supported by CombinedStream.");
        }
    }
}
class ConcatenatedStream : Stream
{
    Queue<Stream> streams;

    public ConcatenatedStream(IEnumerable<Stream> streams)
    {
        this.streams = new Queue<Stream>(streams);
    }

    public override bool CanRead
    {
        get { return true; }
    }

    public override int Read(byte[] buffer, int offset, int count)
    {
        int totalBytesRead = 0;

        while (count > 0 && streams.Count > 0)
        {
            int bytesRead = streams.Peek().Read(buffer, offset, count);
            if (bytesRead == 0)
            {
                streams.Dequeue().Dispose();
                continue;
            }

            totalBytesRead += bytesRead;
            offset += bytesRead;
            count -= bytesRead;
        }

        return totalBytesRead;
    }

    public override bool CanSeek
    {
        get { return false; }
    }

    public override bool CanWrite
    {
        get { return false; }
    }

    public override void Flush()
    {
        throw new NotImplementedException();
    }

    public override long Length
    {
        get { throw new NotImplementedException(); }
    }

    public override long Position
    {
        get
        {
            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();
    }
}
class StreamEnumerator : Stream
{
    private long position;
    bool closeStreams;
    IEnumerator<Stream> iterator;
    Stream current;
    private void EndOfStream() {
        if (closeStreams && current != null)
        {
            current.Close();
            current.Dispose();
        }
        current = null;
    }
    private Stream Current
    {
        get {
            if(current != null) return current;
            if (iterator == null) throw new ObjectDisposedException(GetType().Name);
            if (iterator.MoveNext()) {
                current = iterator.Current;
            }
            return current;
        }
    }
    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            EndOfStream();
            iterator.Dispose();
            iterator = null;
            current = null;
        }
        base.Dispose(disposing);
    }
    public StreamEnumerator(IEnumerable<Stream> source, bool closeStreams)
    {
        if (source == null) throw new ArgumentNullException("source");
        iterator = source.GetEnumerator();
        this.closeStreams = closeStreams;
    }
    public override bool CanRead { get { return true; } }
    public override bool CanWrite { get { return false; } }
    public override void Write(byte[] buffer, int offset, int count)
    {
        throw new NotSupportedException();
    }
    public override void WriteByte(byte value)
    {
        throw new NotSupportedException();
    }
    public override bool CanSeek { get { return false; } }
    public override bool CanTimeout { get { return false; } }
    public override void SetLength(long value)
    {
        throw new NotSupportedException();
    }
    public override long Seek(long offset, SeekOrigin origin)
    {
        throw new NotSupportedException();
    }
    public override void Flush()
    { /* nothing to do */ }
    public override long Length
    {
        get { throw new NotSupportedException(); }
    }
    public override long Position
    {
        get { return position; }
        set { if (value != this.position) throw new NotSupportedException(); }
    }
    public override int Read(byte[] buffer, int offset, int count)
    {
        int result = 0;
        while (count > 0)
        {
            Stream stream = Current;
            if (stream == null) break;
            int thisCount = stream.Read(buffer, offset, count);
            result += thisCount;
            count -= thisCount;
            offset += thisCount;
            if (thisCount == 0) EndOfStream();
        }
        position += result;
        return result;
    }
}
public sealed class ConcatenatedStream : Stream
{
    List<Stream> streams;
    private long _Position { get; set; }

    public ConcatenatedStream(List<Stream> streams)
    {
        this.streams = streams;
        Seek(0, SeekOrigin.Begin);
    }

    public override bool CanRead
    {
        get { return true; }
    }

    public override int Read(byte[] buffer, int offset, int count)
    {
        if (streams.Count == 0)
            return 0;

        var startStream = 0;
        var cumulativeCapacity = 0L;
        for (var i = 0; i < streams.Count; i++)
        {
            cumulativeCapacity += streams[i].Length;
            if (_Position < cumulativeCapacity)
            {
                startStream = i;
                break;
            }
        }

        var bytesRead = 0;
        var curStream = startStream;

        while (_Position < Length && bytesRead < count && curStream < streams.Count)
        {
            var r = streams[curStream].Read(buffer, offset + bytesRead, count - bytesRead);
            bytesRead += r;
            Seek(_Position + r, SeekOrigin.Begin);
            curStream++;
        }

        return bytesRead;
    }

    public override bool CanSeek
    {
        get { return true; }
    }

    public override bool CanWrite
    {
        get { return false; }
    }

    public override void Flush()
    {
        throw new NotImplementedException();
    }

    public override long Length
    {
        get {
            long length = 0;
            for (var i = 0; i < streams.Count; i++)
            {
                length += streams[i].Length;
            }
            return length;
        }
    }

    public override long Position
    {
        get
        {
            return _Position;
        }
        set
        {
            Seek(value, SeekOrigin.Begin);
        }
    }

    public override long Seek(long offset, SeekOrigin origin)
    {
        if (origin == SeekOrigin.Begin)
        {
            _Position = offset;

            var prevLength = 0L;
            var cumulativeLength = 0L;
            for (var i = 0; i < streams.Count; i++)
            {
                cumulativeLength += streams[i].Length;
                if (offset < cumulativeLength)
                {
                    streams[i].Seek(offset - prevLength, SeekOrigin.Begin);
                    return _Position;
                }
                prevLength = cumulativeLength;
            }
        }

        if (origin == SeekOrigin.Current)
        {
            var newAbs = _Position + offset;
            return Seek(newAbs, SeekOrigin.Begin);
        } 
        else if(origin == SeekOrigin.End)
        {
            var newAbs = Length - offset;
            return Seek(newAbs, SeekOrigin.Begin);
        }

        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();
    }
}