This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
khosb/includes/kohana/modules/email/vendor/swift/classes/Swift/ByteStream/ArrayByteStream.php

198 lines
4.4 KiB
PHP
Raw Normal View History

2011-01-07 02:50:53 +00:00
<?php
/*
Bi-Directional ByteStream using an array in Swift Mailer.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//@require 'Swift/InputByteStream.php';
//@require 'Swift/OutputByteStream.php';
/**
* Allows reading and writing of bytes to and from an array.
* @package Swift
* @subpackage ByteStream
* @author Chris Corbyn
*/
class Swift_ByteStream_ArrayByteStream
implements Swift_InputByteStream, Swift_OutputByteStream
{
/**
* The internal stack of bytes.
* @var string[]
* @access private
*/
private $_array = array();
/**
* The size of the stack
* @var int
* @access private
*/
private $_arraySize = 0;
/**
* The internal pointer offset.
* @var int
* @access private
*/
private $_offset = 0;
/** Bound streams */
private $_mirrors = array();
/**
* 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))
{
$this->_array = $stack;
$this->_arraySize = count($stack);
}
elseif (is_string($stack))
{
$this->write($stack);
}
else
{
$this->_array = array();
}
}
/**
* 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)
{
if ($this->_offset == $this->_arraySize)
{
return false;
}
// Don't use array slice
$end = $length + $this->_offset;
$end = $this->_arraySize<$end
?$this->_arraySize
:$end;
$ret = '';
for (; $this->_offset < $end; ++$this->_offset)
{
$ret .= $this->_array[$this->_offset];
}
return $ret;
}
/**
* Writes $bytes to the end of the stream.
* @param string $bytes
*/
public function write($bytes)
{
$to_add = str_split($bytes);
array_unshift($to_add, &$this->_array);
$this->_arraySize = call_user_func_array('array_push', $to_add);
foreach ($this->_mirrors as $stream)
{
$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)
{
$this->_mirrors[] = $is;
}
/**
* 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)
{
foreach ($this->_mirrors as $k => $stream)
{
if ($is === $stream)
{
unset($this->_mirrors[$k]);
}
}
}
/**
* Move the internal read pointer to $byteOffset in the stream.
* @param int $byteOffset
* @return boolean
*/
public function setReadPointer($byteOffset)
{
if ($byteOffset > $this->_arraySize)
{
$byteOffset = $this->_arraySize;
}
elseif ($byteOffset < 0)
{
$byteOffset = 0;
}
$this->_offset = $byteOffset;
}
/**
* Flush the contents of the stream (empty it) and set the internal pointer
* to the beginning.
*/
public function flushBuffers()
{
$this->_offset = 0;
$this->_array = array();
$this->_arraySize = 0;
foreach ($this->_mirrors as $stream)
{
$stream->flushBuffers();
}
}
}