3 namespace Org\Mxchange\CoreFramework\Filesystem\Pointer\Output;
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\FrameworkInterface;
10 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
13 use \InvalidArgumentException;
18 * A class for writing files
20 * @author Roland Haeder <webmaster@shipsimu.org>
22 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2022 Core Developer Team
23 * @license GNU GPL 3.0 or any newer version
24 * @link http://www.shipsimu.org
26 * This program is free software: you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License as published by
28 * the Free Software Foundation, either version 3 of the License, or
29 * (at your option) any later version.
31 * This program is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 * GNU General Public License for more details.
36 * You should have received a copy of the GNU General Public License
37 * along with this program. If not, see <http://www.gnu.org/licenses/>.
39 class FrameworkRawFileOutputPointer extends BaseFileIo implements OutputPointer {
41 * Protected constructor
45 private function __construct () {
46 // Call parent constructor
47 parent::__construct(__CLASS__);
51 * Create a file pointer based on the given file. The file will also
54 * @param $fileInstance An instance of a SplFileInfo class
55 * @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
60 public static final function createFrameworkRawFileOutputPointer (SplFileInfo $fileInstance, string $mode) {
61 // Is the parameter valid?
62 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('RAW-FILE-OUTPUT-POINTER: fileInstance=%s,mode=%s - CALLED!', $fileInstance->__toString(), $mode));
64 // No fileInstance given
65 throw new InvalidArgumentException('Parameter "mode" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
68 // Try to open a handler
69 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('RAW-FILE-OUTPUT-POINTER: Invoking fileInstance->openFile(%s) ...', $mode));
70 $fileObject = $fileInstance->openFile($mode);
73 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('RAW-FILE-OUTPUT-POINTER: fileObject[]=%s', gettype($fileObject)));
74 if (!($fileObject instanceof SplFileObject)) {
75 // Something bad happend
76 throw new FileIoException($fileInstance, self::EXCEPTION_FILE_POINTER_INVALID);
79 // Create new instance
80 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('RAW-FILE-OUTPUT-POINTER: Creating pointer instance ...');
81 $pointerInstance = new FrameworkRawFileOutputPointer();
83 // Set file pointer and file name
84 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('RAW-FILE-OUTPUT-POINTER: pointerInstance=%s,fileObject=%s', $pointerInstance->__toString(), get_class($fileObject)));
85 $pointerInstance->setFileObject($fileObject);
87 // Return the instance
88 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('RAW-FILE-OUTPUT-POINTER: pointerInstance=%s - EXIT!', $pointerInstance->__toString()));
89 return $pointerInstance;
93 * Write data to a file pointer
95 * @param $dataStream The data stream we shall write to the file
96 * @return mixed Number of writes bytes or false on error
97 * @throws InvalidArgumentException If a parameter is invalid
98 * @throws NullPointerException If the file pointer instance is not set by setFileObject()
99 * @throws LogicException If there is no object being set
101 public function writeToFile (string $dataStream) {
102 // Validate parameter and class own attributes
103 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('RAW-FILE-OUTPUT-POINTER: dataStream(%d)=%s (trimmed) - CALLED!', strlen($dataStream), trim($dataStream)));
104 if (empty($dataStream)) {
106 throw new InvalidArgumentException('Parameter "dataStream" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
107 } elseif (is_null($this->getFileObject())) {
108 // Pointer not initialized
109 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
110 } elseif (!is_object($this->getFileObject())) {
111 // Pointer is not a valid resource!
112 throw new LogicException(sprintf('this->fileObject[]=%s is no object', gettype($this->getFileObject())), FrameworkInterface::EXCEPTION_LOGIC_EXCEPTION);
115 // Write data to the file pointer and return written bytes
116 return $this->getFileObject()->fwrite($dataStream);
120 * Analyzes entries in index file. This will count all found (and valid)
121 * entries, mark invalid as damaged and count gaps ("fragmentation"). If
122 * only gaps are found, the file is considered as "virgin" (no entries).
125 * @throws UnsupportedOperationException If this method is called
127 public function analyzeFileStructure () {
128 throw new UnsupportedOperationException([$this, __FUNCTION__], self::EXCEPTION_UNSPPORTED_OPERATION);
132 * Writes at given position by seeking to it.
134 * @param $seekPosition Seek position in file
135 * @param $data Data to be written
136 * @return mixed Number of writes bytes or false on error
137 * @throws UnsupportedOperationException If this method is called
139 public function writeAtPosition (int $seedPosition, string $data) {
140 throw new UnsupportedOperationException([$this, __FUNCTION__], self::EXCEPTION_UNSPPORTED_OPERATION);
144 * Advances to next "block" of bytes
147 * @throws UnsupportedOperationException If this method is called
149 public function next () {
150 throw new UnsupportedOperationException([$this, __FUNCTION__], self::EXCEPTION_UNSPPORTED_OPERATION);
154 * Checks wether the current entry is valid (not at the end of the file).
155 * This method will return true if an emptied (nulled) entry has been found.
157 * @return $isValid Whether the next entry is valid
158 * @throws UnsupportedOperationException If this method is called
160 public function valid () {
161 throw new UnsupportedOperationException([$this, __FUNCTION__], self::EXCEPTION_UNSPPORTED_OPERATION);
165 * Gets current seek position ("key").
167 * @return $key Current key in iteration
168 * @throws UnsupportedOperationException If this method is called
170 public function key () {
171 throw new UnsupportedOperationException([$this, __FUNCTION__], self::EXCEPTION_UNSPPORTED_OPERATION);