|
1 | 1 | using System.IO; |
| 2 | +using System.IO.Pipelines; |
| 3 | +using System.Threading; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using BencodeNET.IO; |
2 | 6 | using BencodeNET.Objects; |
3 | 7 |
|
4 | 8 | namespace BencodeNET.Parsing |
@@ -94,44 +98,71 @@ public static T Parse<T>(this IBencodeParser parser, string filePath) where T : |
94 | 98 | } |
95 | 99 | } |
96 | 100 |
|
97 | | -#if !NETSTANDARD1_3 |
98 | 101 | /// <summary> |
99 | | - /// Parses a stream through a <see cref="BufferedStream"/> into an <see cref="IBObject"/>. |
100 | | - /// The input <paramref name="stream"/> is disposed when parsing is completed. |
| 102 | + /// Parses a stream into an <see cref="IBObject"/>. |
101 | 103 | /// </summary> |
102 | 104 | /// <param name="parser"></param> |
103 | 105 | /// <param name="stream">The stream to parse.</param> |
104 | | - /// <param name="bufferSize">The buffer size to use. Uses default size of <see cref="BufferedStream"/> if null.</param> |
105 | 106 | /// <returns>The parsed object.</returns> |
106 | | - public static IBObject ParseBuffered(this IBencodeParser parser, Stream stream, int? bufferSize = null) |
| 107 | + public static IBObject Parse(this IBencodeParser parser, Stream stream) |
107 | 108 | { |
108 | | -; |
109 | | - using (var bufferedStream = bufferSize == null |
110 | | - ? new BufferedStream(stream) |
111 | | - : new BufferedStream(stream, bufferSize.Value)) |
| 109 | + using (var reader = new BencodeReader(stream, leaveOpen: true)) |
112 | 110 | { |
113 | | - return parser.Parse(bufferedStream); |
| 111 | + return parser.Parse(reader); |
114 | 112 | } |
115 | 113 | } |
116 | 114 |
|
117 | 115 | /// <summary> |
118 | | - /// Parses a stream through a <see cref="BufferedStream"/> into an <see cref="IBObject"/> of type <typeparamref name="T"/>. |
119 | | - /// The input <paramref name="stream"/> is disposed when parsing is completed. |
| 116 | + /// Parses a stream into an <see cref="IBObject"/> of type <typeparamref name="T"/>. |
120 | 117 | /// </summary> |
121 | 118 | /// <typeparam name="T">The type of <see cref="IBObject"/> to parse as.</typeparam> |
122 | 119 | /// <param name="parser"></param> |
123 | 120 | /// <param name="stream">The stream to parse.</param> |
124 | | - /// <param name="bufferSize">The buffer size to use. Uses default size of <see cref="BufferedStream"/> if null.</param> |
125 | 121 | /// <returns>The parsed object.</returns> |
126 | | - public static T ParseBuffered<T>(this IBencodeParser parser, Stream stream, int? bufferSize = null) where T : class, IBObject |
| 122 | + public static T Parse<T>(this IBencodeParser parser, Stream stream) where T : class, IBObject |
127 | 123 | { |
128 | | - using (var bufferedStream = bufferSize == null |
129 | | - ? new BufferedStream(stream) |
130 | | - : new BufferedStream(stream, bufferSize.Value)) |
| 124 | + using (var reader = new BencodeReader(stream, leaveOpen: true)) |
131 | 125 | { |
132 | | - return parser.Parse<T>(bufferedStream); |
| 126 | + return parser.Parse<T>(reader); |
133 | 127 | } |
134 | 128 | } |
135 | | -#endif |
| 129 | + |
| 130 | + /// <summary> |
| 131 | + /// Parses an <see cref="IBObject"/> from the <see cref="PipeReader"/>. |
| 132 | + /// </summary> |
| 133 | + public static ValueTask<IBObject> ParseAsync(this IBencodeParser parser, PipeReader pipeReader, CancellationToken cancellationToken = default) |
| 134 | + { |
| 135 | + var reader = new PipeBencodeReader(pipeReader); |
| 136 | + return parser.ParseAsync(reader, cancellationToken); |
| 137 | + } |
| 138 | + |
| 139 | + /// <summary> |
| 140 | + /// Parses an <see cref="IBObject"/> of type <typeparamref name="T"/> from the <see cref="PipeReader"/>. |
| 141 | + /// </summary> |
| 142 | + /// <typeparam name="T">The type of <see cref="IBObject"/> to parse as.</typeparam> |
| 143 | + public static ValueTask<T> ParseAsync<T>(this IBencodeParser parser, PipeReader pipeReader, CancellationToken cancellationToken = default) where T : class, IBObject |
| 144 | + { |
| 145 | + var reader = new PipeBencodeReader(pipeReader); |
| 146 | + return parser.ParseAsync<T>(reader, cancellationToken); |
| 147 | + } |
| 148 | + |
| 149 | + /// <summary> |
| 150 | + /// Parses an <see cref="IBObject"/> from the <see cref="Stream"/> asynchronously using a <see cref="PipeReader"/>. |
| 151 | + /// </summary> |
| 152 | + public static ValueTask<IBObject> ParseAsync(this IBencodeParser parser, Stream stream, StreamPipeReaderOptions readerOptions = null, CancellationToken cancellationToken = default) |
| 153 | + { |
| 154 | + var reader = PipeReader.Create(stream, readerOptions); |
| 155 | + return parser.ParseAsync(reader, cancellationToken); |
| 156 | + } |
| 157 | + |
| 158 | + /// <summary> |
| 159 | + /// Parses an <see cref="IBObject"/> of type <typeparamref name="T"/> from the <see cref="Stream"/> asynchronously using a <see cref="PipeReader"/>. |
| 160 | + /// </summary> |
| 161 | + /// <typeparam name="T">The type of <see cref="IBObject"/> to parse as.</typeparam> |
| 162 | + public static ValueTask<T> ParseAsync<T>(this IBencodeParser parser, Stream stream, StreamPipeReaderOptions readerOptions = null, CancellationToken cancellationToken = default) where T : class, IBObject |
| 163 | + { |
| 164 | + var reader = PipeReader.Create(stream, readerOptions); |
| 165 | + return parser.ParseAsync<T>(reader, cancellationToken); |
| 166 | + } |
136 | 167 | } |
137 | 168 | } |
0 commit comments