public function testMultipleStreamsAreReadLikeAConcatenatedString() { $cache1 = new Swift_Cache_Memory(); $cache1->write("foo", "abc"); $os1 = $cache1->getOutputStream("foo"); $cache2 = new Swift_Cache_Memory(); $cache2->write("bar", "def"); $os2 = $cache2->getOutputStream("bar"); $cache3 = new Swift_Cache_Memory(); $cache3->write("zip", "123456789"); $os3 = $cache3->getOutputStream("zip"); $joint_os = new Swift_Cache_JointOutputStream(array($os1, $os2, $os3)); $ret = ""; while (false !== ($bytes = $joint_os->read())) { $ret .= $bytes; } $this->assertEqual("abcdef123456789", $ret); }
/** * Compile the entire MIME document into a string * The returned string may be used in other documents if needed. * @return Swift_Cache_OutputStream */ public function build() { $this->preBuild(); $data = $this->buildData(); $joint_os = new Swift_Cache_JointOutputStream(); $this->cache->clear("headers"); $this->cache->write("headers", $this->headers->build()); $joint_os->addStream($this->cache->getOutputStream("headers")); $this->cache->clear("dbl_le"); $this->cache->write("dbl_le", str_repeat($this->LE, 2)); $joint_os->addStream($this->cache->getOutputStream("dbl_le")); $joint_os->addStream($data); return $joint_os; //return $this->headers->build() . str_repeat($this->LE, 2) . $data; }
/** * Get the data in the format suitable for sending * @return Swift_Cache_OutputStream * @throws Swift_FileException If the file stream given cannot be read * @throws Swift_Message_MimeException If some required headers have been forcefully removed */ function &buildData() { Swift_ClassLoader::load("Swift_Cache_JointOutputStream"); $encoder =& $this->_encoder; if (!empty($this->children)) { if ($this->boundary === null) { $this->boundary = Swift_Message_Mime::generateBoundary(); } $this->headers->setAttribute("Content-Type", "boundary", $this->boundary); $this->cache->clear("append"); foreach ($this->children as $id => $part) { $this->cache->write("append", $this->LE . "--" . $this->boundary . $this->LE); $part_stream = $this->children[$id]->build(); while (false !== ($bytes = $part_stream->read())) { $this->cache->write("append", $bytes); } } $this->cache->write("append", $this->LE . "--" . $this->boundary . "--" . $this->LE); } $i = array(); $joint_os = new Swift_Cache_JointOutputStream($i); //Try using a cached version to save some cycles (at the expense of memory) if ($this->cache->has("body")) { $body =& $this->cache->getOutputStream("body"); $joint_os->addStream($body); $append =& $this->cache->getOutputStream("append"); $joint_os->addStream($append); return $joint_os; } $data =& $this->getData(); $is_file = is_a($this->getData(), "Swift_File"); switch ($this->getEncoding()) { case "quoted-printable": if ($is_file) { $qp_os = $encoder->QPEncodeFile($this->getData(), 76, $this->LE); while (false !== ($bytes = $qp_os->read())) { $this->cache->write("body", $bytes); } } else { $this->cache->write("body", $encoder->QPEncode($this->getData(), 76, 0, false, $this->LE)); } break; case "base64": if ($is_file) { $b64_os = $encoder->base64EncodeFile($this->getData(), 76, $this->LE); while (false !== ($bytes = $b64_os->read())) { $this->cache->write("body", $bytes); } } else { $this->cache->write("body", $encoder->base64Encode($this->getData(), 76, 0, false, $this->LE)); } break; case "binary": if ($is_file) { $data = $this->getData(); while (false !== ($bytes = $data->read(8192))) { $this->cache->write("body", $bytes); } } else { $this->cache->write("body", $this->getData()); } break; case "7bit": if ($is_file) { $os = $encoder->encode7BitFile($this->getData(), $this->wrap, $this->LE); while (false !== ($bytes = $os->read())) { $this->cache->write("body", $bytes); } } else { $this->cache->write("body", $encoder->encode7Bit($this->getData(), $this->wrap, $this->LE)); } break; case "8bit": default: if ($is_file) { $os = $encoder->encode8BitFile($this->getData(), $this->wrap, $this->LE); while (false !== ($bytes = $os->read())) { $this->cache->write("body", $bytes); } } else { $this->cache->write("body", $encoder->encode8Bit($this->getData(), $this->wrap, $this->LE)); } break; } $body =& $this->cache->getOutputStream("body"); $joint_os->addStream($body); $append =& $this->cache->getOutputStream("append"); $joint_os->addStream($append); return $joint_os; }