e4c3d9b0a4c9f7d1030fdff98ba923656928538f
[yaffs-website] / vendor / mikey179 / vfsStream / src / main / php / org / bovigo / vfs / content / SeekableFileContent.php
1 <?php
2 /**
3  * This file is part of vfsStream.
4  *
5  * For the full copyright and license information, please view the LICENSE
6  * file that was distributed with this source code.
7  *
8  * @package  org\bovigo\vfs
9  */
10 namespace org\bovigo\vfs\content;
11 /**
12  * Default implementation for file contents based on simple strings.
13  *
14  * @since  1.3.0
15  */
16 abstract class SeekableFileContent implements FileContent
17 {
18     /**
19      * current position within content
20      *
21      * @type  int
22      */
23     private $offset = 0;
24
25     /**
26      * reads the given amount of bytes from content
27      *
28      * @param   int     $count
29      * @return  string
30      */
31     public function read($count)
32     {
33         $data = $this->doRead($this->offset, $count);
34         $this->offset += $count;
35         return $data;
36     }
37
38     /**
39      * actual reading of given byte count starting at given offset
40      *
41      * @param  int  $offset
42      * @param  int  $count
43      */
44     protected abstract function doRead($offset, $count);
45
46     /**
47      * seeks to the given offset
48      *
49      * @param   int   $offset
50      * @param   int   $whence
51      * @return  bool
52      */
53     public function seek($offset, $whence)
54     {
55         $newOffset = $this->offset;
56         switch ($whence) {
57             case SEEK_CUR:
58                 $newOffset += $offset;
59                 break;
60
61             case SEEK_END:
62                 $newOffset = $this->size() + $offset;
63                 break;
64
65             case SEEK_SET:
66                 $newOffset = $offset;
67                 break;
68
69             default:
70                 return false;
71         }
72         
73         if ($newOffset<0) {
74             return false;
75         }
76         $this->offset = $newOffset;
77         return true;
78     }
79
80     /**
81      * checks whether pointer is at end of file
82      *
83      * @return  bool
84      */
85     public function eof()
86     {
87         return $this->size() <= $this->offset;
88     }
89
90     /**
91      * writes an amount of data
92      *
93      * @param   string  $data
94      * @return  amount of written bytes
95      */
96     public function write($data)
97     {
98         $dataLength    = strlen($data);
99         $this->doWrite($data, $this->offset, $dataLength);
100         $this->offset += $dataLength;
101         return $dataLength;
102     }
103
104     /**
105      * actual writing of data with specified length at given offset
106      *
107      * @param   string  $data
108      * @param   int     $offset
109      * @param   int     $length
110      */
111     protected abstract function doWrite($data, $offset, $length);
112
113     /**
114      * for backwards compatibility with vfsStreamFile::bytesRead()
115      *
116      * @return  int
117      * @deprecated
118      */
119     public function bytesRead()
120     {
121         return $this->offset;
122     }
123
124     /**
125      * for backwards compatibility with vfsStreamFile::readUntilEnd()
126      *
127      * @return  string
128      * @deprecated
129      */
130     public function readUntilEnd()
131     {
132         return substr($this->content(), $this->offset);
133     }
134 }