PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/phing/phing/classes/phing/system/io/BufferedReader.php

https://gitlab.com/Isaki/le331.fr
PHP | 196 lines | 91 code | 28 blank | 77 comment | 13 complexity | 4bf77aa30a053d1a17eddecd9e27a3ae MD5 | raw file
  1. <?php
  2. /*
  3. * $Id: 860064af39647023c2644e87267184447ceb93ae $
  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. *
  27. * @see FilterReader
  28. *
  29. * @package phing.system.io
  30. */
  31. class BufferedReader extends Reader
  32. {
  33. private $bufferSize = 0;
  34. private $buffer = null;
  35. private $bufferPos = 0;
  36. /**
  37. * The Reader we are buffering for.
  38. */
  39. private $in;
  40. /**
  41. *
  42. * @param Reader $reader The reader (e.g. FileReader).
  43. * @param integer $buffsize The size of the buffer we should use for reading files.
  44. * A large buffer ensures that most files (all scripts?) are parsed in 1 buffer.
  45. */
  46. public function __construct(Reader $reader, $buffsize = 65536)
  47. {
  48. $this->in = $reader;
  49. $this->bufferSize = $buffsize;
  50. }
  51. /**
  52. * Reads and returns a chunk of data.
  53. * @param int $len Number of bytes to read. Default is to read configured buffer size number of bytes.
  54. * @return mixed buffer or -1 if EOF.
  55. */
  56. public function read($len = null)
  57. {
  58. // if $len is specified, we'll use that; otherwise, use the configured buffer size.
  59. if ($len === null) {
  60. $len = $this->bufferSize;
  61. }
  62. if (($data = $this->in->read($len)) !== -1) {
  63. // not all files end with a newline character, so we also need to check EOF
  64. if (!$this->in->eof()) {
  65. $notValidPart = strrchr($data, "\n");
  66. $notValidPartSize = strlen($notValidPart);
  67. if ($notValidPartSize > 1) {
  68. // Block doesn't finish on a EOL
  69. // Find the last EOL and forget all following stuff
  70. $dataSize = strlen($data);
  71. $validSize = $dataSize - $notValidPartSize + 1;
  72. $data = substr($data, 0, $validSize);
  73. // Rewind to the beginning of the forgotten stuff.
  74. $this->in->skip(-$notValidPartSize + 1);
  75. }
  76. } // if !EOF
  77. }
  78. return $data;
  79. }
  80. /**
  81. * @param int $n
  82. */
  83. public function skip($n)
  84. {
  85. return $this->in->skip($n);
  86. }
  87. public function reset()
  88. {
  89. return $this->in->reset();
  90. }
  91. /**
  92. * @return mixed
  93. */
  94. public function close()
  95. {
  96. return $this->in->close();
  97. }
  98. /**
  99. * @return mixed
  100. */
  101. public function open()
  102. {
  103. return $this->in->open();
  104. }
  105. /**
  106. * Read a line from input stream.
  107. */
  108. public function readLine()
  109. {
  110. $line = null;
  111. while (($ch = $this->readChar()) !== -1) {
  112. if ($ch === "\n") {
  113. break;
  114. }
  115. $line .= $ch;
  116. }
  117. // Warning : Not considering an empty line as an EOF
  118. if ($line === null && $ch !== -1) {
  119. return "";
  120. }
  121. return $line;
  122. }
  123. /**
  124. * Reads a single char from the reader.
  125. * @return string single char or -1 if EOF.
  126. */
  127. public function readChar()
  128. {
  129. if ($this->buffer === null) {
  130. // Buffer is empty, fill it ...
  131. $read = $this->in->read($this->bufferSize);
  132. if ($read === -1) {
  133. $ch = -1;
  134. } else {
  135. $this->buffer = $read;
  136. return $this->readChar(); // recurse
  137. }
  138. } else {
  139. // Get next buffered char ...
  140. // handle case where buffer is read-in, but is empty. The next readChar() will return -1 EOF,
  141. // so we just return empty string (char) at this point. (Probably could also return -1 ...?)
  142. $ch = ($this->buffer !== "") ? $this->buffer{$this->bufferPos} : '';
  143. $this->bufferPos++;
  144. if ($this->bufferPos >= strlen($this->buffer)) {
  145. $this->buffer = null;
  146. $this->bufferPos = 0;
  147. }
  148. }
  149. return $ch;
  150. }
  151. /**
  152. * Returns whether eof has been reached in stream.
  153. * This is important, because filters may want to know if the end of the file (and not just buffer)
  154. * has been reached.
  155. * @return boolean
  156. */
  157. public function eof()
  158. {
  159. return $this->in->eof();
  160. }
  161. /**
  162. * @return string
  163. */
  164. public function getResource()
  165. {
  166. return $this->in->getResource();
  167. }
  168. }