-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathbuffered_stream.py
More file actions
59 lines (46 loc) · 1.93 KB
/
buffered_stream.py
File metadata and controls
59 lines (46 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# pylint: skip-file
"""Small helper class to provide a small slice of a stream.
This class reads ahead to detect if we are at the end of the stream.
"""
from gcloud.streaming import exceptions
# TODO(user): Consider replacing this with a StringIO.
class BufferedStream(object):
"""Buffers a stream, reading ahead to determine if we're at the end."""
def __init__(self, stream, start, size):
self._stream = stream
self._start_pos = start
self._buffer_pos = 0
self._buffered_data = self._stream.read(size)
self._stream_at_end = len(self._buffered_data) < size
self._end_pos = self._start_pos + len(self._buffered_data)
def __repr__(self):
return ('Buffered stream %s from position %s-%s with %s '
'bytes remaining' % (self._stream, self._start_pos,
self._end_pos, self._bytes_remaining))
def __len__(self):
return len(self._buffered_data)
@property
def stream_exhausted(self):
return self._stream_at_end
@property
def stream_end_position(self):
return self._end_pos
@property
def _bytes_remaining(self):
return len(self._buffered_data) - self._buffer_pos
def read(self, size=None): # pylint: disable=invalid-name
"""Reads from the buffer."""
if size is None or size < 0:
raise exceptions.NotYetImplementedError(
'Illegal read of size %s requested on BufferedStream. '
'Wrapped stream %s is at position %s-%s, '
'%s bytes remaining.' %
(size, self._stream, self._start_pos, self._end_pos,
self._bytes_remaining))
data = b''
if self._bytes_remaining:
size = min(size, self._bytes_remaining)
data = self._buffered_data[
self._buffer_pos:self._buffer_pos + size]
self._buffer_pos += size
return data