-
Notifications
You must be signed in to change notification settings - Fork 4
/
AbstractFile.php
180 lines (158 loc) · 3.91 KB
/
AbstractFile.php
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
namespace Aternos\Hawk;
use Exception;
abstract class AbstractFile
{
/**
* @var false|resource
*/
protected $fileStream;
protected ?string $fileName = null;
protected ?string $dir = null;
/**
* @param int $int Uint32 big endian
* @return string Byte string
*/
public static function uInt32BigEndianToString(int $int): string
{
return pack("N", $int);
}
/**
* @codeCoverageIgnore
* @param int $uInt8 Uint8
* @return string Byte
*/
public static function uInt8ToString(int $uInt8): string
{
return pack("C", $uInt8);
}
/**
* @param ?string $path
* @throws Exception
*/
public function __construct(string $path = null)
{
if ($path === null) {
return;
}
$this->dir = dirname($path);
$this->fileName = basename($path);
$this->fileStream = fopen($this->dir . "/" . $this->fileName, "r+");
if ($this->fileStream === false) {
throw new Exception("Error while opening file.");
}
}
public function __destruct()
{
if (is_resource($this->fileStream)) {
fclose($this->fileStream);
}
}
/**
* Closes file stream
*
* @return void
*/
public function close(): void
{
if (is_resource($this->fileStream)) {
fclose($this->fileStream);
}
}
/**
* @codeCoverageIgnore
* @return string|null File dir
*/
public function getDir(): ?string
{
return $this->dir;
}
/**
* @codeCoverageIgnore
* @return string|null File name
*/
public function getFileName(): ?string
{
return $this->fileName;
}
/**
* @codeCoverageIgnore
* @param string $fileName
* @return void
*/
public function setFileName(string $fileName): void
{
$this->fileName = $fileName;
}
/**
* @codeCoverageIgnore
* @return false|resource
*/
public function getFileStream(): mixed
{
return $this->fileStream;
}
/**
* Reads byte string and converts it to Uint32 big endian
*
* @return int Uint32 big endian
* @throws Exception "Nothing to unpack"
*/
public function readStringToUInt32BigEndian(): int
{
$result = unpack("N", $this->read(4));
if ($result === false) {
throw new Exception("Nothing to unpack");
}
return $result[1];
}
/**
* Reads byte string and converts it to Uint8
*
* @return int Uint8
* @throws Exception "Error while reading"
*/
public function readStringToUInt8(): int
{
return unpack("C", $this->read(1))[1];
}
/**
* @param string $content
* @return void
*/
public function setContent(string $content): void
{
$this->fileStream = fopen("php://memory", "r+");
fputs($this->fileStream, $content);
fseek($this->fileStream,0,SEEK_SET);
}
public function getContent(): string
{
fseek($this->fileStream,0,SEEK_SET);
$content = "";
while (!feof($this->fileStream)){
$currentContent = fgets($this->fileStream);
if ($currentContent === false) {
throw new Exception("Error while getting content");
}
$content .= $currentContent;
}
return $content;
}
/**
* @throws Exception "Error while reading"
*/
abstract public function read(int $length): string;
/**
* @throws Exception "Error while writing"
*/
abstract public function write(string $data): int;
/**
* @throws Exception "Error while seeking"
*/
abstract public function seek(int $offset, int $seekType): bool;
/**
* @throws Exception "Error while getting pointer position."
*/
abstract public function tell(): int;
}