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;
14 * A class for writing files
16 * @author Roland Haeder <webmaster@shipsimu.org>
18 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
19 * @license GNU GPL 3.0 or any newer version
20 * @link http://www.shipsimu.org
22 * This program is free software: you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation, either version 3 of the License, or
25 * (at your option) any later version.
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with this program. If not, see <http://www.gnu.org/licenses/>.
35 class FrameworkRawFileOutputPointer extends BaseFileIo implements OutputPointer {
37 * Protected constructor
41 protected function __construct () {
42 // Call parent constructor
43 parent::__construct(__CLASS__);
47 * Create a file pointer based on the given file. The file will also
50 * @param $infoInstance An instance of a SplFileInfo class
51 * @param $mode The output mode ('w', 'a' are valid)
52 * @throws FileIsEmptyException If the provided file name is empty.
53 * @throws FileIoException If fopen() returns not a file resource
56 public static final function createFrameworkRawFileOutputPointer (SplFileInfo $infoInstance, $mode) {
57 // Some pre-sanity checks...
58 if (is_null($infoInstance)) {
59 // No infoInstance given
60 throw new FileIsEmptyException(NULL, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
63 // Try to open a handler
64 $fileObject = $infoInstance->openFile($mode);
65 if ((is_null($fileObject)) || ($fileObject === false)) {
66 // Something bad happend
67 throw new FileIoException ($infoInstance, self::EXCEPTION_FILE_POINTER_INVALID);
70 // Create new instance
71 $pointerInstance = new FrameworkRawFileOutputPointer();
73 // Set file pointer and file name
74 $pointerInstance->setFileObject($fileObject);
76 // Return the instance
77 return $pointerInstance;
81 * Write data to a file pointer
83 * @param $dataStream The data stream we shall write to the file
84 * @return mixed Number of writes bytes or false on error
85 * @throws NullPointerException If the file pointer instance is not set by setPointer()
86 * @throws LogicException If there is no object being set
88 public function writeToFile ($dataStream) {
89 if (is_null($this->getFileObject())) {
90 // Pointer not initialized
91 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
92 } elseif (!is_object($this->getFileObject())) {
93 // Pointer is not a valid resource!
94 throw new LogicException(sprintf('this->fileObject[]=%s is no object', gettype($this->getFileObject())));
97 // Write data to the file pointer and return written bytes
98 return $this->getFileObject()->fwrite($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);