Swift_Mime_ContentEncoder_QpContentEncoder::encodeByteStream PHP Method

encodeByteStream() public method

QP encoded strings have a maximum line length of 76 characters. If the first line needs to be shorter, indicate the difference with $firstLineOffset.
public encodeByteStream ( Swift_OutputByteStream $os, Swift_InputByteStream $is, integer $firstLineOffset, integer $maxLineLength )
$os Swift_OutputByteStream output stream
$is Swift_InputByteStream input stream
$firstLineOffset integer
$maxLineLength integer
    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);
            $i = strpos($enc, '=0D=0A');
            $newLineLength = $lineLen + ($i === false ? $size : $i);
            if ($currentLine && $newLineLength >= $thisLineLength) {
                $is->write($prepend . $this->_standardize($currentLine));
                $currentLine = '';
                $prepend = "=\r\n";
                $thisLineLength = $maxLineLength;
                $lineLen = 0;
            }
            $currentLine .= $enc;
            if ($i === false) {
                $lineLen += $size;
            } else {
                // 6 is the length of '=0D=0A'.
                $lineLen = $size - strrpos($enc, '=0D=0A') - 6;
            }
        }
        if (strlen($currentLine)) {
            $is->write($prepend . $this->_standardize($currentLine));
        }
    }

Usage Example

 public function testEncodingAndDecodingSamples()
 {
     $sampleFp = opendir($this->_samplesDir);
     while (false !== ($encodingDir = readdir($sampleFp))) {
         if (substr($encodingDir, 0, 1) == '.') {
             continue;
         }
         $encoding = $encodingDir;
         $charStream = new Swift_CharacterStream_NgCharacterStream($this->_factory, $encoding);
         $encoder = new Swift_Mime_ContentEncoder_QpContentEncoder($charStream);
         $sampleDir = $this->_samplesDir . '/' . $encodingDir;
         if (is_dir($sampleDir)) {
             $fileFp = opendir($sampleDir);
             while (false !== ($sampleFile = readdir($fileFp))) {
                 if (substr($sampleFile, 0, 1) == '.') {
                     continue;
                 }
                 $text = file_get_contents($sampleDir . '/' . $sampleFile);
                 $os = new Swift_ByteStream_ArrayByteStream();
                 $os->write($text);
                 $is = new Swift_ByteStream_ArrayByteStream();
                 $encoder->encodeByteStream($os, $is);
                 $encoded = '';
                 while (false !== ($bytes = $is->read(8192))) {
                     $encoded .= $bytes;
                 }
                 $this->assertEquals(quoted_printable_decode($encoded), $text, '%s: Encoded string should decode back to original string for sample ' . $sampleDir . '/' . $sampleFile);
             }
             closedir($fileFp);
         }
     }
     closedir($sampleFp);
 }
All Usage Examples Of Swift_Mime_ContentEncoder_QpContentEncoder::encodeByteStream