|
TYPO3 API
SVNRelease
|
00001 <?php 00002 00003 /* 00004 * This file is part of SwiftMailer. 00005 * (c) 2004-2009 Chris Corbyn 00006 * 00007 * For the full copyright and license information, please view the LICENSE 00008 * file that was distributed with this source code. 00009 */ 00010 00011 //@require 'Swift/ByteStream/AbstractFilterableInputStream.php'; 00012 //@require 'Swift/InputByteStream.php'; 00013 //@require 'Swift/FileStream.php'; 00014 //@require 'Swift/IoException.php'; 00015 00016 /** 00017 * Allows reading and writing of bytes to and from a file. 00018 * @package Swift 00019 * @subpackage ByteStream 00020 * @author Chris Corbyn 00021 */ 00022 class Swift_ByteStream_FileByteStream 00023 extends Swift_ByteStream_AbstractFilterableInputStream 00024 implements Swift_FileStream 00025 { 00026 00027 /** The internal pointer offset */ 00028 private $_offset = 0; 00029 00030 /** The path to the file */ 00031 private $_path; 00032 00033 /** The mode this file is opened in for writing */ 00034 private $_mode; 00035 00036 /** A lazy-loaded resource handle for reading the file */ 00037 private $_reader; 00038 00039 /** A lazy-loaded resource handle for writing the file */ 00040 private $_writer; 00041 00042 /** If magic_quotes_runtime is on, this will be true */ 00043 private $_quotes = false; 00044 00045 /** 00046 * Create a new FileByteStream for $path. 00047 * @param string $path 00048 * @param string $writable if true 00049 */ 00050 public function __construct($path, $writable = false) 00051 { 00052 $this->_path = $path; 00053 $this->_mode = $writable ? 'w+b' : 'rb'; 00054 $this->_quotes = get_magic_quotes_runtime(); 00055 } 00056 00057 /** 00058 * Get the complete path to the file. 00059 * @return string 00060 */ 00061 public function getPath() 00062 { 00063 return $this->_path; 00064 } 00065 00066 /** 00067 * Reads $length bytes from the stream into a string and moves the pointer 00068 * through the stream by $length. If less bytes exist than are requested the 00069 * remaining bytes are given instead. If no bytes are remaining at all, boolean 00070 * false is returned. 00071 * @param int $length 00072 * @return string 00073 * @throws Swift_IoException 00074 */ 00075 public function read($length) 00076 { 00077 $fp = $this->_getReadHandle(); 00078 if (!feof($fp)) 00079 { 00080 if ($this->_quotes) 00081 { 00082 set_magic_quotes_runtime(0); 00083 } 00084 $bytes = fread($fp, $length); 00085 if ($this->_quotes) 00086 { 00087 set_magic_quotes_runtime(1); 00088 } 00089 $this->_offset = ftell($fp); 00090 return $bytes; 00091 } 00092 else 00093 { 00094 return false; 00095 } 00096 } 00097 00098 /** 00099 * Move the internal read pointer to $byteOffset in the stream. 00100 * @param int $byteOffset 00101 * @return boolean 00102 */ 00103 public function setReadPointer($byteOffset) 00104 { 00105 if (isset($this->_reader)) 00106 { 00107 fseek($this->_reader, $byteOffset, SEEK_SET); 00108 } 00109 $this->_offset = $byteOffset; 00110 } 00111 00112 // -- Private methods 00113 00114 /** Just write the bytes to the file */ 00115 protected function _commit($bytes) 00116 { 00117 fwrite($this->_getWriteHandle(), $bytes); 00118 $this->_resetReadHandle(); 00119 } 00120 00121 /** Not used */ 00122 protected function _flush() 00123 { 00124 } 00125 00126 /** Get the resource for reading */ 00127 private function _getReadHandle() 00128 { 00129 if (!isset($this->_reader)) 00130 { 00131 if (!$this->_reader = fopen($this->_path, 'rb')) 00132 { 00133 throw new Swift_IoException( 00134 'Unable to open file for reading [' . $this->_path . ']' 00135 ); 00136 } 00137 fseek($this->_reader, $this->_offset, SEEK_SET); 00138 } 00139 return $this->_reader; 00140 } 00141 00142 /** Get the resource for writing */ 00143 private function _getWriteHandle() 00144 { 00145 if (!isset($this->_writer)) 00146 { 00147 if (!$this->_writer = fopen($this->_path, $this->_mode)) 00148 { 00149 throw new Swift_IoException( 00150 'Unable to open file for writing [' . $this->_path . ']' 00151 ); 00152 } 00153 } 00154 return $this->_writer; 00155 } 00156 00157 /** Force a reload of the resource for writing */ 00158 private function _resetWriteHandle() 00159 { 00160 if (isset($this->_writer)) 00161 { 00162 fclose($this->_writer); 00163 $this->_writer = null; 00164 } 00165 } 00166 00167 /** Force a reload of the resource for reading */ 00168 private function _resetReadHandle() 00169 { 00170 if (isset($this->_reader)) 00171 { 00172 fclose($this->_reader); 00173 $this->_reader = null; 00174 } 00175 } 00176 00177 }
1.8.0