@@ -37,7 +37,7 @@ public function prepend($str)
37
37
*
38
38
* @param int $len length in bytes, must be positive or zero
39
39
* @return string
40
- * @throws \LogicException
40
+ * @throws \UnderflowException
41
41
*/
42
42
public function read ($ len )
43
43
{
@@ -53,7 +53,7 @@ public function read($len)
53
53
54
54
// ensure buffer size contains $len bytes by checking target buffer position
55
55
if ($ len < 0 || !isset ($ this ->buffer [$ this ->bufferPos + $ len - 1 ])) {
56
- throw new \LogicException ('Not enough data in buffer to read ' . $ len . ' bytes ' );
56
+ throw new \UnderflowException ('Not enough data in buffer to read ' . $ len . ' bytes ' );
57
57
}
58
58
$ buffer = \substr ($ this ->buffer , $ this ->bufferPos , $ len );
59
59
$ this ->bufferPos += $ len ;
@@ -62,39 +62,58 @@ public function read($len)
62
62
}
63
63
64
64
/**
65
- * Skips binary string data with given byte length from buffer
65
+ * Reads data with given byte length from buffer into a new buffer
66
66
*
67
- * This method can be used instead of `read()` if you do not care about the
68
- * bytes that will be skipped.
67
+ * This class keeps consumed data in memory for performance reasons and only
68
+ * advances the internal buffer position by default. Reading data into a new
69
+ * buffer will clear the data from the original buffer to free memory.
69
70
*
70
- * @param int $len length in bytes, must be positve and non- zero
71
- * @return void
72
- * @throws \LogicException
71
+ * @param int $len length in bytes, must be positive or zero
72
+ * @return self
73
+ * @throws \UnderflowException
73
74
*/
74
- public function skip ($ len )
75
+ public function readBuffer ($ len )
75
76
{
76
- if ($ len < 1 || !isset ($ this ->buffer [$ this ->bufferPos + $ len - 1 ])) {
77
- throw new \LogicException ('Not enough data in buffer ' );
77
+ // happy path to return empty buffer without any memory access for zero length string
78
+ if ($ len === 0 ) {
79
+ return new self ();
78
80
}
79
- $ this ->bufferPos += $ len ;
81
+
82
+ // ensure buffer size contains $len bytes by checking target buffer position
83
+ if ($ len < 0 || !isset ($ this ->buffer [$ this ->bufferPos + $ len - 1 ])) {
84
+ throw new \UnderflowException ('Not enough data in buffer to read ' . $ len . ' bytes ' );
85
+ }
86
+
87
+ $ buffer = new self ();
88
+ $ buffer ->buffer = $ this ->read ($ len );
89
+
90
+ if (!isset ($ this ->buffer [$ this ->bufferPos ])) {
91
+ $ this ->buffer = '' ;
92
+ } else {
93
+ $ this ->buffer = \substr ($ this ->buffer , $ this ->bufferPos );
94
+ }
95
+ $ this ->bufferPos = 0 ;
96
+
97
+ return $ buffer ;
98
+
80
99
}
81
100
82
101
/**
83
- * Clears all consumed data from the buffer
102
+ * Skips binary string data with given byte length from buffer
84
103
*
85
- * This class keeps consumed data in memory for performance reasons and only
86
- * advances the internal buffer position until this method is called .
104
+ * This method can be used instead of `read()` if you do not care about the
105
+ * bytes that will be skipped .
87
106
*
107
+ * @param int $len length in bytes, must be positve and non-zero
88
108
* @return void
109
+ * @throws \UnderflowException
89
110
*/
90
- public function trim ( )
111
+ public function skip ( $ len )
91
112
{
92
- if (!isset ($ this ->buffer [$ this ->bufferPos ])) {
93
- $ this ->buffer = '' ;
94
- } else {
95
- $ this ->buffer = \substr ($ this ->buffer , $ this ->bufferPos );
113
+ if ($ len < 1 || !isset ($ this ->buffer [$ this ->bufferPos + $ len - 1 ])) {
114
+ throw new \UnderflowException ('Not enough data in buffer ' );
96
115
}
97
- $ this ->bufferPos = 0 ;
116
+ $ this ->bufferPos += $ len ;
98
117
}
99
118
100
119
/**
@@ -201,13 +220,13 @@ public function readStringLen()
201
220
* Reads string until NULL character
202
221
*
203
222
* @return string
204
- * @throws \LogicException
223
+ * @throws \UnderflowException
205
224
*/
206
225
public function readStringNull ()
207
226
{
208
227
$ pos = \strpos ($ this ->buffer , "\0" , $ this ->bufferPos );
209
228
if ($ pos === false ) {
210
- throw new \LogicException ('Missing NULL character ' );
229
+ throw new \UnderflowException ('Missing NULL character ' );
211
230
}
212
231
213
232
$ ret = $ this ->read ($ pos - $ this ->bufferPos );
0 commit comments