FileOutputStream.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2007 by Ivan Y. Khvostishkov                            *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 /* $Id: FileOutputStream.class.php 4687 2007-12-09 18:57:18Z voxus $ */
00012 
00016     final class FileOutputStream extends OutputStream
00017     {
00018         private $fd     = null;
00019         
00020         public function __construct($nameOrFd, $append = false)
00021         {
00022             if (is_resource($nameOrFd)) {
00023                 if (get_resource_type($nameOrFd) !== 'stream')
00024                     throw new IOException('not a file resource');
00025                 
00026                 $this->fd = $nameOrFd;
00027                 
00028             } else {
00029                 try {
00030                     $this->fd = fopen($nameOrFd, ($append ? 'a' : 'w').'b');
00031                 } catch (BaseException $e) {
00032                     throw new IOException($e->getMessage());
00033                 }
00034                 
00035             }
00036         }
00037         
00038         public function __destruct()
00039         {
00040             try {
00041                 $this->close();
00042             } catch (BaseException $e) {
00043                 // boo.
00044             }
00045         }
00046         
00050         public static function create($nameOrFd, $append = false)
00051         {
00052             return new self($nameOrFd, $append);
00053         }
00054         
00058         public function write($buffer)
00059         {
00060             if (!$this->fd || $buffer === null)
00061                 return $this;
00062             
00063             try {
00064                 $written = fwrite($this->fd, $buffer);
00065             } catch (BaseException $e) {
00066                 throw new IOException($e->getMessage());
00067             }
00068             
00069             if (!$written || $written < strlen($buffer))
00070                 throw new IOException('disk full and/or buffer too large?');
00071             
00072             return $this;
00073         }
00074         
00078         public function close()
00079         {
00080             fclose($this->fd);
00081             
00082             $this->fd = null;
00083             
00084             return $this;
00085         }
00086     }
00087 ?>

Generated on Sun Dec 9 21:56:24 2007 for onPHP by  doxygen 1.5.4