From e424ea2cbe4fdcb7a29c88663747ce34f930410d Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Sun, 19 Dec 2010 17:56:58 +0000 Subject: [PATCH] [TASK] Implemented getBufferLength() and getNextChars() --- Classes/Misc/Buffer.php | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/Classes/Misc/Buffer.php b/Classes/Misc/Buffer.php index e101d12..362fc05 100644 --- a/Classes/Misc/Buffer.php +++ b/Classes/Misc/Buffer.php @@ -3,7 +3,6 @@ * Buffer class for a string. * Will fix issues with sockets that don't care about linebreaks. * Can also be used for all kinds of purpose. - * TODO: implement a method to return the next X bytes (return false if not enough bytes there,yet) * @author jpt * @package Misc */ @@ -76,12 +75,23 @@ class Misc_Buffer { } /** - * Returns the next $length chars from the buffer. - * @return string Returns "" if no + * Returns the length of the buffer. + * @return int + */ + public function getBufferLength() { + return strlen($this->buffer); + } + + /** + * Returns the next $length chars from the buffer or throws an exception if this cannot be done. + * @throws Exception_GeneralException + * @return string */ public function getNextChars($length) { - //TODO: substr und so - throw new Exception_GeneralException("This feature is not implemented yet!", 1291824224); + if(strlen($this->buffer) < $length) throw new Exception_GeneralException("Cannot return ".$length." chars, there are only ".strlen($this->buffer)." chars left!", 1292780386); + $data = substr($this->buffer, 0, $length); + $this->buffer = substr($this->buffer, $length); + return $data; } /**