PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/gulliver/thirdparty/phing/system/io/BufferedReader.php

https://bitbucket.org/ferOnti/processmaker
PHP | 170 lines | 80 code | 25 blank | 65 comment | 13 complexity | abc439ef8a146dcc22d652597a77688f MD5 | raw file
  1. <?php
  2. /*
  3. * $Id: BufferedReader.php 3076 2006-12-18 08:52:12Z fabien $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information please see
  19. * <http://phing.info>.
  20. */
  21. include_once 'phing/system/io/Reader.php';
  22. /*
  23. * Convenience class for reading files.
  24. *
  25. * @author <a href="mailto:yl@seasonfive.com">Yannick Lecaillez</a>
  26. * @version $Revision: 1.6 $ $Date: 2005/12/27 19:12:13 $
  27. * @access public
  28. * @see FilterReader
  29. * @package phing.system.io
  30. */
  31. class BufferedReader extends Reader {
  32. private $bufferSize = 0;
  33. private $buffer = null;
  34. private $bufferPos = 0;
  35. /**
  36. * The Reader we are buffering for.
  37. */
  38. private $in;
  39. /**
  40. *
  41. * @param object $reader The reader (e.g. FileReader).
  42. * @param integer $buffsize The size of the buffer we should use for reading files.
  43. * A large buffer ensures that most files (all scripts?) are parsed in 1 buffer.
  44. */
  45. function __construct(Reader $reader, $buffsize = 65536) {
  46. $this->in = $reader;
  47. $this->bufferSize = $buffsize;
  48. }
  49. /**
  50. * Reads and returns $_bufferSize chunk of data.
  51. * @return mixed buffer or -1 if EOF.
  52. */
  53. function read($len = null) {
  54. // ignore $len param, not sure how to hanlde it, since
  55. // this should only read bufferSize amount of data.
  56. if ($len !== null) {
  57. $this->currentPosition = ftell($this->fd);
  58. }
  59. if ( ($data = $this->in->read($this->bufferSize)) !== -1 ) {
  60. // not all files end with a newline character, so we also need to check EOF
  61. if (!$this->in->eof()) {
  62. $notValidPart = strrchr($data, "\n");
  63. $notValidPartSize = strlen($notValidPart);
  64. if ( $notValidPartSize > 1 ) {
  65. // Block doesn't finish on a EOL
  66. // Find the last EOL and forgot all following stuff
  67. $dataSize = strlen($data);
  68. $validSize = $dataSize - $notValidPartSize + 1;
  69. $data = substr($data, 0, $validSize);
  70. // Rewind to the begining of the forgotten stuff.
  71. $this->in->skip(-$notValidPartSize+1);
  72. }
  73. } // if !EOF
  74. }
  75. return $data;
  76. }
  77. function skip($n) {
  78. return $this->in->skip($n);
  79. }
  80. function reset() {
  81. return $this->in->reset();
  82. }
  83. function close() {
  84. return $this->in->close();
  85. }
  86. function open() {
  87. return $this->in->open();
  88. }
  89. /**
  90. * Read a line from input stream.
  91. */
  92. function readLine() {
  93. $line = null;
  94. while ( ($ch = $this->readChar()) !== -1 ) {
  95. if ( $ch === "\n" ) {
  96. break;
  97. }
  98. $line .= $ch;
  99. }
  100. // Warning : Not considering an empty line as an EOF
  101. if ( $line === null && $ch !== -1 )
  102. return "";
  103. return $line;
  104. }
  105. /**
  106. * Reads a single char from the reader.
  107. * @return string single char or -1 if EOF.
  108. */
  109. function readChar() {
  110. if ( $this->buffer === null ) {
  111. // Buffer is empty, fill it ...
  112. $read = $this->in->read($this->bufferSize);
  113. if ($read === -1) {
  114. $ch = -1;
  115. } else {
  116. $this->buffer = $read;
  117. return $this->readChar(); // recurse
  118. }
  119. } else {
  120. // Get next buffered char ...
  121. // handle case where buffer is read-in, but is empty. The next readChar() will return -1 EOF,
  122. // so we just return empty string (char) at this point. (Probably could also return -1 ...?)
  123. $ch = ($this->buffer !== "") ? $this->buffer{$this->bufferPos} : '';
  124. $this->bufferPos++;
  125. if ( $this->bufferPos >= strlen($this->buffer) ) {
  126. $this->buffer = null;
  127. $this->bufferPos = 0;
  128. }
  129. }
  130. return $ch;
  131. }
  132. /**
  133. * Returns whether eof has been reached in stream.
  134. * This is important, because filters may want to know if the end of the file (and not just buffer)
  135. * has been reached.
  136. * @return boolean
  137. */
  138. function eof() {
  139. return $this->in->eof();
  140. }
  141. function getResource() {
  142. return $this->in->getResource();
  143. }
  144. }
  145. ?>