3 namespace CoreFramework\Filesystem\Pointer\Output;
5 // Import framework stuff
6 use CoreFramework\FileSystem\BaseFileIo;
7 use CoreFramework\Filesystem\Pointer\OutputPointer;
8 use CoreFramework\Generic\NullPointerException;
11 * A class for writing files
13 * @author Roland Haeder <webmaster@shipsimu.org>
15 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
16 * @license GNU GPL 3.0 or any newer version
17 * @link http://www.shipsimu.org
19 * This program is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 class FrameworkRawFileOutputPointer extends BaseFileIo implements OutputPointer {
34 * Protected constructor
38 protected function __construct () {
39 // Call parent constructor
40 parent::__construct(__CLASS__);
44 * Create a file pointer based on the given file. The file will also
47 * @param $fileName The file name we shall pass to fopen()
48 * @param $mode The output mode ('w', 'a' are valid)
49 * @throws FileIsEmptyException If the provided file name is empty.
50 * @throws FileIoException If fopen() returns not a file resource
53 public static final function createFrameworkRawFileOutputPointer ($fileName, $mode) {
54 // Some pre-sanity checks...
55 if (is_null($fileName)) {
57 throw new FileIsEmptyException(NULL, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
60 // Try to open a handler
61 $filePointer = @fopen($fileName, $mode);
62 if ((is_null($filePointer)) || ($filePointer === FALSE)) {
63 // Something bad happend
64 throw new FileIoException ($fileName, self::EXCEPTION_FILE_POINTER_INVALID);
67 // Create new instance
68 $pointerInstance = new FrameworkRawFileOutputPointer();
70 // Set file pointer and file name
71 $pointerInstance->setPointer($filePointer);
72 $pointerInstance->setFileName($fileName);
74 // Return the instance
75 return $pointerInstance;
79 * Write data to a file pointer
81 * @param $dataStream The data stream we shall write to the file
82 * @return mixed Number of writes bytes or FALSE on error
83 * @throws NullPointerException If the file pointer instance
84 * is not set by setPointer()
85 * @throws InvalidResourceException If there is being set
86 * an invalid file resource
88 public function writeToFile ($dataStream) {
89 if (is_null($this->getPointer())) {
90 // Pointer not initialized
91 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
92 } elseif (!is_resource($this->getPointer())) {
93 // Pointer is not a valid resource!
94 throw new InvalidResourceException($this, self::EXCEPTION_INVALID_RESOURCE);
97 // Write data to the file pointer and return written bytes
98 return fwrite($this->getPointer(), $dataStream);
102 * Analyzes entries in index file. This will count all found (and valid)
103 * entries, mark invalid as damaged and count gaps ("fragmentation"). If
104 * only gaps are found, the file is considered as "virgin" (no entries).
107 * @throws UnsupportedOperationException If this method is called
109 public function analyzeFile () {
110 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
114 * Writes at given position by seeking to it.
116 * @param $seekPosition Seek position in file
117 * @param $data Data to be written
118 * @return mixed Number of writes bytes or FALSE on error
119 * @throws UnsupportedOperationException If this method is called
121 public function writeAtPosition ($seedPosition, $data) {
122 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
126 * Advances to next "block" of bytes
129 * @throws UnsupportedOperationException If this method is called
131 public function next () {
132 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
136 * Checks wether the current entry is valid (not at the end of the file).
137 * This method will return TRUE if an emptied (nulled) entry has been found.
139 * @return $isValid Whether the next entry is valid
140 * @throws UnsupportedOperationException If this method is called
142 public function valid () {
143 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
147 * Gets current seek position ("key").
149 * @return $key Current key in iteration
150 * @throws UnsupportedOperationException If this method is called
152 public function key () {
153 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);