Login | Register
My pages Projects Community openCollabNet

Discussions > commits > svn commit: r212 - trunk/library/Cucua/Io/FileOutputStream.php

cucua
Discussion topic

Back to topic list

svn commit: r212 - trunk/library/Cucua/Io/FileOutputStream.php

Reply

Author siteartwork
Full name Thorsten Suckow-Homberg
Date 2006-06-10 16:58:50 PDT
Message Author: siteartwork
Date: 2006-06-10 16:58:49-0700
New Revision: 212

Modified:
   trunk/library/Cucua/​Io/FileOutputStream.​php

Log:
Updated for 0.1.0

Modified: trunk/library/Cucua/​Io/FileOutputStream.​php
Url: http://cucua.tigris.​org/source/browse/cu​cua/trunk/library/Cu​cua/Io/FileOutputStr​eam.php?view=diff​&rev=212&p1=tru​nk/library/Cucua/Io/​FileOutputStream.php​&p2=trunk/librar​y/Cucua/Io/FileOutpu​tStream.php&r1=2​11&r2=212
====================​====================​====================​==================
--- trunk/library/Cucua/​Io/FileOutputStream.​php (original)
+++ trunk/library/Cucua/​Io/FileOutputStream.​php 2006-06-10 16:58:49-0700
@@ -0,0 +1,166 @@
+<?php
+// +-------------------​--------------------​--------------------​-----------+
+// | FileOutputStream.php |
+// +-------------------​--------------------​--------------------​-----------+
+// | PHP Version 5.1 |
+// +-------------------​--------------------​--------------------​-----------+
+// | Copyright (C) 2006, The Cucua Open Source Project |
+// +-------------------​--------------------​--------------------​-----------+
+// | LICENSE |
+// | |
+// | This library is free software; you can redistribute it and/or |
+// | modify it under the terms of the GNU Lesser General Public |
+// | License as published by the Free Software Foundation; either |
+// | version 2.1 of the License, or (at your option) any later version. |
+// | |
+// | This library is distributed in the hope that it will be useful, |
+// | but WITHOUT ANY WARRANTY; without even the implied warranty of |
+// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
+// | Lesser General Public License for more details. |
+// | |
+// | You should have received a copy of the GNU Lesser General Public |
+// | License along with this library; if not, write to the Free Software |
+// | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
+// | MA 02110-1301 USA |
+// +-------------------​--------------------​--------------------​-----------+
+// | Author: Thorsten Suckow-Homberg <ts at siteartwork dot de> |
+// +-------------------​--------------------​--------------------​-----------+
+
+ /**
+ * Cucua_Io_IOException
+ */
+ require_once('Cucua/​Io/IOException.php')​;
+
+ /**
+ * Cucua_Io_OutputStrean
+ */
+ require_once('Cucua/​Io/OutputStream.php'​);
+
+ /**
+ * Cucua_Io_FileNotFoundException
+ */
+ require_once('Cucua/​Io/FileNotFoundExcep​tion.php');
+
+ /**
+ * Cucua_Lang_SecurityException
+ */
+ require_once('Cucua/​Lang/SecurityExcepti​on.php');
+
+/**
+ * Class for writing a stream of data to a file.
+ *
+ * @author Thorsten Suckow-Homberg <ts at siteartwork dot de>
+ * @package Cucua_Io
+ */
+class Cucua_Io_FileOutputStream extends Cucua_Io_OutputStream {
+
+ /**
+ * PHP native's file resource.
+ * @var resource
+ */
+ private $res;
+
+ /**
+ * The path the file can be found under.
+ * @var string
+ */
+ private $path;
+
+ /**
+ * Initializes a new instance of <b>Cucua_Io_Fi​leOutputrStream</​b>.
+ *
+ * Sets up a new nstance of this class for writing a stream of data
+ * to the given file.
+ *
+ * @param Cucua_Io_File|string $path A file object representing the file to
+ * write to or the path to the file as a
+ * string
+ * @param boolean $append true if the data should be appended at the end of the file,
+ * false to overwrite contents of the file with the stream of data
+ *
+ * @throws Cucua_Lang_SecurityException
+ * @throws Cucua_Io_IOException
+ */
+ public function __construct($path, $append = false)
+ {
+ switch ($append) {
+ case true:
+ $prop = 'w+';
+ break;
+
+ default:
+ $prop = 'w';
+ break;
+ }
+
+ if (!($path instanceof Cucua_Io_File)) {
+ $path = new Cucua_Io_File($path);
+ }
+
+ try {
+ if ($path->exists() && !$path->canWrite()) {
+ throw new Cucua_Io_IOException("Can't write to file: ".$path->getPath());
+ }
+ } catch (Cucua_Lang_SecurityException $se) {
+ throw new Cucua_Lang_SecurityE​xception("Can't write to file: ".$path->getPath());
+ }
+
+ $this->res = @fopen($path->getPath(), $prop);
+
+ if (!$this->res) {
+ throw new Cucua_Io_IOException("Proplem open/creating file: ".$path->getPath());
+ }
+
+ $this->path = $path->getPath();
+ }
+
+
+ /**
+ * Writes the given string to the file.
+ *
+ * @param string $string The string to write to the file
+ *
+ * @throws Cucua_Io_IOException
+ */
+ public function write($string)
+ {
+ if (!@fwrite($this->res, $string)) {
+ throw new Cucua_Io_IOException("Can't write to ".$this->path);
+ }
+ }
+
+ /**
+ * Closes the underlying file.
+ *
+ * Further attempts to write to this file using an instance of this class
+ * will most likely generate an exception.
+ *
+ * @throws Cucua_Io_IOException
+ */
+ public function close ()
+ {
+ if ($this->res === null) {
+ return;
+ }
+
+ if (!@fclose($this->res)) {
+ throw new Cucua_Io_IOException("problem closing ".$this->path):
+ }
+
+ $this->res = null;
+ }
+
+ /**
+ * Calls close() to make sure the filehandle is destroyed.
+ */
+ public function __destruct()
+ {
+ try {
+ $this->close()
+ } catch (IOException $io) {
+ // ignore
+ }
+ }
+
+}
+?>
\ No newline at end of file

« Previous message in topic | 1 of 1 | Next message in topic »

Messages

Show all messages in topic

svn commit: r212 - trunk/library/Cucua/Io/FileOutputStream.php siteartwork Thorsten Suckow-Homberg 2006-06-10 16:58:50 PDT
Messages per page: