-
Notifications
You must be signed in to change notification settings - Fork 2
/
AWSInputByteStream.php
101 lines (87 loc) · 2.6 KB
/
AWSInputByteStream.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
<?php
namespace UltimateGuitar\SwiftSesTransport;
use Swift_InputByteStream;
class AWSInputByteStream implements Swift_InputByteStream
{
public function __construct( $socket )
{
$this->socket = $socket;
$this->buffer = '';
$this->counter = 0;
}
/**
* Writes $bytes to the end of the stream.
*
* Writing may not happen immediately if the stream chooses to buffer. If
* you want to write these bytes with immediate effect, call {@link commit()}
* after calling write().
*
* This method returns the sequence ID of the write (i.e. 1 for first, 2 for
* second, etc etc).
*
* @param string $bytes
* @return int
*/
public function write($bytes)
{
$block = $this->buffer . $bytes;
$block_size = strlen( $block );
$encoded = base64_encode( $block );
$setback = 0;
while( substr( $encoded, -1 ) === '=' )
{
++$setback;
if( $setback >= $block_size )
{
$this->buffer = $block;
return ++$this->counter;
}
$encoded = base64_encode( substr( $block, 0, $setback * -1 ) );
}
if( $setback > 0 )
{
$this->buffer = substr( $block, $setback * -1 );
}
else
{
$this->buffer = '';
}
unset( $block );
$this->socket->write( urlencode( $encoded ) );
unset( $encoded );
return ++$this->counter;
}
/**
* For any bytes that are currently buffered inside the stream, force them
* off the buffer.
*/
public function commit()
{
// NOP - Since we have a required packet offset (3-bytes), we can't commit arbitrarily.
}
public function flushBuffers()
{
if( strlen( $this->buffer ) > 0 )
{
$this->socket->write( urlencode( base64_encode( $this->buffer ) ) );
}
$this->socket->finishWrite();
}
/**
* Attach $is to this stream.
* The stream acts as an observer, receiving all data that is written.
* All {@link write()} and {@link flushBuffers()} operations will be mirrored.
*
* @param Swift_InputByteStream $is
*/
public function bind(Swift_InputByteStream $is){}
/**
* Remove an already bound stream.
* If $is is not bound, no errors will be raised.
* If the stream currently has any buffered data it will be written to $is
* before unbinding occurs.
*
* @param Swift_InputByteStream $is
*/
public function unbind(Swift_InputByteStream $is){}
}