|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
14
|
|
|
* |
|
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
|
16
|
|
|
* and is licensed under the LGPL. For more information please see |
|
17
|
|
|
* <http://phing.info>. |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Wrapper class for PHP stream that supports write operations. |
|
22
|
|
|
* |
|
23
|
|
|
* @package phing.system.io |
|
24
|
|
|
*/ |
|
25
|
|
|
class OutputStream |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var resource The configured PHP stream. |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $stream; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Construct a new OutputStream. |
|
34
|
|
|
* |
|
35
|
|
|
* @param resource $stream Configured PHP stream for writing. |
|
36
|
|
|
* @throws IOException |
|
37
|
|
|
*/ |
|
38
|
43 |
|
public function __construct($stream) |
|
39
|
|
|
{ |
|
40
|
43 |
|
if (!is_resource($stream)) { |
|
41
|
|
|
throw new IOException("Passed argument is not a valid stream."); |
|
42
|
|
|
} |
|
43
|
43 |
|
$this->stream = $stream; |
|
44
|
43 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Closes attached stream, flushing output first. |
|
48
|
|
|
* |
|
49
|
|
|
* @throws IOException if cannot close stream (note that attempting to close an already closed stream will not raise an IOException) |
|
50
|
|
|
* @return void |
|
51
|
|
|
*/ |
|
52
|
35 |
|
public function close() |
|
53
|
|
|
{ |
|
54
|
35 |
|
if ($this->stream === null) { |
|
55
|
|
|
return; |
|
56
|
|
|
} |
|
57
|
35 |
|
$this->flush(); |
|
58
|
35 |
|
error_clear_last(); |
|
59
|
35 |
|
if (false === @fclose($this->stream)) { |
|
60
|
|
|
$lastError = error_get_last(); |
|
61
|
|
|
$errormsg = $lastError['message']; |
|
62
|
|
|
$metaData = stream_get_meta_data($this->stream); |
|
63
|
|
|
$resource = $metaData["uri"]; |
|
64
|
|
|
$msg = "Cannot close " . $resource . ": $errormsg"; |
|
65
|
|
|
throw new IOException($msg); |
|
66
|
|
|
} |
|
67
|
35 |
|
$this->stream = null; |
|
68
|
35 |
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Flushes stream. |
|
72
|
|
|
* |
|
73
|
|
|
* @throws IOException if unable to flush data (e.g. stream is not open). |
|
74
|
|
|
*/ |
|
75
|
36 |
|
public function flush() |
|
76
|
|
|
{ |
|
77
|
36 |
|
error_clear_last(); |
|
78
|
36 |
|
if (false === @fflush($this->stream)) { |
|
79
|
|
|
$lastError = error_get_last(); |
|
80
|
|
|
$errormsg = $lastError['message']; |
|
81
|
|
|
throw new IOException("Could not flush stream: " . $errormsg); |
|
82
|
|
|
} |
|
83
|
36 |
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Writes data to stream. |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $buf Binary/character data to write. |
|
89
|
|
|
* @param int $off (Optional) offset. |
|
90
|
|
|
* @param int $len (Optional) number of bytes/chars to write. |
|
91
|
|
|
* @return void |
|
92
|
|
|
* @throws IOException - if there is an error writing to stream |
|
93
|
|
|
*/ |
|
94
|
41 |
|
public function write($buf, $off = null, $len = null) |
|
95
|
|
|
{ |
|
96
|
41 |
|
if ($off === null && $len === null) { |
|
97
|
41 |
|
$to_write = $buf; |
|
98
|
1 |
|
} elseif ($off !== null && $len === null) { |
|
99
|
1 |
|
$to_write = substr($buf, $off); |
|
100
|
1 |
|
} elseif ($off === null && $len !== null) { |
|
101
|
|
|
$to_write = substr($buf, 0, $len); |
|
102
|
|
|
} else { |
|
103
|
1 |
|
$to_write = substr($buf, $off, $len); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
41 |
|
$result = @fwrite($this->stream, $to_write); |
|
107
|
|
|
|
|
108
|
41 |
|
if ($result === false) { |
|
109
|
|
|
throw new IOException("Error writing to stream."); |
|
110
|
|
|
} |
|
111
|
41 |
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Returns a string representation of the attached PHP stream. |
|
115
|
|
|
* |
|
116
|
|
|
* @return string |
|
117
|
|
|
*/ |
|
118
|
|
|
public function __toString() |
|
119
|
|
|
{ |
|
120
|
|
|
return (string) $this->stream; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|