~tcuthbert/wordpress/openstack-objectstorage-k8s

« back to all changes in this revision

Viewing changes to vendor/guzzlehttp/streams/src/LimitStream.php

  • Committer: Jacek Nykis
  • Date: 2015-02-11 15:35:31 UTC
  • Revision ID: jacek.nykis@canonical.com-20150211153531-hmy6zi0ov2qfkl0b
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
namespace GuzzleHttp\Stream;
 
3
use GuzzleHttp\Stream\Exception\SeekException;
 
4
 
 
5
/**
 
6
 * Decorator used to return only a subset of a stream
 
7
 */
 
8
class LimitStream implements StreamInterface, MetadataStreamInterface
 
9
{
 
10
    use StreamDecoratorTrait;
 
11
 
 
12
    /** @var int Offset to start reading from */
 
13
    private $offset;
 
14
 
 
15
    /** @var int Limit the number of bytes that can be read */
 
16
    private $limit;
 
17
 
 
18
    /**
 
19
     * @param StreamInterface $stream Stream to wrap
 
20
     * @param int             $limit  Total number of bytes to allow to be read
 
21
     *                                from the stream. Pass -1 for no limit.
 
22
     * @param int|null        $offset Position to seek to before reading (only
 
23
     *                                works on seekable streams).
 
24
     */
 
25
    public function __construct(
 
26
        StreamInterface $stream,
 
27
        $limit = -1,
 
28
        $offset = 0
 
29
    ) {
 
30
        $this->stream = $stream;
 
31
        $this->setLimit($limit);
 
32
        $this->setOffset($offset);
 
33
    }
 
34
 
 
35
    public function eof()
 
36
    {
 
37
        if ($this->limit == -1) {
 
38
            return $this->stream->eof();
 
39
        }
 
40
 
 
41
        $tell = $this->stream->tell();
 
42
 
 
43
        return $tell === false ||
 
44
            ($tell >= $this->offset + $this->limit) ||
 
45
            $this->stream->eof();
 
46
    }
 
47
 
 
48
    /**
 
49
     * Returns the size of the limited subset of data
 
50
     * {@inheritdoc}
 
51
     */
 
52
    public function getSize()
 
53
    {
 
54
        if (null === ($length = $this->stream->getSize())) {
 
55
            return null;
 
56
        } elseif ($this->limit == -1) {
 
57
            return $length - $this->offset;
 
58
        } else {
 
59
            return min($this->limit, $length - $this->offset);
 
60
        }
 
61
    }
 
62
 
 
63
    /**
 
64
     * Allow for a bounded seek on the read limited stream
 
65
     * {@inheritdoc}
 
66
     */
 
67
    public function seek($offset, $whence = SEEK_SET)
 
68
    {
 
69
        if ($whence != SEEK_SET) {
 
70
            return false;
 
71
        }
 
72
 
 
73
        if ($offset < $this->offset) {
 
74
            $offset = $this->offset;
 
75
        }
 
76
 
 
77
        if ($this->limit !== -1 && $offset > ($this->offset + $this->limit)) {
 
78
            $offset = $this->offset + $this->limit;
 
79
        }
 
80
 
 
81
        return $this->stream->seek($offset);
 
82
    }
 
83
 
 
84
    /**
 
85
     * Give a relative tell()
 
86
     * {@inheritdoc}
 
87
     */
 
88
    public function tell()
 
89
    {
 
90
        return $this->stream->tell() - $this->offset;
 
91
    }
 
92
 
 
93
    /**
 
94
     * Set the offset to start limiting from
 
95
     *
 
96
     * @param int $offset Offset to seek to and begin byte limiting from
 
97
     *
 
98
     * @return self
 
99
     * @throws SeekException
 
100
     */
 
101
    public function setOffset($offset)
 
102
    {
 
103
        $current = $this->stream->tell();
 
104
 
 
105
        if ($current !== $offset) {
 
106
            // If the stream cannot seek to the offset position, then read to it
 
107
            if (!$this->stream->seek($offset)) {
 
108
                if ($current > $offset) {
 
109
                    throw new SeekException($this, $offset);
 
110
                } else {
 
111
                    $this->stream->read($offset - $current);
 
112
                }
 
113
            }
 
114
        }
 
115
 
 
116
        $this->offset = $offset;
 
117
 
 
118
        return $this;
 
119
    }
 
120
 
 
121
    /**
 
122
     * Set the limit of bytes that the decorator allows to be read from the
 
123
     * stream.
 
124
     *
 
125
     * @param int $limit Number of bytes to allow to be read from the stream.
 
126
     *                   Use -1 for no limit.
 
127
     * @return self
 
128
     */
 
129
    public function setLimit($limit)
 
130
    {
 
131
        $this->limit = $limit;
 
132
 
 
133
        return $this;
 
134
    }
 
135
 
 
136
    public function read($length)
 
137
    {
 
138
        if ($this->limit == -1) {
 
139
            return $this->stream->read($length);
 
140
        }
 
141
 
 
142
        // Check if the current position is less than the total allowed
 
143
        // bytes + original offset
 
144
        $remaining = ($this->offset + $this->limit) - $this->stream->tell();
 
145
        if ($remaining > 0) {
 
146
            // Only return the amount of requested data, ensuring that the byte
 
147
            // limit is not exceeded
 
148
            return $this->stream->read(min($remaining, $length));
 
149
        } else {
 
150
            return false;
 
151
        }
 
152
    }
 
153
}