// Import SPL stuff
use \BadMethodCallException;
+use \InvalidArgumentException;
use \SplFileInfo;
/**
*
* @param $length Length of raw data
* @return $seekPosition Found next gap's seek position
+ * @throws InvalidArgumentException If the parameter is not valid
*/
public function searchNextGap (int $length) {
// If the file is only gaps, no need to seek
- if ($this->isFileOnlyGaps()) {
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: length=%d - CALLED!', $length));
+ if ($length <= 0) {
+ // Throw IAE
+ throw new InvalidArgumentException(sprintf('length=%d is not valid', $length));
+ } elseif ($this->isFileOnlyGaps()) {
// The first empty block is the first one right after the header
return ($this->getHeaderSize() + 1);
}
* @param $totalEntries Total entries in this file
* @return void
*/
- protected final function setCounter ($counter) {
+ protected final function setCounter (int $counter) {
// Set it
$this->totalEntries = $counter;
}
* @throws InvalidResourceException If there is being set
* an invalid file resource
*/
- public function writeToFile ($dataStream) {
+ public function writeToFile (string $dataStream) {
// Call pointer instance
return $this->getPointerInstance()->writeToFile($dataStream);
}
* @param $bytes Amount of bytes to read
* @return $data Data read from file
*/
- public function read ($bytes = NULL) {
- // $bytes shall be integer
- assert(is_int($bytes));
-
+ public function read (int $bytes = NULL) {
// Try to read given characters
$data = $this->getFileObject()->fread($bytes);
* @throws NullPointerException If the file pointer instance is not set by setFileObject()
* @throws InvalidResourceException If there is no object being set
*/
- public function read ($bytes = NULL) {
+ public function read (int $bytes = NULL) {
// Some sanity checks
if (is_null($this->getFileObject())) {
// Pointer not initialized
* @param $dataStream The data stream we shall write to the file
* @return mixed Number of writes bytes or false on error
*/
- public function writeToFile ($dataStream) {
+ public function writeToFile (string $dataStream) {
// Validate the pointer
$this->validateFilePointer();
* @param $data Data to be written
* @return mixed Number of writes bytes or false on error
*/
- public function writeAtPosition ($seekPosition, $data) {
+ public function writeAtPosition (int $seekPosition, string $data) {
// First seek to it
$this->seek($seekPosition);
* @param $bytes Amount of bytes to read
* @return $data Data read from file
*/
- public function read ($bytes = NULL) {
+ public function read (int $bytes = NULL) {
// Validate the pointer
$this->validateFilePointer();
* @throws NullPointerException If the file pointer instance is not set by setFileObject()
* @throws LogicException If there is no object being set
*/
- public function writeToFile ($dataStream) {
+ public function writeToFile (string $dataStream) {
if (is_null($this->getFileObject())) {
// Pointer not initialized
throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
* @return mixed Number of writes bytes or false on error
* @throws UnsupportedOperationException If this method is called
*/
- public function writeAtPosition ($seedPosition, $data) {
+ public function writeAtPosition (int $seedPosition, string $data) {
throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
}
* @throws NullPointerException If the file pointer instance is not set by setFileObject()
* @throws LogicException If there is no object being set
*/
- public function writeToFile ($dataStream) {
+ public function writeToFile (string $dataStream) {
if (is_null($this->getFileObject())) {
// Pointer not initialized
throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
* @return mixed Number of writes bytes or false on error
* @throws UnsupportedOperationException If this method is called
*/
- public function writeAtPosition ($seedPosition, $data) {
+ public function writeAtPosition (int $seedPosition, string $data) {
throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
}
use Org\Mxchange\CoreFramework\Stacker\Index\IndexableStack;
// Import SPL stuff
+use \InvalidArgumentException;
use \SplFileInfo;
/**
*
* @param $length Length of raw data
* @return $seekPosition Found next gap's seek position
+ * @throws InvalidArgumentException If the parameter is not valid
+ * @todo Unfinished work
*/
public function searchNextGap (int $length) {
- $this->partialStub('length=' . $length);
+ // Validate parameter
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-STACK-INDEX: length=%d - CALLED!', $length));
+ if ($length <= 0) {
+ // Throw IAE
+ throw new InvalidArgumentException(sprintf('length=%d is not valid', $length));
+ }
}
}
*/
public function searchNextGap (int $length) {
// Call block instance
- print $this->getBlockInstance()->__toString() . PHP_EOL;
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-ITERATOR: length=%d - CALLED!', $length));
return $this->getBlockInstance()->searchNextGap($length);
}
* @throws UnsupportedOperationException This method is not (and maybe never will be) supported
*/
public function searchNextGap (int $length) {
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: length=%s', $length));
+ // Not supported here
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: length=%d - CALLED!', $length));
throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
}
$gapPosition = $this->getIteratorInstance()->searchNextGap(strlen($rawData));
// Gap position cannot be smaller than header length + 1
- assert($gapPosition > $this->getIteratorInstance()->getHeaderSize());
+ if ($gapPosition <= $this->getIteratorInstance()->getHeaderSize()) {
+ // Improper gap position
+ throw new UnexpectedValueException(sprintf('gapPosition[%s]=%d is not larger than headerSize=%d',
+ gettype($gapPosition),
+ $gapPosition,
+ $this->getIteratorInstance()->getHeaderSize()
+ ));
+ }
// Then write the data at that gap
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: groupId=%s,hash=%s,gapPosition=%s', $groupId, $hash, $gapPosition));
* is not set by setFileObject()
* @throws InvalidResourceException If there is being set
*/
- function read ($bytes = NULL);
+ function read (int $bytes = NULL);
}
* @throws InvalidResourceException If there is being set
* an invalid file resource
*/
- function writeToFile ($dataStream);
+ function writeToFile (string $dataStream);
/**
* Writes at given position by seeking to it.
* @param $data Data to be written
* @return mixed Number of writes bytes or false on error
*/
- function writeAtPosition ($seedPosition, $data);
+ function writeAtPosition (int $seedPosition, string $data);
}