Some updates:
[core.git] / framework / main / classes / file_directories / output / raw / class_FrameworkRawFileOutputPointer.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Filesystem\Pointer\Output;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\FileSystem\BaseFileIo;
7 use Org\Mxchange\CoreFramework\Filesystem\Pointer\OutputPointer;
8 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
9
10 // Import SPL stuff
11 use \InvalidArgumentException;
12 use \SplFileInfo;
13
14 /**
15  * A class for writing files
16  *
17  * @author              Roland Haeder <webmaster@shipsimu.org>
18  * @version             0.0.0
19 <<<<<<< HEAD:framework/main/classes/file_directories/output/raw/class_FrameworkRawFileOutputPointer.php
20  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
21 =======
22  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
23 >>>>>>> Some updates::inc/main/classes/file_directories/output/raw/class_FrameworkRawFileOutputPointer.php
24  * @license             GNU GPL 3.0 or any newer version
25  * @link                http://www.shipsimu.org
26  *
27  * This program is free software: you can redistribute it and/or modify
28  * it under the terms of the GNU General Public License as published by
29  * the Free Software Foundation, either version 3 of the License, or
30  * (at your option) any later version.
31  *
32  * This program is distributed in the hope that it will be useful,
33  * but WITHOUT ANY WARRANTY; without even the implied warranty of
34  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35  * GNU General Public License for more details.
36  *
37  * You should have received a copy of the GNU General Public License
38  * along with this program. If not, see <http://www.gnu.org/licenses/>.
39  */
40 class FrameworkRawFileOutputPointer extends BaseFileIo implements OutputPointer {
41         /**
42          * Protected constructor
43          *
44          * @return      void
45          */
46         protected function __construct () {
47                 // Call parent constructor
48                 parent::__construct(__CLASS__);
49         }
50
51         /**
52          * Create a file pointer based on the given file. The file will also
53          * be verified here.
54          *
55          * @param       $infoInstance   An instance of a SplFileInfo class
56          * @param       $mode           The output mode ('w', 'a' are valid)
57          * @throws      InvalidArgumentException        If parameter mode is empty
58          * @throws      FileIoException                 If fopen() returns not a file resource
59          * @return      void
60          */
61         public static final function createFrameworkRawFileOutputPointer (SplFileInfo $infoInstance, $mode) {
62                 // Some pre-sanity checks...
63                 if (is_null($mode)) {
64                         // No infoInstance given
65                         throw new InvalidArgumentException('Parameter "mode" is empty');
66                 } // END - if
67
68                 // Try to open a handler
69                 $fileObject = $infoInstance->openFile($mode);
70                 if ((is_null($fileObject)) || ($fileObject === false)) {
71                         // Something bad happend
72                         throw new FileIoException($infoInstance, self::EXCEPTION_FILE_POINTER_INVALID);
73                 } // END - if
74
75                 // Create new instance
76                 $pointerInstance = new FrameworkRawFileOutputPointer();
77
78                 // Set file pointer and file name
79                 $pointerInstance->setFileObject($fileObject);
80
81                 // Return the instance
82                 return $pointerInstance;
83         }
84
85         /**
86          * Write data to a file pointer
87          *
88          * @param       $dataStream             The data stream we shall write to the file
89          * @return      mixed                   Number of writes bytes or false on error
90          * @throws      NullPointerException    If the file pointer instance is not set by setPointer()
91          * @throws      LogicException  If there is no object being set
92          */
93         public function writeToFile ($dataStream) {
94                 if (is_null($this->getFileObject())) {
95                         // Pointer not initialized
96                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
97                 } elseif (!is_object($this->getFileObject())) {
98                         // Pointer is not a valid resource!
99                         throw new LogicException(sprintf('this->fileObject[]=%s is no object', gettype($this->getFileObject())));
100                 }
101
102                 // Write data to the file pointer and return written bytes
103                 return $this->getFileObject()->fwrite($dataStream);
104         }
105
106         /**
107          * Analyzes entries in index file. This will count all found (and valid)
108          * entries, mark invalid as damaged and count gaps ("fragmentation"). If
109          * only gaps are found, the file is considered as "virgin" (no entries).
110          *
111          * @return      void
112          * @throws      UnsupportedOperationException   If this method is called
113          */
114         public function analyzeFile () {
115                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
116         }
117
118         /**
119          * Writes at given position by seeking to it.
120          *
121          * @param       $seekPosition   Seek position in file
122          * @param       $data                   Data to be written
123          * @return      mixed                   Number of writes bytes or false on error
124          * @throws      UnsupportedOperationException   If this method is called
125          */
126         public function writeAtPosition ($seedPosition, $data) {
127                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
128         }
129
130         /**
131          * Advances to next "block" of bytes
132          *
133          * @return      void
134          * @throws      UnsupportedOperationException   If this method is called
135          */
136         public function next () {
137                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
138         }
139
140         /**
141          * Checks wether the current entry is valid (not at the end of the file).
142          * This method will return true if an emptied (nulled) entry has been found.
143          *
144          * @return      $isValid        Whether the next entry is valid
145          * @throws      UnsupportedOperationException   If this method is called
146          */
147         public function valid () {
148                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
149         }
150
151         /**
152          * Gets current seek position ("key").
153          *
154          * @return      $key    Current key in iteration
155          * @throws      UnsupportedOperationException   If this method is called
156          */
157         public function key () {
158                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
159         }
160
161 }