Version 1
[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         switch ($whence) {
56             case SEEK_CUR:
57                 $this->offset += $offset;
58                 return true;
59
60             case SEEK_END:
61                 $this->offset = $this->size() + $offset;
62                 return true;
63
64             case SEEK_SET:
65                 $this->offset = $offset;
66                 return true;
67
68             default:
69                 return false;
70         }
71
72         return false;
73     }
74
75     /**
76      * checks whether pointer is at end of file
77      *
78      * @return  bool
79      */
80     public function eof()
81     {
82         return $this->size() <= $this->offset;
83     }
84
85     /**
86      * writes an amount of data
87      *
88      * @param   string  $data
89      * @return  amount of written bytes
90      */
91     public function write($data)
92     {
93         $dataLength    = strlen($data);
94         $this->doWrite($data, $this->offset, $dataLength);
95         $this->offset += $dataLength;
96         return $dataLength;
97     }
98
99     /**
100      * actual writing of data with specified length at given offset
101      *
102      * @param   string  $data
103      * @param   int     $offset
104      * @param   int     $length
105      */
106     protected abstract function doWrite($data, $offset, $length);
107
108     /**
109      * for backwards compatibility with vfsStreamFile::bytesRead()
110      *
111      * @return  int
112      * @deprecated
113      */
114     public function bytesRead()
115     {
116         return $this->offset;
117     }
118
119     /**
120      * for backwards compatibility with vfsStreamFile::readUntilEnd()
121      *
122      * @return  string
123      * @deprecated
124      */
125     public function readUntilEnd()
126     {
127         return substr($this->content(), $this->offset);
128     }
129 }