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\NullPointerException;
12 use \InvalidArgumentException;
17 * A class for writing files
19 * @author Roland Haeder <webmaster@shipsimu.org>
21 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2021 Core Developer Team
22 * @license GNU GPL 3.0 or any newer version
23 * @link http://www.shipsimu.org
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.
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.
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/>.
38 class FrameworkRawFileOutputPointer extends BaseFileIo implements OutputPointer {
40 * Protected constructor
44 private function __construct () {
45 // Call parent constructor
46 parent::__construct(__CLASS__);
50 * Create a file pointer based on the given file. The file will also
53 * @param $fileInstance An instance of a SplFileInfo class
54 * @param $mode The output mode ('w', 'a' are valid)
56 * @throws InvalidArgumentException If parameter mode is empty
57 * @throws FileIoException If fopen() returns not a file resource
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));
63 // No fileInstance given
64 throw new InvalidArgumentException('Parameter "mode" is empty');
67 // Try to open a handler
68 $fileObject = $fileInstance->openFile($mode);
71 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('RAW-FILE-OUTPUT-POINTER: fileObject[]=%s', gettype($fileObject)));
72 if (!($fileObject instanceof SplFileObject)) {
73 // Something bad happend
74 throw new FileIoException($fileInstance, self::EXCEPTION_FILE_POINTER_INVALID);
77 // Create new instance
78 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('RAW-FILE-OUTPUT-POINTER: Creating pointer instance ...');
79 $pointerInstance = new FrameworkRawFileOutputPointer();
81 // Set file pointer and file name
82 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('RAW-FILE-OUTPUT-POINTER: pointerInstance=%s,fileObject=%s', $pointerInstance->__toString(), $fileObject->__toString()));
83 $pointerInstance->setFileObject($fileObject);
85 // Return the instance
86 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('RAW-FILE-OUTPUT-POINTER: pointerInstance=%s - EXIT!', $pointerInstance->__toString()));
87 return $pointerInstance;
91 * Write data to a file pointer
93 * @param $dataStream The data stream we shall write to the file
94 * @return mixed Number of writes bytes or false on error
95 * @throws InvalidArgumentException If a parameter is invalid
96 * @throws NullPointerException If the file pointer instance is not set by setFileObject()
97 * @throws LogicException If there is no object being set
99 public function writeToFile (string $dataStream) {
100 // Validate parameter and class own attributes
101 if (empty($dataStream)) {
103 throw new InvalidArgumentException('Parameter "dataStream" is empty');
104 } elseif (is_null($this->getFileObject())) {
105 // Pointer not initialized
106 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
107 } elseif (!is_object($this->getFileObject())) {
108 // Pointer is not a valid resource!
109 throw new LogicException(sprintf('this->fileObject[]=%s is no object', gettype($this->getFileObject())));
112 // Write data to the file pointer and return written bytes
113 return $this->getFileObject()->fwrite($dataStream);
117 * Analyzes entries in index file. This will count all found (and valid)
118 * entries, mark invalid as damaged and count gaps ("fragmentation"). If
119 * only gaps are found, the file is considered as "virgin" (no entries).
122 * @throws UnsupportedOperationException If this method is called
124 public function analyzeFileStructure () {
125 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
129 * Writes at given position by seeking to it.
131 * @param $seekPosition Seek position in file
132 * @param $data Data to be written
133 * @return mixed Number of writes bytes or false on error
134 * @throws UnsupportedOperationException If this method is called
136 public function writeAtPosition (int $seedPosition, string $data) {
137 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
141 * Advances to next "block" of bytes
144 * @throws UnsupportedOperationException If this method is called
146 public function next () {
147 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
151 * Checks wether the current entry is valid (not at the end of the file).
152 * This method will return true if an emptied (nulled) entry has been found.
154 * @return $isValid Whether the next entry is valid
155 * @throws UnsupportedOperationException If this method is called
157 public function valid () {
158 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
162 * Gets current seek position ("key").
164 * @return $key Current key in iteration
165 * @throws UnsupportedOperationException If this method is called
167 public function key () {
168 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);