OutputStream.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /*
  3. * $Id: OutputStream.php 905 2010-10-05 16:28:03Z 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. * Wrapper class for PHP stream that supports write operations.
  23. *
  24. * @package phing.system.io
  25. */
  26. class OutputStream {
  27. /**
  28. * @var resource The configured PHP stream.
  29. */
  30. protected $stream;
  31. /**
  32. * Construct a new OutputStream.
  33. * @param resource $stream Configured PHP stream for writing.
  34. */
  35. public function __construct($stream) {
  36. if (!is_resource($stream)) {
  37. throw new IOException("Passed argument is not a valid stream.");
  38. }
  39. $this->stream = $stream;
  40. }
  41. /**
  42. * Closes attached stream, flushing output first.
  43. * @throws IOException if cannot close stream (note that attempting to close an already closed stream will not raise an IOException)
  44. * @return void
  45. */
  46. public function close() {
  47. if ($this->stream === null) {
  48. return;
  49. }
  50. $this->flush();
  51. if (false === @fclose($this->stream)) {
  52. $msg = "Cannot close " . $this->getResource() . ": $php_errormsg";
  53. throw new IOException($msg);
  54. }
  55. $this->stream = null;
  56. }
  57. /**
  58. * Flushes stream.
  59. *
  60. * @throws IOException if unable to flush data (e.g. stream is not open).
  61. */
  62. public function flush() {
  63. if (false === @fflush($this->stream)) {
  64. throw new IOException("Could not flush stream: " . $php_errormsg);
  65. }
  66. }
  67. /**
  68. * Writes data to stream.
  69. *
  70. * @param string $buf Binary/character data to write.
  71. * @param int $off (Optional) offset.
  72. * @param int $len (Optional) number of bytes/chars to write.
  73. * @return void
  74. * @throws IOException - if there is an error writing to stream
  75. */
  76. public function write($buf, $off = null, $len = null) {
  77. if ( $off === null && $len === null ) {
  78. $to_write = $buf;
  79. } elseif ($off !== null && $len === null) {
  80. $to_write = substr($buf, $off);
  81. } elseif ($off === null && $len !== null) {
  82. $to_write = substr($buf, 0, $len);
  83. } else {
  84. $to_write = substr($buf, $off, $len);
  85. }
  86. $result = @fwrite($this->stream, $to_write);
  87. if ( $result === false ) {
  88. throw new IOException("Error writing to stream.");
  89. }
  90. }
  91. /**
  92. * Returns a string representation of the attached PHP stream.
  93. * @return string
  94. */
  95. public function __toString() {
  96. return (string) $this->stream;
  97. }
  98. }