forked from xp-framework/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuffer.class.php
More file actions
executable file
·161 lines (140 loc) · 4.22 KB
/
Buffer.class.php
File metadata and controls
executable file
·161 lines (140 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php namespace io\streams;
use io\{File, Folder, Path, TempFile, IOException};
use lang\IllegalArgumentException;
/**
* Buffers in memory up until a given threshold, using the file system once
* it's exceeded.
*
* @see https://github.com/xp-forge/web/issues/118
* @test io.unittest.BufferTest
*/
class Buffer implements InputStream, OutputStream, Seekable {
private $files, $threshold, $persist;
private $memory= '';
private $file= null;
private $size= 0;
private $pointer= 0;
/**
* Creates a new buffer
*
* @param string|io.Folder|io.Path|io.File $files
* @param int $threshold
* @param bool $persist
* @throws lang.IllegalArgumentException
*/
public function __construct($files, int $threshold= 0, bool $persist= false) {
if ($threshold < 0) {
throw new IllegalArgumentException('Threshold must be >= 0');
}
$this->threshold= $threshold;
$this->persist= $persist;
if ($files instanceof File) {
$this->files= fn() => $files;
} else if ($files instanceof Path && $files->isFile()) {
$this->files= fn() => $files->asFile();
} else {
$this->files= fn() => new TempFile("b{$this->threshold}", $files);
}
}
/** Returns buffer size */
public function size(): int { return $this->size; }
/** Returns the underlying file, if any */
public function file(): ?File { return $this->file; }
/**
* Write a string
*
* @param string $bytes
* @return void
*/
public function write($bytes) {
$length= strlen($bytes);
if ($this->size + $length <= $this->threshold) {
$tail= strlen($this->memory);
if ($this->pointer < $tail) {
$this->memory= substr_replace($this->memory, $bytes, $this->pointer, $length);
} else if ($this->pointer > $tail) {
$this->memory.= str_repeat("\x00", $this->pointer - $tail).$bytes;
} else {
$this->memory.= $bytes;
}
$this->pointer+= $length;
$this->size= strlen($this->memory);
} else {
if (null === $this->file) {
$this->file= ($this->files)();
$this->file->open(File::REWRITE);
$this->file->write($this->memory);
$this->file->seek($this->pointer, SEEK_SET);
$this->memory= null;
}
$this->file->write($bytes);
$this->size= $this->file->size();
}
}
/** @return void */
public function flush() {
$this->file && $this->file->flush();
}
/** @return int */
public function available() {
return $this->size - ($this->file ? $this->file->tell() : $this->pointer);
}
/**
* Read a string
*
* @param int $limit
* @return string
*/
public function read($limit= 8192) {
if ($this->file) {
return (string)$this->file->read($limit);
} else {
$chunk= substr($this->memory, $this->pointer, $limit);
$this->pointer+= strlen($chunk);
return $chunk;
}
}
/**
* Resets buffer to be able to read from the beginning. Optimized
* form of calling `seek(0, SEEK_SET)`.
*
* @return void
*/
public function reset() {
$this->file ? $this->file->seek(0, SEEK_SET) : $this->pointer= 0;
}
/**
* Seeks to a given offset.
*
* @param int $offset
* @param int $whence SEEK_SET, SEEK_CUR or SEEK_END
* @return void
* @throws io.IOException
*/
public function seek($offset, $whence= SEEK_SET) {
switch ($whence) {
case SEEK_SET: $position= $offset; break;
case SEEK_CUR: $position= ($this->file ? $this->file->tell() : $this->pointer) + $offset; break;
case SEEK_END: $position= $this->size + $offset; break;
default: $position= -1; break;
}
if ($position < 0) {
throw new IOException("Seek error, position {$offset} in mode {$whence}");
}
$this->file ? $this->file->seek($position, SEEK_SET) : $this->pointer= $position;
}
/** @return int */
public function tell() {
return $this->file ? $this->file->tell() : $this->pointer;
}
/** @return void */
public function close() {
if (null === $this->file || !$this->file->isOpen()) return;
$this->file->close();
$this->persist || ($this->file->exists() && $this->file->unlink());
}
/** Ensure the file (if any) is closed */
public function __destruct() {
$this->close();
}
}