XOOPS RMCommon Utilities  2.1.8.91RC
 All Classes Namespaces Files Functions Variables
FileByteStream.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of SwiftMailer.
5  * (c) 2004-2009 Chris Corbyn
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10 
11 //@require 'Swift/ByteStream/AbstractFilterableInputStream.php';
12 //@require 'Swift/InputByteStream.php';
13 //@require 'Swift/FileStream.php';
14 //@require 'Swift/IoException.php';
15 
24  implements Swift_FileStream
25 {
26 
28  private $_offset = 0;
29 
31  private $_path;
32 
34  private $_mode;
35 
37  private $_reader;
38 
40  private $_writer;
41 
43  private $_quotes = false;
44 
50  public function __construct($path, $writable = false)
51  {
52  $this->_path = $path;
53  $this->_mode = $writable ? 'w+b' : 'rb';
54  $this->_quotes = get_magic_quotes_runtime();
55  }
56 
61  public function getPath()
62  {
63  return $this->_path;
64  }
65 
75  public function read($length)
76  {
77  $fp = $this->_getReadHandle();
78  if (!feof($fp))
79  {
80  if ($this->_quotes)
81  {
82  set_magic_quotes_runtime(0);
83  }
84  $bytes = fread($fp, $length);
85  if ($this->_quotes)
86  {
87  set_magic_quotes_runtime(1);
88  }
89  $this->_offset = ftell($fp);
90  return $bytes;
91  }
92  else
93  {
94  return false;
95  }
96  }
97 
103  public function setReadPointer($byteOffset)
104  {
105  if (isset($this->_reader))
106  {
107  fseek($this->_reader, $byteOffset, SEEK_SET);
108  }
109  $this->_offset = $byteOffset;
110  }
111 
112  // -- Private methods
113 
115  protected function _commit($bytes)
116  {
117  fwrite($this->_getWriteHandle(), $bytes);
118  $this->_resetReadHandle();
119  }
120 
122  protected function _flush()
123  {
124  }
125 
127  private function _getReadHandle()
128  {
129  if (!isset($this->_reader))
130  {
131  if (!$this->_reader = fopen($this->_path, 'rb'))
132  {
133  throw new Swift_IoException(
134  'Unable to open file for reading [' . $this->_path . ']'
135  );
136  }
137  fseek($this->_reader, $this->_offset, SEEK_SET);
138  }
139  return $this->_reader;
140  }
141 
143  private function _getWriteHandle()
144  {
145  if (!isset($this->_writer))
146  {
147  if (!$this->_writer = fopen($this->_path, $this->_mode))
148  {
149  throw new Swift_IoException(
150  'Unable to open file for writing [' . $this->_path . ']'
151  );
152  }
153  }
154  return $this->_writer;
155  }
156 
158  private function _resetWriteHandle()
159  {
160  if (isset($this->_writer))
161  {
162  fclose($this->_writer);
163  $this->_writer = null;
164  }
165  }
166 
168  private function _resetReadHandle()
169  {
170  if (isset($this->_reader))
171  {
172  fclose($this->_reader);
173  $this->_reader = null;
174  }
175  }
176 
177 }