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