@@ -21,43 +21,48 @@ internal static void ThrowIfEmpty<T>(in Memory<T> buffer, [CallerArgumentExpress
2121 throw new ArgumentException ( ExceptionMessages . BufferTooSmall , expression ) ;
2222 }
2323
24+ private static Stream Combine ( Stream stream , ReadOnlySpan < Stream > others , bool leaveOpen )
25+ => others switch
26+ {
27+ [ ] => stream ,
28+ [ var s ] => new SparseStream < ( Stream , Stream ) > ( ( stream , s ) , leaveOpen ) ,
29+ [ var s1 , var s2 ] => new SparseStream < ( Stream , Stream , Stream ) > ( ( stream , s1 , s2 ) , leaveOpen ) ,
30+ [ var s1 , var s2 , var s3 ] => new SparseStream < ( Stream , Stream , Stream , Stream ) > ( ( stream , s1 , s2 , s3 ) , leaveOpen ) ,
31+ [ var s1 , var s2 , var s3 , var s4 ] => new SparseStream < ( Stream , Stream , Stream , Stream , Stream ) > ( ( stream , s1 , s2 , s3 , s4 ) , leaveOpen ) ,
32+ [ var s1 , var s2 , var s3 , var s4 , var s5 ] => new SparseStream < ( Stream , Stream , Stream , Stream , Stream , Stream ) > ( ( stream , s1 , s2 , s3 , s4 ,
33+ s5 ) , leaveOpen ) ,
34+ _ => new UnboundedSparseStream ( others , leaveOpen ) ,
35+ } ;
36+
2437 /// <summary>
2538 /// Combines multiple readable streams.
2639 /// </summary>
2740 /// <param name="stream">The stream to combine.</param>
2841 /// <param name="others">A collection of streams.</param>
2942 /// <returns>An object that represents multiple streams as one logical stream.</returns>
3043 public static Stream Combine ( this Stream stream , ReadOnlySpan < Stream > others ) // TODO: Use params in future
31- => others switch
32- {
33- [ ] => stream ,
34- [ var s ] => new SparseStream < ( Stream , Stream ) > ( ( stream , s ) ) ,
35- [ var s1 , var s2 ] => new SparseStream < ( Stream , Stream , Stream ) > ( ( stream , s1 , s2 ) ) ,
36- [ var s1 , var s2 , var s3 ] => new SparseStream < ( Stream , Stream , Stream , Stream ) > ( ( stream , s1 , s2 , s3 ) ) ,
37- [ var s1 , var s2 , var s3 , var s4 ] => new SparseStream < ( Stream , Stream , Stream , Stream , Stream ) > ( ( stream , s1 , s2 , s3 , s4 ) ) ,
38- [ var s1 , var s2 , var s3 , var s4 , var s5 ] => new SparseStream < ( Stream , Stream , Stream , Stream , Stream , Stream ) > ( ( stream , s1 , s2 , s3 , s4 ,
39- s5 ) ) ,
40- _ => new UnboundedSparseStream ( others . ToArray ( ) ) ,
41- } ;
44+ => Combine ( stream , others , leaveOpen : true ) ;
4245
4346 /// <summary>
4447 /// Combines multiple readable streams.
4548 /// </summary>
4649 /// <param name="streams">A collection of streams.</param>
50+ /// <param name="leaveOpen"><see langword="true"/> to keep the wrapped streams alive when combined stream disposed; otherwise, <see langword="false"/>.</param>
4751 /// <returns>An object that represents multiple streams as one logical stream.</returns>
4852 /// <exception cref="ArgumentException"><paramref name="streams"/> is empty.</exception>
49- public static Stream Combine ( this ReadOnlySpan < Stream > streams )
53+ public static Stream Combine ( this ReadOnlySpan < Stream > streams , bool leaveOpen = true )
5054 => streams is [ var first , .. var rest]
51- ? Combine ( first , rest )
55+ ? Combine ( first , rest , leaveOpen )
5256 : throw new ArgumentException ( ExceptionMessages . BufferTooSmall , nameof ( streams ) ) ;
5357
5458 /// <summary>
5559 /// Combines multiple readable streams.
5660 /// </summary>
5761 /// <param name="streams">A collection of streams.</param>
62+ /// <param name="leaveOpen"><see langword="true"/> to keep the wrapped streams alive when combined stream disposed; otherwise, <see langword="false"/>.</param>
5863 /// <returns>An object that represents multiple streams as one logical stream.</returns>
5964 /// <exception cref="ArgumentException"><paramref name="streams"/> is empty.</exception>
60- public static Stream Combine ( this IEnumerable < Stream > streams )
65+ public static Stream Combine ( this IEnumerable < Stream > streams , bool leaveOpen = true )
6166 {
6267 // Use buffer to allocate streams on the stack
6368 var buffer = new StreamBuffer ( ) ;
@@ -67,7 +72,7 @@ public static Stream Combine(this IEnumerable<Stream> streams)
6772 try
6873 {
6974 writer . AddAll ( streams ) ;
70- result = Combine ( writer . WrittenSpan ) ;
75+ result = Combine ( writer . WrittenSpan , leaveOpen ) ;
7176 }
7277 finally
7378 {
0 commit comments