Commit 7304e954 authored by Sebastian Kummer's avatar Sebastian Kummer

ZP-707 Undo changes to the stream wrappers to be able to write at the

beginning of the stream introduced with
f133d268.

Released under the Affero GNU General Public License (AGPL) version 3.
parent 1bd83093
...@@ -48,7 +48,6 @@ class MAPIStreamWrapper { ...@@ -48,7 +48,6 @@ class MAPIStreamWrapper {
private $mapistream; private $mapistream;
private $position; private $position;
private $streamlength; private $streamlength;
private $writtenData;
private $toTruncate; private $toTruncate;
/** /**
...@@ -70,8 +69,6 @@ class MAPIStreamWrapper { ...@@ -70,8 +69,6 @@ class MAPIStreamWrapper {
return false; return false;
$this->position = 0; $this->position = 0;
$this->writtenData = "";
$this->toTruncate = false; $this->toTruncate = false;
// this is our stream! // this is our stream!
...@@ -96,27 +93,18 @@ class MAPIStreamWrapper { ...@@ -96,27 +93,18 @@ class MAPIStreamWrapper {
*/ */
public function stream_read($len) { public function stream_read($len) {
$len = ($this->position + $len > $this->streamlength) ? ($this->streamlength - $this->position) : $len; $len = ($this->position + $len > $this->streamlength) ? ($this->streamlength - $this->position) : $len;
$data = "";
$prependLength = strlen($this->writtenData); // read 4 additional bytes from the stream so we can always truncate correctly
// prepend data at the beginning of the stream or when we are in the middle of it if ($this->toTruncate)
if ($prependLength > 0 && ($this->position == 0 || $this->position < $prependLength)) { $len += 4;
$prependDataLength = ($prependLength <= $len) ? $prependLength : $len; $data = mapi_stream_read($this->mapistream, $len);
$data = substr($this->writtenData, $this->position, $prependDataLength);
// is there remaining data to be read from the mapi stream?
$len = $len - strlen($data);
}
if ($len > 0) {
// read 4 additional bytes from the stream so we can always truncate correctly
if ($this->toTruncate)
$len += 4;
$data .= mapi_stream_read($this->mapistream, $len);
}
$this->position += strlen($data); $this->position += strlen($data);
// we need to truncate UTF8 compatible if ftruncate() was called // we need to truncate UTF8 compatible if ftruncate() was called
if ($this->toTruncate && $this->position >= $this->streamlength) { if ($this->toTruncate && $this->position >= $this->streamlength) {
$data = Utils::Utf8_truncate($data, $this->streamlength); $data = Utils::Utf8_truncate($data, $this->streamlength);
} }
return $data; return $data;
} }
...@@ -141,18 +129,6 @@ class MAPIStreamWrapper { ...@@ -141,18 +129,6 @@ class MAPIStreamWrapper {
return mapi_stream_seek($this->mapistream, $offset, $mapiWhence); return mapi_stream_seek($this->mapistream, $offset, $mapiWhence);
} }
/**
* Writes to the stream.
* Attention: In this implementation it will always write to the beginning of the stream and not the current position of the stream and NOT overwrite the stream, but prepend.
* Several write operation will be concatinated and be prepended to the original MAPI stream. This resets the position to 0 automatically.
* @param unknown $data
*/
public function stream_write($data) {
$this->writtenData .= $data;
$this->streamlength += strlen($data);
$this->position = 0;
}
/** /**
* Returns the current position on stream * Returns the current position on stream
* *
...@@ -175,14 +151,14 @@ class MAPIStreamWrapper { ...@@ -175,14 +151,14 @@ class MAPIStreamWrapper {
/** /**
* Truncates the stream to the new size. * Truncates the stream to the new size.
* *
* @param int $new_size * @param int $new_size
* @return boolean * @return boolean
*/ */
public function stream_truncate ($new_size) { public function stream_truncate ($new_size) {
$this->streamlength = $new_size; $this->streamlength = $new_size;
$this->toTruncate = true; $this->toTruncate = true;
if ($this->position > $this->streamlength) { if ($this->position > $this->streamlength) {
ZLog::Write(LOGLEVEL_WARN, sprintf("MAPIStreamWrapper->stream_truncate(): stream position (%d) ahead of new size of %d. Repositioning pointer to end of stream.", $this->position, $this->streamlength)); ZLog::Write(LOGLEVEL_WARN, sprintf("MAPIStreamWrapper->stream_truncate(): stream position (%d) ahead of new size of %d. Repositioning pointer to end of stream.", $this->position, $this->streamlength));
$this->position = $this->streamlength; $this->position = $this->streamlength;
......
...@@ -94,15 +94,13 @@ class StringStreamWrapper { ...@@ -94,15 +94,13 @@ class StringStreamWrapper {
/** /**
* Writes data to the stream. * Writes data to the stream.
* Attention: In this implementation will NOT overwrite the stream at the position, but insert. This resets the position to 0 automatically.
* *
* @param string $data * @param string $data
* @return int * @return int
*/ */
public function stream_write($data){ public function stream_write($data){
$l = strlen($data); $l = strlen($data);
$this->stringstream = substr($this->stringstream, 0, $this->position) . $data . substr($this->stringstream, $this->position); $this->stringstream = substr($this->stringstream, 0, $this->position) . $data . substr($this->stringstream, $this->position += $l);
$this->position = 0;
$this->stringlength = strlen($this->stringstream); $this->stringlength = strlen($this->stringstream);
return $l; return $l;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment