Skip to content

Commit a5e1b55

Browse files
committed
Copy SEEK_END behavior from fseek
1 parent 02886c5 commit a5e1b55

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

src/CachingStream.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ public function seek($offset, $whence = SEEK_SET)
5252
if ($size === null) {
5353
$size = $this->cacheEntireStream();
5454
}
55-
// Because 0 is the first byte, we seek to size - 1.
56-
$byte = $size - 1 - $offset;
55+
$byte = $size + $offset;
5756
} else {
5857
throw new \InvalidArgumentException('Invalid whence');
5958
}

tests/CachingStreamTest.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ public function testCanSeekNearEndWithSeekEnd()
4444
{
4545
$baseStream = Psr7\stream_for(implode('', range('a', 'z')));
4646
$cached = new CachingStream($baseStream);
47-
$cached->seek(1, SEEK_END);
48-
$this->assertEquals(24, $baseStream->tell());
49-
$this->assertEquals('y', $cached->read(1));
47+
$cached->seek(-1, SEEK_END);
48+
$this->assertEquals(25, $baseStream->tell());
49+
$this->assertEquals('z', $cached->read(1));
5050
$this->assertEquals(26, $cached->getSize());
5151
}
5252

@@ -55,8 +55,8 @@ public function testCanSeekToEndWithSeekEnd()
5555
$baseStream = Psr7\stream_for(implode('', range('a', 'z')));
5656
$cached = new CachingStream($baseStream);
5757
$cached->seek(0, SEEK_END);
58-
$this->assertEquals(25, $baseStream->tell());
59-
$this->assertEquals('z', $cached->read(1));
58+
$this->assertEquals(26, $baseStream->tell());
59+
$this->assertEquals('', $cached->read(1));
6060
$this->assertEquals(26, $cached->getSize());
6161
}
6262

@@ -67,8 +67,8 @@ public function testCanUseSeekEndWithUnknownSize()
6767
'getSize' => function () { return null; }
6868
]);
6969
$cached = new CachingStream($decorated);
70-
$cached->seek(1, SEEK_END);
71-
$this->assertEquals('ng', $cached->read(2));
70+
$cached->seek(-1, SEEK_END);
71+
$this->assertEquals('g', $cached->read(1));
7272
}
7373

7474
public function testRewindUsesSeek()

0 commit comments

Comments
 (0)