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