Skip to content

Commit

Permalink
[mms] Fix getString() in certain use cases.
Browse files Browse the repository at this point in the history
I'll admit the getString() usage is a bit wonky, but it is an
established API call and, thus, the proper way to return strings from
the object based on lengths.
  • Loading branch information
slusarz committed Sep 30, 2013
1 parent def19d4 commit 7ff5d4a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 25 deletions.
55 changes: 30 additions & 25 deletions framework/Stream/lib/Horde/Stream.php
Expand Up @@ -111,7 +111,7 @@ public function add($data, $reset = false)
} elseif ($data instanceof Horde_Stream) {
$dpos = $data->pos();
while (!$data->eof()) {
$this->add($data->read(65536));
$this->add($data->getString(null, $data->pos() + 65536));
}
$data->seek($dpos, false);
} else {
Expand Down Expand Up @@ -287,41 +287,46 @@ public function search($char, $reverse = false, $reset = true)
}

/**
* Returns the stream (or a portion of it) as a string.
*
* @param integer $start The starting position. If null, starts from the
* current position. If negative, starts this far
* back from the current position.
* @param integer $end The ending position. If null, reads the entire
* stream. If negative, sets ending this far from
* the end of the stream.
* Returns the stream (or a portion of it) as a string. Position values
* are the byte position in the stream.
*
* @param integer $start The starting position. If positive, start from
* this position. If negative, starts this length
* back from the current position. If null, starts
* from the current position.
* @param integer $end The ending position relative to the starting
* position (if positive). If negative, end this
* length back from the end of the stream. If null,
* reads to the end of the stream.
*
* @return string A string.
*/
public function getString($start = null, $end = null)
{
if (!is_null($start) && ($start < 0)) {
$this->seek($start);
$start = $this->pos();
if (!is_null($start)) {
$this->seek($start, ($start < 0));
}

if (!is_null($end)) {
$curr = is_null($start)
? $this->pos()
: $start;
if (is_null($end)) {
$len = 8192;
} else {
$end = ($end >= 0)
? $end - $curr + 1
: $this->length() - $curr + $end;
if ($end < 0) {
$end = 0;
? $end - $this->pos() + 1
: $this->length() - $this->pos() + $end;
$len = max($end, 0);
}

$out = '';

while (!feof($this->stream) && (is_null($end) || $len)) {
$read = fread($this->stream, $len);
$out .= $read;
if (!is_null($end)) {
$len -= strlen($read);
}
}

return stream_get_contents(
$this->stream,
is_null($end) ? -1 : $end,
is_null($start) ? -1 : $start
);
return $out;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions framework/Stream/package.xml
Expand Up @@ -22,6 +22,7 @@
</stability>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
* [mms] Fix getString() in certain use cases.
* [mjr] Add Horde_Stream#read().
* [mms] Horde_Stream#getToChar() now supports multiple characters in search string.
* [mms] Horde_Stream#search() now supports multiple characters in search string.
Expand Down Expand Up @@ -206,6 +207,7 @@
<date>2013-08-05</date>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
* [mms] Fix getString() in certain use cases.
* [mjr] Add Horde_Stream#read().
* [mms] Horde_Stream#getToChar() now supports multiple characters in search string.
* [mms] Horde_Stream#search() now supports multiple characters in search string.
Expand Down
21 changes: 21 additions & 0 deletions framework/Stream/test/Horde/Stream/TempTest.php
Expand Up @@ -463,4 +463,25 @@ public function testUtf8Parsing()
);
}

public function testParsingAnExistingStreamObject()
{
$stream = new Horde_Stream_Temp();
// 100000 byte stream.
$stream->add(str_repeat('1234567890', 10000));
$stream->rewind();

$this->assertEquals(
100000,
$stream->length()
);

$stream2 = new Horde_Stream_Temp();
$stream2->add($stream);

$this->assertEquals(
100000,
$stream2->length()
);
}

}

0 comments on commit 7ff5d4a

Please sign in to comment.