Classes implementing this interface may use a subsystem which requires less memory than working with large strings of data.
Author: Chris Corbyn
 /**
  * Encode stream $in to stream $out.
  *
  * @param Swift_OutputByteStream $os
  * @param Swift_InputByteStream  $is
  * @param int                    $firstLineOffset
  * @param int                    $maxLineLength,  optional, 0 indicates the default of 76 bytes
  */
 public function encodeByteStream(Swift_OutputByteStream $os, Swift_InputByteStream $is, $firstLineOffset = 0, $maxLineLength = 0)
 {
     if (0 >= $maxLineLength || 76 < $maxLineLength) {
         $maxLineLength = 76;
     }
     $remainder = 0;
     while (false !== ($bytes = $os->read(8190))) {
         $encoded = base64_encode($bytes);
         $encodedTransformed = '';
         $thisMaxLineLength = $maxLineLength - $remainder - $firstLineOffset;
         while ($thisMaxLineLength < strlen($encoded)) {
             $encodedTransformed .= substr($encoded, 0, $thisMaxLineLength) . "\r\n";
             $firstLineOffset = 0;
             $encoded = substr($encoded, $thisMaxLineLength);
             $thisMaxLineLength = $maxLineLength;
             $remainder = 0;
         }
         if (0 < ($remainingLength = strlen($encoded))) {
             $remainder += $remainingLength;
             $encodedTransformed .= $encoded;
             $encoded = null;
         }
         $is->write($encodedTransformed);
     }
 }
 /**
  * Encode $in to $out.
  *
  * @param Swift_OutputByteStream $os              to read from
  * @param Swift_InputByteStream  $is              to write to
  * @param integer                $firstLineOffset
  * @param integer                $maxLineLength   0 indicates the default length for this encoding
  */
 public function encodeByteStream(Swift_OutputByteStream $os, Swift_InputByteStream $is, $firstLineOffset = 0, $maxLineLength = 0)
 {
     $string = '';
     while (false !== ($bytes = $os->read(8192))) {
         $string .= $bytes;
     }
     $is->write($this->encodeString($string));
 }
 /**
  * Encode stream $in to stream $out.
  *
  * @param Swift_OutputByteStream $os
  * @param Swift_InputByteStream  $is
  * @param int                    $firstLineOffset
  * @param int                    $maxLineLength,  optional, 0 indicates the default of 76 bytes
  */
 public function encodeByteStream(Swift_OutputByteStream $os, Swift_InputByteStream $is, $firstLineOffset = 0, $maxLineLength = 0)
 {
     if (0 >= $maxLineLength || 76 < $maxLineLength) {
         $maxLineLength = 76;
     }
     $remainder = 0;
     $base64ReadBufferRemainderBytes = null;
     // To reduce memory usage, the output buffer is streamed to the input buffer like so:
     //   Output Stream => base64encode => wrap line length => Input Stream
     // HOWEVER it's important to note that base64_encode() should only be passed whole triplets of data (except for the final chunk of data)
     // otherwise it will assume the input data has *ended* and it will incorrectly pad/terminate the base64 data mid-stream.
     // We use $base64ReadBufferRemainderBytes to carry over 1-2 "remainder" bytes from the each chunk from OutputStream and pre-pend those onto the
     // chunk of bytes read in the next iteration.
     // When the OutputStream is empty, we must flush any remainder bytes.
     while (true) {
         $readBytes = $os->read(8192);
         $atEOF = $readBytes === false;
         if ($atEOF) {
             $streamTheseBytes = $base64ReadBufferRemainderBytes;
         } else {
             $streamTheseBytes = $base64ReadBufferRemainderBytes . $readBytes;
         }
         $base64ReadBufferRemainderBytes = null;
         $bytesLength = strlen($streamTheseBytes);
         if ($bytesLength === 0) {
             // no data left to encode
             break;
         }
         // if we're not on the last block of the ouput stream, make sure $streamTheseBytes ends with a complete triplet of data
         // and carry over remainder 1-2 bytes to the next loop iteration
         if (!$atEOF) {
             $excessBytes = $bytesLength % 3;
             if ($excessBytes !== 0) {
                 $base64ReadBufferRemainderBytes = substr($streamTheseBytes, -$excessBytes);
                 $streamTheseBytes = substr($streamTheseBytes, 0, $bytesLength - $excessBytes);
             }
         }
         $encoded = base64_encode($streamTheseBytes);
         $encodedTransformed = '';
         $thisMaxLineLength = $maxLineLength - $remainder - $firstLineOffset;
         while ($thisMaxLineLength < strlen($encoded)) {
             $encodedTransformed .= substr($encoded, 0, $thisMaxLineLength) . "\r\n";
             $firstLineOffset = 0;
             $encoded = substr($encoded, $thisMaxLineLength);
             $thisMaxLineLength = $maxLineLength;
             $remainder = 0;
         }
         if (0 < ($remainingLength = strlen($encoded))) {
             $remainder += $remainingLength;
             $encodedTransformed .= $encoded;
             $encoded = null;
         }
         $is->write($encodedTransformed);
         if ($atEOF) {
             break;
         }
     }
 }
 /**
  * Writes $bytes to the end of the stream.
  *
  * @param string                $bytes
  * @param Swift_InputByteStream $is    optional
  */
 public function write($bytes, Swift_InputByteStream $is = null)
 {
     $this->_keyCache->setString($this->_nsKey, $this->_itemKey, $bytes, Swift_KeyCache::MODE_APPEND);
     if (isset($is)) {
         $is->write($bytes);
     }
     if (isset($this->_writeThrough)) {
         $this->_writeThrough->write($bytes);
     }
 }
 /**
  * Encode $in to $out.
  *
  * @param Swift_OutputByteStream $os              to read from
  * @param Swift_InputByteStream  $is              to write to
  * @param int                    $firstLineOffset
  * @param int                    $maxLineLength   0 indicates the default length for this encoding
  *
  * @throws RuntimeException
  */
 public function encodeByteStream(Swift_OutputByteStream $os, Swift_InputByteStream $is, $firstLineOffset = 0, $maxLineLength = 0)
 {
     if ($this->charset !== 'utf-8') {
         throw new RuntimeException(sprintf('Charset "%s" not supported. NativeQpContentEncoder only supports "utf-8"', $this->charset));
     }
     $string = '';
     while (false !== ($bytes = $os->read(8192))) {
         $string .= $bytes;
     }
     $is->write($this->encodeString($string));
 }
 /**
  * Encode stream $in to stream $out.
  *
  * @param Swift_OutputByteStream $os
  * @param Swift_InputByteStream  $is
  * @param int                    $firstLineOffset ignored
  * @param int                    $maxLineLength   optional, 0 means no wrapping will occur
  */
 public function encodeByteStream(Swift_OutputByteStream $os, Swift_InputByteStream $is, $firstLineOffset = 0, $maxLineLength = 0)
 {
     $leftOver = '';
     while (false !== ($bytes = $os->read(8192))) {
         $toencode = $leftOver . $bytes;
         if ($this->_canonical) {
             $toencode = $this->_canonicalize($toencode);
         }
         $wrapped = $this->_safeWordWrap($toencode, $maxLineLength, "\r\n");
         $lastLinePos = strrpos($wrapped, "\r\n");
         $leftOver = substr($wrapped, $lastLinePos);
         $wrapped = substr($wrapped, 0, $lastLinePos);
         $is->write($wrapped);
     }
     if (strlen($leftOver)) {
         $is->write($leftOver);
     }
 }
Example #7
0
 /**
  * Encode stream $in to stream $out.
  * QP encoded strings have a maximum line length of 76 characters.
  * If the first line needs to be shorter, indicate the difference with
  * $firstLineOffset.
  * @param Swift_OutputByteStream $os output stream
  * @param Swift_InputByteStream $is input stream
  * @param int $firstLineOffset
  * @param int $maxLineLength
  */
 public function encodeByteStream(Swift_OutputByteStream $os, Swift_InputByteStream $is, $firstLineOffset = 0, $maxLineLength = 0)
 {
     if ($maxLineLength > 76 || $maxLineLength <= 0) {
         $maxLineLength = 76;
     }
     $thisLineLength = $maxLineLength - $firstLineOffset;
     $this->_charStream->flushContents();
     $this->_charStream->importByteStream($os);
     $currentLine = '';
     $prepend = '';
     $size = $lineLen = 0;
     while (false !== ($bytes = $this->_nextSequence())) {
         //If we're filtering the input
         if (isset($this->_filter)) {
             //If we can't filter because we need more bytes
             while ($this->_filter->shouldBuffer($bytes)) {
                 //Then collect bytes into the buffer
                 if (false === ($moreBytes = $this->_nextSequence(1))) {
                     break;
                 }
                 foreach ($moreBytes as $b) {
                     $bytes[] = $b;
                 }
             }
             //And filter them
             $bytes = $this->_filter->filter($bytes);
         }
         $enc = $this->_encodeByteSequence($bytes, $size);
         if ($currentLine && $lineLen + $size >= $thisLineLength) {
             $is->write($prepend . $this->_standardize($currentLine));
             $currentLine = '';
             $prepend = "=\r\n";
             $thisLineLength = $maxLineLength;
             $lineLen = 0;
         }
         $lineLen += $size;
         $currentLine .= $enc;
     }
     if (strlen($currentLine)) {
         $is->write($prepend . $this->_standardize($currentLine));
     }
 }
 /**
  * Encode stream $in to stream $out.
  *
  * @param Swift_OutputByteStream $in
  * @param Swift_InputByteStream  $out
  * @param int                    $firstLineOffset ignored
  * @param int                    $maxLineLength   ignored
  */
 public function encodeByteStream(Swift_OutputByteStream $os, Swift_InputByteStream $is, $firstLineOffset = 0, $maxLineLength = 0)
 {
     while (false !== ($bytes = $os->read(8192))) {
         $is->write($bytes);
     }
 }
Example #9
0
 /**
  * Get data back out of the cache as a ByteStream.
  *
  * @param string                $nsKey
  * @param string                $itemKey
  * @param Swift_InputByteStream $is      to write the data to
  */
 public function exportToByteStream($nsKey, $itemKey, Swift_InputByteStream $is)
 {
     if ($this->hasKey($nsKey, $itemKey)) {
         $fp = $this->_getHandle($nsKey, $itemKey, self::POSITION_START);
         if ($this->_quotes) {
             ini_set('magic_quotes_runtime', 0);
         }
         while (!feof($fp) && false !== ($bytes = fread($fp, 8192))) {
             $is->write($bytes);
         }
         if ($this->_quotes) {
             ini_set('magic_quotes_runtime', 1);
         }
         $this->_freeHandle($nsKey, $itemKey);
     }
 }
Example #10
0
 /**
  * Get data back out of the cache as a ByteStream.
  *
  * @param string                $nsKey
  * @param string                $itemKey
  * @param Swift_InputByteStream $is      to write the data to
  */
 public function exportToByteStream($nsKey, $itemKey, Swift_InputByteStream $is)
 {
     $this->_prepareCache($nsKey);
     $is->write($this->getString($nsKey, $itemKey));
 }
 /**
  * Write this entire entity to a {@see Swift_InputByteStream}.
  *
  * @param Swift_InputByteStream
  */
 public function toByteStream(Swift_InputByteStream $is)
 {
     $is->write($this->_headers->toString());
     $is->commit();
     $this->_bodyToByteStream($is);
 }
 /**
  * @param Swift_OutputByteStream $fromStream
  * @param Swift_InputByteStream  $toStream
  */
 protected function copyFromOpenSSLOutput(Swift_OutputByteStream $fromStream, Swift_InputByteStream $toStream)
 {
     $bufferLength = 4096;
     $filteredStream = new Swift_ByteStream_TemporaryFileByteStream();
     $filteredStream->addFilter($this->replacementFactory->createFilter("\r\n", "\n"), 'CRLF to LF');
     $filteredStream->addFilter($this->replacementFactory->createFilter("\n", "\r\n"), 'LF to CRLF');
     while (false !== ($buffer = $fromStream->read($bufferLength))) {
         $filteredStream->write($buffer);
     }
     $filteredStream->flushBuffers();
     while (false !== ($buffer = $filteredStream->read($bufferLength))) {
         $toStream->write($buffer);
     }
     $toStream->commit();
 }
Example #13
0
 /**
  * Write this entire entity to a {@see Swift_InputByteStream}.
  *
  * @param Swift_InputByteStream
  */
 public function toByteStream(Swift_InputByteStream $is)
 {
     $is->write($this->_headers->toString());
     $is->commit();
     if (empty($this->_immediateChildren)) {
         if (isset($this->_body)) {
             if ($this->_cache->hasKey($this->_cacheKey, 'body')) {
                 $this->_cache->exportToByteStream($this->_cacheKey, 'body', $is);
             } else {
                 $cacheIs = $this->_cache->getInputByteStream($this->_cacheKey, 'body');
                 if ($cacheIs) {
                     $is->bind($cacheIs);
                 }
                 $is->write("\r\n");
                 if ($this->_body instanceof Swift_OutputByteStream) {
                     $this->_body->setReadPointer(0);
                     $this->_encoder->encodeByteStream($this->_body, $is, 0, $this->getMaxLineLength());
                 } else {
                     $is->write($this->_encoder->encodeString($this->getBody(), 0, $this->getMaxLineLength()));
                 }
                 if ($cacheIs) {
                     $is->unbind($cacheIs);
                 }
             }
         }
     }
     if (!empty($this->_immediateChildren)) {
         foreach ($this->_immediateChildren as $child) {
             $is->write("\r\n\r\n--" . $this->getBoundary() . "\r\n");
             $child->toByteStream($is);
         }
         $is->write("\r\n\r\n--" . $this->getBoundary() . "--\r\n");
     }
 }