Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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