34
34
using System . IO ;
35
35
using PdfSharpCore . Internal ;
36
36
using PdfSharpCore . Pdf . Internal ;
37
+ using System . Collections . Generic ;
38
+ using System . Linq ;
37
39
38
40
namespace PdfSharpCore . Pdf . IO
39
41
{
@@ -170,6 +172,19 @@ public Symbol ScanNextToken()
170
172
/// Reads the raw content of a stream.
171
173
/// </summary>
172
174
public byte [ ] ReadStream ( int length )
175
+ {
176
+ var pos = MoveToStartOfStream ( ) ;
177
+ _pdfSteam . Position = pos ;
178
+ byte [ ] bytes = new byte [ length ] ;
179
+ int read = _pdfSteam . Read ( bytes , 0 , length ) ;
180
+ Debug . Assert ( read == length ) ;
181
+
182
+ // Synchronize idxChar etc.
183
+ Position = pos + length ;
184
+ return bytes ;
185
+ }
186
+
187
+ internal long MoveToStartOfStream ( )
173
188
{
174
189
long pos ;
175
190
@@ -187,15 +202,45 @@ public byte[] ReadStream(int length)
187
202
}
188
203
else
189
204
pos = _idxChar + 1 ;
205
+ return pos ;
206
+ }
190
207
191
- _pdfSteam . Position = pos ;
192
- byte [ ] bytes = new byte [ length ] ;
193
- int read = _pdfSteam . Read ( bytes , 0 , length ) ;
194
- Debug . Assert ( read == length ) ;
208
+ /// <summary>
209
+ /// Scans the input stream for the specified marker.<br></br>
210
+ /// Returns the bytes from the current position up to the start of the marker or the end of the stream.<br></br>
211
+ /// The position of the input-stream is the byte right after the marker (if found) or the end of the stream.
212
+ /// </summary>
213
+ /// <param name="marker">The marker to scan for</param>
214
+ /// <param name="markerFound">Receives a boolean that indicates whether the marker was found</param>
215
+ /// <returns></returns>
216
+ internal byte [ ] ScanUntilMarker ( byte [ ] marker , out bool markerFound )
217
+ {
218
+ markerFound = false ;
219
+ var result = new List < byte > ( ) ;
220
+ while ( true )
221
+ {
222
+ var markerIndex = 0 ;
223
+ while ( _currChar != Chars . EOF && _currChar != marker [ markerIndex ] )
224
+ {
225
+ result . Add ( ( byte ) _currChar ) ;
226
+ ScanNextChar ( false ) ;
227
+ }
228
+ while ( _currChar != Chars . EOF && markerIndex < marker . Length && _currChar == marker [ markerIndex ] )
229
+ {
230
+ markerIndex ++ ;
231
+ ScanNextChar ( false ) ;
232
+ }
233
+ if ( _currChar == Chars . EOF || markerIndex == marker . Length )
234
+ {
235
+ if ( markerIndex == marker . Length )
236
+ markerFound = true ;
237
+ break ;
238
+ }
239
+ // only part of the marker was found, add to result and continue
240
+ result . AddRange ( marker . Take ( markerIndex ) ) ;
241
+ }
195
242
196
- // Synchronize idxChar etc.
197
- Position = pos + length ;
198
- return bytes ;
243
+ return result . ToArray ( ) ;
199
244
}
200
245
201
246
/// <summary>
@@ -722,7 +767,6 @@ public char MoveToNonWhiteSpace()
722
767
}
723
768
return _currChar ;
724
769
}
725
-
726
770
// #if DEBUG
727
771
// public string SurroundingsOfCurrentPosition(bool hex)
728
772
// {
0 commit comments