Exemple #1
0
 /**
  * Closes the file associated with this recorder.
  * Used by Recorder.
  */
 public function closeFile()
 {
     if ($this->out != null) {
         $this->out->close();
         $this->out = null;
     }
 }
 /**
  * Constructor
  *
  * @param   io.streams.OutputStream out
  * @param   int level default 6
  * @throws  lang.IllegalArgumentException if the level is not between 0 and 9
  */
 public function __construct(OutputStream $out, $level = 6)
 {
     if ($level < 0 || $level > 9) {
         throw new \lang\IllegalArgumentException('Level ' . $level . ' out of range [0..9]');
     }
     // Write GZIP format header:
     // * ID1, ID2 (Identification, \x1F, \x8B)
     // * CM       (Compression Method, 8 = deflate)
     // * FLG      (Flags, use 0)
     // * MTIME    (Modification time, Un*x timestamp)
     // * XFL      (Extra flags, 2 = compressor used maximum compression)
     // * OS       (Operating system, 255 = unknown)
     $out->write(pack('CCCCVCC', 0x1f, 0x8b, 8, 0, time(), 2, 255));
     // Now, convert stream to file handle and append deflating filter
     $this->out = Streams::writeableFd($out);
     if (!($this->filter = stream_filter_append($this->out, 'zlib.deflate', STREAM_FILTER_WRITE, $level))) {
         fclose($this->out);
         $this->out = null;
         throw new \io\IOException('Could not append stream filter');
     }
     $this->md = hash_init('crc32b');
 }
 /**
  * Construct a new FileOutputStream.
  * @param mixed $file
  * @param boolean $append Whether to append bytes to end of file rather than beginning.
  * @throws Exception - if invalid argument specified.
  * @throws IOException - if unable to open file.
  */
 public function __construct($file, $append = false) {
 	if ($file instanceof PhingFile) {
         $this->file = $file;
     } elseif (is_string($file)) {
         $this->file = new PhingFile($file);
     } else {
         throw new Exception("Invalid argument type for \$file.");
     }
     if ($append) {
     	$stream = @fopen($this->file->getAbsolutePath(), "ab");
     } else {
     	$stream = @fopen($this->file->getAbsolutePath(), "wb");
     }
     if ($stream === false) {
     	throw new IOException("Unable to open " . $this->file->__toString() . " for writing: " . $php_errormsg);
     }
     parent::__construct($stream);
 }
Exemple #4
0
 /**
  * Closes this output stream and releases any system resources associated with the stream. 
  * The close method of FilterOutputStream calls its flush method, and then calls the 
  * close method of its underlying output stream. 
  *
  * @overrides close in class OutputStream
  * @throws IOException If an I/O error occurs.
  * @see flush(), out
  */
 public function close()
 {
     $this->flush();
     parent::close();
 }
 /**
  * Prints a message to console.
  * 
  * @param string $message  The message to print. 
  *                 Should not be <code>null</code>.
  * @param resource $stream The stream to use for message printing.
  * @param int $priority The priority of the message. 
  *                 (Ignored in this implementation.)
  * @return void
  */
 protected function printMessage($message, OutputStream $stream, $priority)
 {
     $stream->write($message . PHP_EOL);
 }
 /**
  * Prints a message to console.
  * 
  * @param string $message  The message to print. 
  *                 Should not be <code>null</code>.
  * @param resource $stream The stream to use for message printing.
  * @param int $priority The priority of the message. 
  *                 (Ignored in this implementation.)
  * @return void
  */
 protected function printMessage($message, OutputStream $stream, $priority)
 {
     $stream->write($message . $this->lSep);
 }
 /**
  * @see DefaultLogger#printMessage
  * @param string $message
  * @param OutputStream $stream
  * @param int $priority
  */
 protected final function printMessage($message, OutputStream $stream, $priority)
 {
     if ($message !== null) {
         if (!$this->colorsSet) {
             $this->setColors();
             $this->colorsSet = true;
         }
         switch ($priority) {
             case Project::MSG_ERR:
                 $message = $this->errColor . $message . self::END_COLOR;
                 break;
             case Project::MSG_WARN:
                 $message = $this->warnColor . $message . self::END_COLOR;
                 break;
             case Project::MSG_INFO:
                 $message = $this->infoColor . $message . self::END_COLOR;
                 break;
             case Project::MSG_VERBOSE:
                 $message = $this->verboseColor . $message . self::END_COLOR;
                 break;
             case Project::MSG_DEBUG:
                 $message = $this->debugColor . $message . self::END_COLOR;
                 break;
         }
         $stream->write($message . PHP_EOL);
     }
 }
 /**
  * Gets a string representation of attached stream resource.
  *
  * @return string String representation of output stream
  */
 public function getResource()
 {
     return $this->outStream->__toString();
 }
 /**
  * Output the properties as xml output.
  *
  * @param Properties   $props the properties to save
  * @param OutputStream $os    the output stream to write to (Note this gets closed)
  *
  * @throws BuildException
  */
 protected function xmlSaveProperties(Properties $props, OutputStream $os)
 {
     $doc = new DOMDocument('1.0', 'UTF-8');
     $doc->formatOutput = true;
     $rootElement = $doc->createElement(self::$PROPERTIES);
     $properties = $props->getProperties();
     ksort($properties);
     foreach ($properties as $key => $value) {
         $propElement = $doc->createElement(self::$PROPERTY);
         $propElement->setAttribute(self::$ATTR_NAME, $key);
         $propElement->setAttribute(self::$ATTR_VALUE, $value);
         $rootElement->appendChild($propElement);
     }
     try {
         $doc->appendChild($rootElement);
         $os->write($doc->saveXML());
     } catch (IOException $ioe) {
         throw new BuildException("Unable to write XML file", $ioe);
     }
 }
 /**
  * @see DefaultLogger#printMessage
  * @param string $message
  * @param OutputStream $stream
  * @param int $priority
  */
 protected final function printMessage($message, OutputStream $stream, $priority)
 {
     if ($message !== null) {
         if (!$this->colorsSet) {
             $this->setColors();
             $this->colorsSet = true;
         }
         $search = array('<', '>');
         $replace = array('&lt;', '&gt;');
         $message = str_replace($search, $replace, $message);
         $search = array("\t", "\n", "\r");
         $replace = array('&nbsp;&nbsp;&nbsp;', '<br>', '');
         $message = str_replace($search, $replace, $message);
         if (preg_match('@^( +)([^ ].+)@', $message, $matches)) {
             $len = strlen($matches[1]);
             $space = '&nbsp;';
             for ($i = 1; $i < $len; $i++) {
                 $space .= '&nbsp;';
             }
             $message = $space . $matches[2];
         }
         switch ($priority) {
             case Project::MSG_ERR:
                 $message = $this->errColor . $message . self::END_COLOR;
                 break;
             case Project::MSG_WARN:
                 $message = $this->warnColor . $message . self::END_COLOR;
                 break;
             case Project::MSG_INFO:
                 $message = $this->infoColor . $message . self::END_COLOR;
                 break;
             case Project::MSG_VERBOSE:
                 $message = $this->verboseColor . $message . self::END_COLOR;
                 break;
             case Project::MSG_DEBUG:
                 $message = $this->debugColor . $message . self::END_COLOR;
                 break;
         }
         $stream->write($message . '<br>');
     }
 }
 /**
  * Serialize and write to output
  *
  * @param  io.streams.OutputStream out
  * @param  webservices.rest.Payload value
  */
 public function write(OutputStream $out, Payload $value = NULL)
 {
     $out->write($this->serializer->serialize($value));
 }
 /**
  * Open an output stream for writing
  *
  * @param   io.streams.OutputStream s
  * @return  resource
  */
 public static function writeableUri(OutputStream $s)
 {
     self::$streams[$s->hashCode()] = $s;
     return 'iostrw://' . $s->hashCode();
 }
 /**
  * Encode PHP data into JSON via stream
  *
  * Gets the PHP data and a stream.<br/>
  * It converts the data to JSON and will put every atom into the stream as soon
  * as it is available.<br/>
  * The usage is similar to encode() except the second argument.
  *
  * @param   var data
  * @param   io.streams.OutputStream stream
  * @return  boolean
  * @throws  webservices.json.JsonException if the data could not be serialized
  */
 public function encodeTo($data, OutputStream $stream)
 {
     switch (gettype($data)) {
         case 'string':
             $stream->write('"' . $this->escape(iconv(xp::ENCODING, 'utf-8', $data)) . '"');
             return TRUE;
         case 'integer':
             $stream->write(strval($data));
             return TRUE;
         case 'double':
             $stream->write(strval($data));
             return TRUE;
         case 'boolean':
             $stream->write($data ? 'true' : 'false');
             return TRUE;
         case 'NULL':
             $stream->write('null');
             return TRUE;
         case 'array':
             if ($this->_isVector($data)) {
                 // Bail out early on bordercase
                 if (0 == sizeof($data)) {
                     $stream->write('[ ]');
                     return TRUE;
                 }
                 $stream->write('[ ');
                 // Get first element
                 $stream->write($this->encode(array_shift($data)));
                 foreach ($data as $value) {
                     $stream->write(' , ' . $this->encode($value));
                 }
                 $stream->write(' ]');
                 return TRUE;
             } else {
                 // Reset array internal pointer
                 reset($data);
                 $stream->write('{ ');
                 $value = each($data);
                 $stream->write($this->encode((string) $value['key']) . ' : ' . $this->encode($value['value']));
                 while ($value = each($data)) {
                     $stream->write(' , ' . $this->encode((string) $value['key']) . ' : ' . $this->encode($value['value']));
                 }
                 $stream->write(' }');
                 return TRUE;
             }
         case 'object':
             // Converts a string object into an normal json string
             if ($data instanceof String) {
                 $stream->write('"' . $this->escape((string) $data->getBytes('utf-8')) . '"');
                 break;
             }
             if ($data instanceof Generic) {
                 if (!method_exists($data, '__sleep')) {
                     $vars = get_object_vars($data);
                 } else {
                     $vars = array();
                     foreach ($data->__sleep() as $var) {
                         $vars[$var] = $data->{$var};
                     }
                 }
                 $this->encodeTo($vars, $stream);
                 return TRUE;
             }
             // Break missing intentially
         default:
             throw new JsonException('Cannot encode data of type ' . gettype($data));
     }
 }
 /**
  * Store to an output stream, e.g. a file
  *
  * @param   io.streams.OutputStream out
  * @throws  io.IOException
  */
 public function store(OutputStream $out)
 {
     foreach (array_keys($this->_data) as $section) {
         $out->write(sprintf("[%s]\n", $section));
         foreach ($this->_data[$section] as $key => $val) {
             if (';' == $key[0]) {
                 $out->write(sprintf("\n; %s\n", $val));
             } else {
                 if ($val instanceof Hashmap) {
                     $str = '';
                     foreach ($val->keys() as $k) {
                         $str .= '|' . $k . ':' . $val->get($k);
                     }
                     $val = (string) substr($str, 1);
                 }
                 if (is_array($val)) {
                     $val = implode('|', $val);
                 }
                 if (is_string($val)) {
                     $val = '"' . $val . '"';
                 }
                 $out->write(sprintf("%s=%s\n", $key, strval($val)));
             }
         }
         $out->write("\n");
     }
 }
Exemple #15
0
 /**
  * Write markup
  *
  * @param   io.streams.OutputStream
  * @param   string input
  */
 protected function writeMarkup($input, OutputStream $stream)
 {
     $stream->write('<div class="markup"><p>');
     $stream->write(strtr($this->processor->markupFor($input), array('<code>' => '<pre class="code">', '</code>' => '</pre>', '<h1>' => '<h2>', '</h1>' => '</h2>', '<h2>' => '<h3>', '</h2>' => '</h3>', '<h3>' => '<h4>', '</h3>' => '</h4>')));
     $stream->write('</p></div>');
 }
 /**
  * Store to an output stream, e.g. a file
  *
  * @param   io.streams.OutputStream out
  * @throws  io.IOException
  */
 public function store(OutputStream $out)
 {
     foreach (array_keys($this->_data) as $section) {
         $out->write('[' . $section . "]\n");
         foreach ($this->_data[$section] as $key => $val) {
             if (';' == $key[0]) {
                 $out->write("\n; " . $val . "\n");
             } else {
                 if (is_array($val)) {
                     if (empty($val)) {
                         $out->write($key . "=\n");
                     } else {
                         if (0 === key($val)) {
                             foreach ($val as $v) {
                                 $out->write($key . '[]=' . $this->quote($v) . "\n");
                             }
                         } else {
                             foreach ($val as $k => $v) {
                                 $out->write($key . '[' . $k . ']=' . $this->quote($v) . "\n");
                             }
                         }
                     }
                 } else {
                     $out->write($key . '=' . $this->quote($val) . "\n");
                 }
             }
         }
         $out->write("\n");
     }
 }