Skip to content

Commit da6fa86

Browse files
committed
Refactor: Extract seeking to Streams class
1 parent 0bff544 commit da6fa86

3 files changed

Lines changed: 24 additions & 16 deletions

File tree

src/main/php/io/Blob.class.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php namespace io;
22

33
use IteratorAggregate, Traversable;
4-
use io\streams\{InputStream, IterableInputStream, Seekable};
5-
use lang\{Value, IllegalArgumentException, IllegalStateException};
4+
use io\streams\{InputStream, IterableInputStream, Streams};
5+
use lang\{Value, IllegalArgumentException};
66
use util\{Bytes, Objects};
77

88
/** @test io.unittest.BlobTest */
@@ -22,15 +22,7 @@ public function __construct($parts= []) {
2222
static $started= false;
2323

2424
return (function() use(&$started) {
25-
if ($started) {
26-
if ($this->parts instanceof Seekable) {
27-
$this->parts->seek(0);
28-
} else {
29-
throw new IllegalStateException('Cannot seek '.Objects::stringOf($this->parts));
30-
}
31-
}
32-
33-
$started= true;
25+
$started ? Streams::seek($this->parts, 0) : $started= true;
3426
while ($this->parts->available()) {
3527
yield $this->parts->read();
3628
}

src/main/php/io/streams/Streams.class.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php namespace io\streams;
22

3-
use io\FileNotFoundException;
4-
use io\IOException;
3+
use io\{FileNotFoundException, OperationNotSupportedException, IOException};
54

65
/**
76
* Wraps I/O streams into PHP streams
@@ -134,6 +133,23 @@ public static function readAll(InputStream $s) {
134133
return $r;
135134
}
136135

136+
/**
137+
* Read an IOElements' contents completely into a buffer in a single call.
138+
*
139+
* @param io.streams.InputStream $s
140+
* @param int $offset
141+
* @param int $whence default SEEK_SET (one of SEEK_[SET|CUR|END])
142+
* @return void
143+
* @throws io.IOException
144+
*/
145+
public static function seek($s, $offset, $whence= SEEK_SET) {
146+
if ($s instanceof Seekable) {
147+
$s->seek($offset, $whence);
148+
} else {
149+
throw new OperationNotSupportedException('Cannot seek instances of '.nameof($s));
150+
}
151+
}
152+
137153
/**
138154
* Callback for fopen
139155
*

src/test/php/io/unittest/BlobTest.class.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php namespace io\unittest;
22

33
use ArrayObject;
4-
use io\Blob;
54
use io\streams\{MemoryInputStream, InputStream};
6-
use lang\{IllegalArgumentException, IllegalStateException};
5+
use io\{Blob, OperationNotSupportedException};
6+
use lang\IllegalArgumentException;
77
use test\{Assert, Expect, Test, Values};
88
use util\Bytes;
99

@@ -85,6 +85,6 @@ public function close() { $this->input= []; }
8585
});
8686
iterator_to_array($fixture->slices());
8787

88-
Assert::throws(IllegalStateException::class, fn() => iterator_to_array($fixture->slices()));
88+
Assert::throws(OperationNotSupportedException::class, fn() => iterator_to_array($fixture->slices()));
8989
}
9090
}

0 commit comments

Comments
 (0)