Skip to content

Commit 6e475cb

Browse files
authored
Merge pull request #311 from thekid/feature/streams-value
Make all InputStream & OutputStream implementations implement Value
2 parents 57a383d + b5b7f78 commit 6e475cb

17 files changed

+145
-56
lines changed

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

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

3+
use lang\Value;
4+
use util\Comparison;
5+
36
/**
47
* Buffered InputStream
8+
*
9+
* @test net.xp_framework.unittest.io.streams.BufferedInputStreamTest
510
*/
6-
class BufferedInputStream implements InputStream {
7-
protected
8-
$in = null,
9-
$buf = '',
10-
$size = 0;
11+
class BufferedInputStream implements InputStream, Value {
12+
use Comparison;
13+
14+
protected $in, $size;
15+
protected $buf= '';
1116

1217
/**
1318
* Constructor
1419
*
15-
* @param io.streams.InputStream in
16-
* @param int size default 512
20+
* @param io.streams.InputStream $in
21+
* @param int $size
1722
*/
1823
public function __construct($in, $size= 512) {
1924
$this->in= $in;
@@ -61,6 +66,7 @@ public function available() {
6166
* @return void
6267
*/
6368
public function close() {
69+
// NOOP
6470
}
6571

6672
/** @return string */

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

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

3+
use lang\Value;
4+
use util\Comparison;
5+
36
/**
47
* OuputStream that writes to another OutputStream but buffers the
58
* results internally. This means not every single byte passed to
69
* write() will be written.
10+
*
11+
* @test net.xp_framework.unittest.io.streams.BufferedOutputStreamTest
712
*/
8-
class BufferedOutputStream implements OutputStream {
9-
protected
10-
$out = null,
11-
$buf = '',
12-
$size = 0;
13-
13+
class BufferedOutputStream implements OutputStream, Value {
14+
use Comparison;
15+
16+
protected $out, $size;
17+
protected $buf= '';
18+
1419
/**
1520
* Constructor
1621
*

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

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

3+
use io\IOException;
4+
use lang\{Value, IllegalArgumentException};
5+
use util\Comparison;
6+
37
/**
48
* OuputStream that compresses content using bzip2
59
*
610
* @ext bz2
711
* @test xp://net.xp_framework.unittest.io.streams.Bz2CompressingOutputStreamTest
812
*/
9-
class Bz2CompressingOutputStream implements OutputStream {
10-
protected $out= null;
13+
class Bz2CompressingOutputStream implements OutputStream, Value {
14+
use Comparison;
15+
16+
protected $out;
1117

1218
/**
1319
* Constructor
1420
*
15-
* @param io.streams.OutputStream out
16-
* @param int level default 6
17-
* @throws lang.IllegalArgumentException if the level is not between 0 and 9
21+
* @param io.streams.OutputStream $out
22+
* @param int $level
23+
* @throws lang.IllegalArgumentException if the level is not between 0 and 9
1824
*/
1925
public function __construct(OutputStream $out, $level= 6) {
2026
if ($level < 0 || $level > 9) {
21-
throw new \lang\IllegalArgumentException('Level '.$level.' out of range [0..9]');
27+
throw new IllegalArgumentException('Level '.$level.' out of range [0..9]');
2228
}
29+
2330
$this->out= Streams::writeableFd($out);
2431
if (!stream_filter_append($this->out, 'bzip2.compress', STREAM_FILTER_WRITE, ['blocks' => $level])) {
2532
fclose($this->out);
2633
$this->out= null;
27-
throw new \io\IOException('Could not append stream filter');
34+
throw new IOException('Could not append stream filter');
2835
}
2936
}
3037

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
11
<?php namespace io\streams;
22

3+
use io\IOException;
4+
use lang\Value;
5+
use util\Comparison;
6+
37
/**
48
* InputStream that decompresses
59
*
610
* @ext bz2
7-
* @test xp://net.xp_framework.unittest.io.streams.Bz2DecompressingInputStreamTest
11+
* @test net.xp_framework.unittest.io.streams.Bz2DecompressingInputStreamTest
812
*/
9-
class Bz2DecompressingInputStream implements InputStream {
10-
protected $in = null;
13+
class Bz2DecompressingInputStream implements InputStream, Value {
14+
use Comparison;
15+
16+
protected $in;
1117

1218
/**
1319
* Constructor
1420
*
15-
* @param io.streams.InputStream in
21+
* @param io.streams.InputStream $in
1622
*/
1723
public function __construct(InputStream $in) {
1824
$this->in= Streams::readableFd($in);
1925
if (!stream_filter_append($this->in, 'bzip2.decompress', STREAM_FILTER_READ)) {
20-
throw new \io\IOException('Could not append stream filter');
26+
throw new IOException('Could not append stream filter');
2127
}
2228
}
2329

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

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

33
use io\IOException;
4+
use lang\Value;
5+
use util\Comparison;
46

57
/**
68
* Input stream that reads from one of the "stdin", "input" channels
79
* provided as PHP input/output streams.
810
*
9-
* @test xp://net.xp_framework.unittest.io.streams.ChannelStreamTest
11+
* @test net.xp_framework.unittest.io.streams.ChannelStreamTest
1012
* @see php://wrappers
11-
* @see xp://io.streams.ChannelOutputStream
13+
* @see io.streams.ChannelOutputStream
1214
*/
13-
class ChannelInputStream implements InputStream {
14-
protected
15-
$name = null,
16-
$fd = null;
17-
15+
class ChannelInputStream implements InputStream, Value {
16+
use Comparison;
17+
18+
protected $fd, $name;
19+
1820
/**
1921
* Constructor
2022
*
@@ -25,6 +27,7 @@ public function __construct($arg) {
2527
if (!($this->fd= fopen('php://'.$arg, 'rb'))) {
2628
throw new IOException('Could not open '.$arg.' channel for reading');
2729
}
30+
$this->name= $arg;
2831
} else if (is_resource($arg)) {
2932
$this->fd= $arg;
3033
$this->name= '#'.(int)$arg;

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

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

33
use io\IOException;
4+
use lang\Value;
5+
use util\Comparison;
46

57
/**
68
* Output stream that writes to one of the "stdout", "stderr", "output"
79
* channels provided as PHP input/output streams.
810
*
9-
* @test xp://net.xp_framework.unittest.io.streams.ChannelStreamTest
11+
* @test net.xp_framework.unittest.io.streams.ChannelStreamTest
1012
* @see php://wrappers
11-
* @see xp://io.streams.ChannelInputStream
13+
* @see io.streams.ChannelInputStream
1214
*/
13-
class ChannelOutputStream implements OutputStream {
14-
protected
15-
$name = null,
16-
$fd = null;
15+
class ChannelOutputStream implements OutputStream, Value {
16+
use Comparison;
17+
18+
protected $fd, $name;
1719

1820
/**
1921
* Constructor
@@ -22,9 +24,10 @@ class ChannelOutputStream implements OutputStream {
2224
*/
2325
public function __construct($arg) {
2426
if ('stdout' === $arg || 'stderr' === $arg || 'output' === $arg) {
25-
if (!($this->fd= fopen('php://'.$arg, 'rb'))) {
26-
throw new IOException('Could not open '.$arg.' channel for reading');
27+
if (!($this->fd= fopen('php://'.$arg, 'wb'))) {
28+
throw new IOException('Could not open '.$arg.' channel for writing');
2729
}
30+
$this->name= $arg;
2831
} else if (is_resource($arg)) {
2932
$this->fd= $arg;
3033
$this->name= '#'.(int)$arg;

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<?php namespace io\streams;
22

3+
use lang\Value;
4+
use util\Comparison;
5+
36
/**
47
* InputStream that reads from the console
58
*
@@ -8,7 +11,9 @@
811
* $in= new ConsoleInputStream(STDIN);
912
* ```
1013
*/
11-
class ConsoleInputStream implements InputStream {
14+
class ConsoleInputStream implements InputStream, Value {
15+
use Comparison;
16+
1217
private $descriptor, $close;
1318

1419
/**

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<?php namespace io\streams;
22

3+
use lang\Value;
4+
use util\Comparison;
5+
36
/**
47
* OuputStream that writes to the console
58
*
@@ -9,7 +12,9 @@
912
* $err= new ConsoleOutputStream(STDERR);
1013
* ```
1114
*/
12-
class ConsoleOutputStream implements OutputStream {
15+
class ConsoleOutputStream implements OutputStream, Value {
16+
use Comparison;
17+
1318
private $descriptor, $close;
1419

1520
/**

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

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

3+
use lang\Value;
4+
use util\Comparison;
5+
36
/**
47
* OuputStream that deflates
58
*
69
* @ext zlib
7-
* @test xp://net.xp_framework.unittest.io.streams.DeflatingOutputStreamTest
10+
* @test net.xp_framework.unittest.io.streams.DeflatingOutputStreamTest
811
*/
9-
class DeflatingOutputStream implements OutputStream {
12+
class DeflatingOutputStream implements OutputStream, Value {
13+
use Comparison;
14+
1015
protected $out= null;
1116

1217
/**

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
<?php namespace io\streams;
22

33
use io\File;
4+
use lang\Value;
5+
use util\Comparison;
46

57
/**
68
* InputStream that reads from a file
79
*
810
* @test xp://net.xp_framework.unittest.io.streams.FileInputStreamTest
911
*/
10-
class FileInputStream implements InputStream, Seekable {
12+
class FileInputStream implements InputStream, Seekable, Value {
13+
use Comparison;
14+
1115
protected $file;
1216

1317
/**

0 commit comments

Comments
 (0)