Version 1
[yaffs-website] / vendor / mikey179 / vfsStream / src / main / php / org / bovigo / vfs / content / LargeFileContent.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  * File content implementation to mock large files.
13  *
14  * When content is written via write() the data will be written into the
15  * positions according to the current offset.
16  * When content is read via read() it will use the already written data. If no
17  * data is written at the offsets to read those offsets will be filled with
18  * spaces.
19  * Please note that accessing the whole content via content() will deliver a
20  * string with the length of the originally defined size. It is not advisable to
21  * do so with large sizes, except you have enough memory and time. :-)
22  *
23  * @since  1.3.0
24  */
25 class LargeFileContent extends SeekableFileContent implements FileContent
26 {
27     /**
28      * byte array of written content
29      *
30      * @type  char[]
31      */
32     private $content = array();
33     /**
34      * file size in bytes
35      *
36      * @type  int
37      */
38     private $size;
39
40     /**
41      * constructor
42      *
43      * @param  int  $size  file size in bytes
44      */
45     public function __construct($size)
46     {
47         $this->size = $size;
48     }
49
50     /**
51      * create large file with given size in kilobyte
52      *
53      * @param   int  $kilobyte
54      * @return  LargeFileContent
55      */
56     public static function withKilobytes($kilobyte)
57     {
58         return new self($kilobyte * 1024);
59     }
60
61     /**
62      * create large file with given size in megabyte
63      *
64      * @param   int  $megabyte
65      * @return  LargeFileContent
66      */
67     public static function withMegabytes($megabyte)
68     {
69         return self::withKilobytes($megabyte * 1024);
70     }
71
72     /**
73      * create large file with given size in gigabyte
74      *
75      * @param   int  $gigabyte
76      * @return  LargeFileContent
77      */
78     public static function withGigabytes($gigabyte)
79     {
80         return self::withMegabytes($gigabyte * 1024);
81     }
82
83     /**
84      * returns actual content
85      *
86      * @return  string
87      */
88     public function content()
89     {
90         return $this->doRead(0, $this->size);
91     }
92
93     /**
94      * returns size of content
95      *
96      * @return  int
97      */
98     public function size()
99     {
100         return $this->size;
101     }
102
103     /**
104      * actual reading of given byte count starting at given offset
105      *
106      * @param  int  $offset
107      * @param  int  $count
108      */
109     protected function doRead($offset, $count)
110     {
111         if (($offset + $count) > $this->size) {
112             $count = $this->size - $offset;
113         }
114
115         $result = '';
116         for ($i = 0; $i < $count; $i++) {
117             if (isset($this->content[$i + $offset])) {
118                 $result .= $this->content[$i + $offset];
119             } else {
120                 $result .= ' ';
121             }
122         }
123
124         return $result;
125     }
126
127     /**
128      * actual writing of data with specified length at given offset
129      *
130      * @param   string  $data
131      * @param   int     $offset
132      * @param   int     $length
133      */
134     protected function doWrite($data, $offset, $length)
135     {
136         for ($i = 0; $i < $length; $i++) {
137             $this->content[$i + $offset] = $data{$i};
138         }
139
140         if ($offset >= $this->size) {
141             $this->size += $length;
142         } elseif (($offset + $length) > $this->size) {
143             $this->size = $offset + $length;
144         }
145     }
146
147     /**
148      * Truncates a file to a given length
149      *
150      * @param   int  $size length to truncate file to
151      * @return  bool
152      */
153     public function truncate($size)
154     {
155         $this->size = $size;
156         foreach (array_filter(array_keys($this->content),
157                               function($pos) use ($size)
158                               {
159                                   return $pos >= $size;
160                               }
161                 ) as $removePos) {
162             unset($this->content[$removePos]);
163         }
164
165         return true;
166     }
167 }