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