aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files_external/3rdparty/icewind/streams/src/RetryWrapper.php
blob: 2899a97a0d19871888e62c4b9d7fb60a3589f246 (plain)
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
<?php
/**
 * SPDX-FileCopyrightText: 2016 Robin Appelman <robin@icewind.nl>
 * SPDX-License-Identifier: MIT
 */

namespace Icewind\Streams;

/**
 * Wrapper that retries reads/writes to remote streams that dont deliver/recieve all requested data at once
 */
class RetryWrapper extends Wrapper {
	public static function wrap($source) {
		return self::wrapSource($source);
	}

	public function dir_opendir($path, $options) {
		return false;
	}

	public function stream_open($path, $mode, $options, &$opened_path) {
		$this->loadContext();
		return true;
	}

	public function stream_read($count) {
		$result = parent::stream_read($count);

		$bytesReceived = strlen($result);
		while (strlen($result) > 0 && $bytesReceived < $count && !$this->stream_eof()) {
			$result .= parent::stream_read($count - $bytesReceived);
			$bytesReceived = strlen($result);
		}

		return $result;
	}

	public function stream_write($data) {
		$bytesToSend = strlen($data);
		$bytesWritten = parent::stream_write($data);
		$result = $bytesWritten;

		while ($bytesWritten > 0 && $result < $bytesToSend && !$this->stream_eof()) {
			$dataLeft = substr($data, $result);
			$bytesWritten = parent::stream_write($dataLeft);
			$result += $bytesWritten;
		}

		return $result;
	}
}