Skip to content

Commit 3c9e0c5

Browse files
committed
using concatenate_bytes
1 parent 00de96c commit 3c9e0c5

1 file changed

Lines changed: 16 additions & 16 deletions

File tree

wolframclient/serializers/wxfencoder/streaming.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from __future__ import absolute_import, print_function, unicode_literals
22

3-
from wolframclient.utils import six
43
from wolframclient.utils.api import zlib
5-
from wolframclient.utils.encoding import force_bytes
4+
from wolframclient.utils.decorators import decorate
5+
from wolframclient.utils.encoding import concatenate_bytes, force_bytes
66

77

88
class ZipCompressedWriter(object):
@@ -32,6 +32,7 @@ class ExactSizeReader(object):
3232
def __init__(self, reader):
3333
self._reader = reader
3434

35+
@decorate(concatenate_bytes)
3536
def read(self, size=-1):
3637
"""Read from an underlying readable object.
3738
@@ -44,17 +45,16 @@ def read(self, size=-1):
4445
# Negative values read until EOF and 0 returns b''. Both remain unchanged.
4546
# Also a fast path when the requested amount of bytes is returned in one go.
4647
if size <= 0 or len(data) == size:
47-
return data
48-
# need an intermediary buffer
49-
out_len = len(data)
50-
data = six.BytesIO(data)
51-
while out_len < size:
52-
chunk = self._reader.read(size - out_len)
53-
if chunk == b"":
54-
raise EOFError("Not enough data to read.")
55-
data.write(chunk)
56-
out_len = out_len + len(chunk)
57-
return data.getvalue()
48+
yield data
49+
else:
50+
# need an intermediary buffer
51+
out_len = len(data)
52+
while out_len < size:
53+
chunk = self._reader.read(size - out_len)
54+
if chunk == b"":
55+
raise EOFError("Not enough data to read.")
56+
yield chunk
57+
out_len = out_len + len(chunk)
5858

5959

6060
class ZipCompressedReader(object):
@@ -70,6 +70,7 @@ def __init__(self, reader):
7070
self._compressor = zlib.decompressobj()
7171
self._reader = reader
7272

73+
@decorate(concatenate_bytes)
7374
def read(self, size=-1):
7475
"""Read from a compressed stream of bytes and return the inflated byte sequence.
7576
@@ -81,7 +82,7 @@ def read(self, size=-1):
8182
size = -1
8283
else:
8384
chunk_size = ZipCompressedReader.CHUNK_SIZE
84-
out_data = six.BytesIO()
85+
8586
out_len = 0
8687
while True:
8788
# first step find try to find some data to uncompress.
@@ -103,8 +104,7 @@ def read(self, size=-1):
103104
# increment output len.
104105
out_len = out_len + len(chunk)
105106
# write to buffer
106-
out_data.write(chunk)
107+
yield chunk
107108
# check requested size against output length.
108109
if size > 0 and out_len == size:
109110
break
110-
return out_data.getvalue()

0 commit comments

Comments
 (0)