dolibarr/htdocs/includes/swiftmailer/lib/classes/Swift/ByteStream/ArrayByteStream.php

183 lines
4.2 KiB
PHP
Raw Normal View History

2016-04-16 18:15:03 +02:00
<?php
/*
* This file is part of SwiftMailer.
* (c) 2004-2009 Chris Corbyn
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Allows reading and writing of bytes to and from an array.
*
2018-01-21 15:55:56 +01:00
* @author Chris Corbyn
2016-04-16 18:15:03 +02:00
*/
class Swift_ByteStream_ArrayByteStream implements Swift_InputByteStream, Swift_OutputByteStream
{
/**
* The internal stack of bytes.
*
* @var string[]
*/
2018-01-21 15:55:56 +01:00
private $array = array();
2016-04-16 18:15:03 +02:00
/**
* The size of the stack.
*
* @var int
*/
2018-01-21 15:55:56 +01:00
private $arraySize = 0;
2016-04-16 18:15:03 +02:00
/**
* The internal pointer offset.
*
* @var int
*/
2018-01-21 15:55:56 +01:00
private $offset = 0;
2016-04-16 18:15:03 +02:00
/**
* Bound streams.
*
* @var Swift_InputByteStream[]
*/
2018-01-21 15:55:56 +01:00
private $mirrors = array();
2016-04-16 18:15:03 +02:00
/**
* Create a new ArrayByteStream.
*
* If $stack is given the stream will be populated with the bytes it contains.
*
* @param mixed $stack of bytes in string or array form, optional
*/
public function __construct($stack = null)
{
if (is_array($stack)) {
2018-01-21 15:55:56 +01:00
$this->array = $stack;
$this->arraySize = count($stack);
2016-04-16 18:15:03 +02:00
} elseif (is_string($stack)) {
$this->write($stack);
} else {
2018-01-21 15:55:56 +01:00
$this->array = array();
2016-04-16 18:15:03 +02:00
}
}
/**
* Reads $length bytes from the stream into a string and moves the pointer
* through the stream by $length.
*
* If less bytes exist than are requested the
* remaining bytes are given instead. If no bytes are remaining at all, boolean
* false is returned.
*
* @param int $length
*
* @return string
*/
public function read($length)
{
2018-01-21 15:55:56 +01:00
if ($this->offset == $this->arraySize) {
2016-04-16 18:15:03 +02:00
return false;
}
// Don't use array slice
2018-01-21 15:55:56 +01:00
$end = $length + $this->offset;
$end = $this->arraySize < $end ? $this->arraySize : $end;
2016-04-16 18:15:03 +02:00
$ret = '';
2018-01-21 15:55:56 +01:00
for (; $this->offset < $end; ++$this->offset) {
$ret .= $this->array[$this->offset];
2016-04-16 18:15:03 +02:00
}
return $ret;
}
/**
* Writes $bytes to the end of the stream.
*
* @param string $bytes
*/
public function write($bytes)
{
$to_add = str_split($bytes);
foreach ($to_add as $value) {
2018-01-21 15:55:56 +01:00
$this->array[] = $value;
2016-04-16 18:15:03 +02:00
}
2018-01-21 15:55:56 +01:00
$this->arraySize = count($this->array);
2016-04-16 18:15:03 +02:00
2018-01-21 15:55:56 +01:00
foreach ($this->mirrors as $stream) {
2016-04-16 18:15:03 +02:00
$stream->write($bytes);
}
}
/**
* Not used.
*/
public function commit()
{
}
/**
* Attach $is to this stream.
*
* The stream acts as an observer, receiving all data that is written.
* All {@link write()} and {@link flushBuffers()} operations will be mirrored.
*
* @param Swift_InputByteStream $is
*/
public function bind(Swift_InputByteStream $is)
{
2018-01-21 15:55:56 +01:00
$this->mirrors[] = $is;
2016-04-16 18:15:03 +02:00
}
/**
* Remove an already bound stream.
*
* If $is is not bound, no errors will be raised.
* If the stream currently has any buffered data it will be written to $is
* before unbinding occurs.
*
* @param Swift_InputByteStream $is
*/
public function unbind(Swift_InputByteStream $is)
{
2018-01-21 15:55:56 +01:00
foreach ($this->mirrors as $k => $stream) {
2016-04-16 18:15:03 +02:00
if ($is === $stream) {
2018-01-21 15:55:56 +01:00
unset($this->mirrors[$k]);
2016-04-16 18:15:03 +02:00
}
}
}
/**
* Move the internal read pointer to $byteOffset in the stream.
*
* @param int $byteOffset
*
* @return bool
*/
public function setReadPointer($byteOffset)
{
2018-01-21 15:55:56 +01:00
if ($byteOffset > $this->arraySize) {
$byteOffset = $this->arraySize;
2016-04-16 18:15:03 +02:00
} elseif ($byteOffset < 0) {
$byteOffset = 0;
}
2018-01-21 15:55:56 +01:00
$this->offset = $byteOffset;
2016-04-16 18:15:03 +02:00
}
/**
* Flush the contents of the stream (empty it) and set the internal pointer
* to the beginning.
*/
public function flushBuffers()
{
2018-01-21 15:55:56 +01:00
$this->offset = 0;
$this->array = array();
$this->arraySize = 0;
2016-04-16 18:15:03 +02:00
2018-01-21 15:55:56 +01:00
foreach ($this->mirrors as $stream) {
2016-04-16 18:15:03 +02:00
$stream->flushBuffers();
}
}
}