File tree Expand file tree Collapse file tree 2 files changed +46
-1
lines changed Expand file tree Collapse file tree 2 files changed +46
-1
lines changed Original file line number Diff line number Diff line change @@ -186,9 +186,26 @@ public function seek($offset)
186
186
$ this ->chunkOffset = (integer ) floor ($ offset / $ this ->chunkSize );
187
187
$ this ->bufferOffset = $ offset % $ this ->chunkSize ;
188
188
189
- if ($ lastChunkOffset !== $ this ->chunkOffset ) {
189
+ /* If we are seeking to a previous chunk, we need to reinitialize the
190
+ * chunk iterator.
191
+ */
192
+ if ($ lastChunkOffset > $ this ->chunkOffset ) {
190
193
$ this ->buffer = null ;
191
194
$ this ->chunksIterator = null ;
195
+ return ;
196
+ }
197
+
198
+ /* If we are seeking to a subsequent chunk, we do not need to
199
+ * reinitalize the chunk iterator. Instead, we can simply move forward
200
+ * to $this->chunkOffset.
201
+ */
202
+ $ numChunks = $ this ->chunkOffset - $ lastChunkOffset ;
203
+ $ i = 0 ;
204
+ if ($ this ->chunksIterator !== null ) {
205
+ while ($ i < $ numChunks ) {
206
+ $ this ->chunksIterator ->next ();
207
+ $ i ++;
208
+ }
192
209
}
193
210
}
194
211
Original file line number Diff line number Diff line change @@ -199,4 +199,32 @@ public function testSeekOutOfRange()
199
199
200
200
$ stream ->seek (11 );
201
201
}
202
+
203
+ public function testSeekPreviousChunk ()
204
+ {
205
+ $ fileDocument = $ this ->collectionWrapper ->findFileById ('length-10 ' );
206
+ $ stream = new ReadableStream ($ this ->collectionWrapper , $ fileDocument );
207
+
208
+ $ stream ->readBytes (1 );
209
+ $ stream ->seek (5 );
210
+ $ stream ->seek (2 );
211
+ $ stream ->readBytes (1 );
212
+ }
213
+
214
+ public function testSeekSubsequentChunk ()
215
+ {
216
+ $ fileDocument = $ this ->collectionWrapper ->findFileById ('length-10 ' );
217
+
218
+ $ observer = $ this ->getMockBuilder (ReadableStream::class)
219
+ ->setConstructorArgs (array ($ this ->collectionWrapper , $ fileDocument ))
220
+ ->getMock ();
221
+
222
+ $ observer ->expects ($ this ->never ())
223
+ ->method ('initChunksIterator ' );
224
+
225
+ $ observer ->readBytes (1 );
226
+ $ observer ->seek (5 );
227
+ $ observer ->seek (2 );
228
+ $ observer ->readBytes (1 );
229
+ }
202
230
}
You can’t perform that action at this time.
0 commit comments