3 namespace Org\Mxchange\CoreFramework\Handler\Filesystem;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
7 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
8 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
9 use Org\Mxchange\CoreFramework\Handler\Stream\IoHandler;
10 use Org\Mxchange\CoreFramework\Middleware\BaseMiddleware;
11 use Org\Mxchange\CoreFramework\Stream\Filesystem\FileInputStreamer;
12 use Org\Mxchange\CoreFramework\Stream\Filesystem\FileOutputStreamer;
18 * This is a file IO handler. It handles reading from and writing to files.
19 * Missing paths in writing process will be automatically created.
21 * @author Roland Haeder <webmaster@shipsimu.org>
23 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
24 * @license GNU GPL 3.0 or any newer version
25 * @link http://www.shipsimu.org
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.
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.
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/>.
40 class FileIoHandler extends BaseMiddleware implements IoHandler {
42 * The *real* file input class we shall use for reading data
44 private $inputStream = NULL;
47 * The *real* file output class we shall use for reading data
49 private $outputStream = NULL;
52 * An instance of this class
54 private static $selfInstance = NULL;
57 * Protected constructor
61 protected function __construct () {
62 // Call parent constructor
63 parent::__construct(__CLASS__);
66 self::$selfInstance = $this;
70 * Creates an instance of this class and prepares the IO system. This is
71 * being done by setting the default file IO class
73 * @return $ioInstance A prepared instance of FilIOHandler
75 public static final function createFileIoHandler () {
77 $ioHandler = new FileIoHandler();
79 // Set the *real* file IO instances (both the same)
80 $ioHandler->setInputStream(ObjectFactory::createObjectByConfiguredName('file_input_class'));
81 $ioHandler->setOutputStream(ObjectFactory::createObjectByConfiguredName('file_output_class'));
88 * Getter for an instance of this class
90 * @return $selfInstance An instance of this class
92 public static final function getSelfInstance () {
93 return self::$selfInstance;
97 * Setter for the *real* file input instance
99 * @param $inputStream The *real* file-input class
102 public final function setInputStream (FileInputStreamer $inputStream) {
103 $this->inputStream = $inputStream;
107 * Getter for the *real* file input instance
109 * @return $inputStream The *real* file-input class
111 public final function getInputStream () {
112 return $this->inputStream;
116 * Setter for the *real* file output instance
118 * @param $outputStream The *real* file-output class
121 public final function setOutputStream (FileOutputStreamer $outputStream) {
122 $this->outputStream = $outputStream;
126 * Getter for the *real* file output instance
128 * @return $outputStream The *real* file-output class
130 public final function getOutputStream () {
131 return $this->outputStream;
134 * Saves streamed (that are mostly serialized objects) data to files or
137 * @param $infoInstance An instance of a SplFileInfo class
138 * @param $dataArray Array containing the compressor's extension and streamed data
140 * @throws UnsupportedOperationException If this method is called
142 public function saveFile (SplFileInfo $infoInstance, array $dataArray) {
143 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('infoInstance.pathname=' . $infoInstance->getPathname() . ',dataArray()=' . count($dataArray));
144 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
148 * Saves a file with data by using the current output stream
150 * @param $infoInstance An instance of a SplFileInfo class
151 * @param $dataStream File data stream
152 * @param $objectInstance An instance of a FrameworkInterface class (default: NULL)
155 public function saveStreamToFile (SplFileInfo $infoInstance, $dataStream, FrameworkInterface $objectInstance = NULL) {
156 // Default is this array
157 $className = $this->__toString();
159 // Is the object instance set?
160 if ($objectInstance instanceof FrameworkInterface) {
162 $className = $objectInstance->__toString();
165 // Prepare output array
171 // Send the infoInstance and dataArray to the output handler
172 $this->getOutputStream()->saveFile($infoInstance, $dataArray);
175 /** Loads data from a file over the input handler
177 * @param $infoInstance An instance of a SplFileInfo class
178 * @return $array Array with the file contents
180 public function loadFileContents (SplFileInfo $infoInstance) {
181 // Read from the input handler
182 return $this->getInputStream()->loadFileContents($infoInstance);
186 * Determines seek position
188 * @return $seekPosition Current seek position
191 public function determineSeekPosition () {
192 $this->partialStub();
196 * Seek to given offset (default) or other possibilities as fseek() gives.
198 * @param $offset Offset to seek to (or used as "base" for other seeks)
199 * @param $whence Added to offset (default: only use offset to seek to)
200 * @return $status Status of file seek: 0 = success, -1 = failed
202 public function seek ($offset, $whence = SEEK_SET) {
203 $this->partialStub('offset=' . $offset . ',whence=' . $whence);
209 * @return $size Size (in bytes) of file
211 public function size () {
212 $this->partialStub();
216 * "Getter" for seek position
218 * @return $seekPosition Current seek position
221 public function getPosition () {
222 $this->partialStub();