/
bytestream.py
76 lines (64 loc) · 2.44 KB
/
bytestream.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""
PC-BASIC - bytestream.py
StringIO-like wrapper for bytearray
(c) 2013, 2014, 2015, 2016 Rob Hagemans
This file is released under the GNU GPL version 3 or later.
"""
class ByteStream(object):
""" StringIO-like wrapper for bytearray. """
def __init__(self, contents=''):
""" Create e new ByteStream. """
self.setvalue(contents)
def setvalue(self, contents=''):
""" Assign a bytearray s, move location to 0. This does not create a copy, changes affect the original bytearray. """
self._contents = contents
self._loc = 0
def getvalue(self):
""" Retrieve the bytearray. changes will affect the bytestream. """
return self._contents
def tell(self):
""" Get the current location. """
return self._loc
def seek(self, n_bytes, from_where=0):
""" Move loc by n bytes from start(w=0), current(w=1) or end(w=2). """
if from_where == 0:
self._loc = n_bytes
elif from_where == 1:
self._loc += n_bytes
elif from_where == 2:
self._loc = len(self._contents)-n_bytes
if self._loc < 0:
self._loc = 0
elif self._loc > len(self._contents):
self._loc = len(self._contents)
def read(self, n_bytes=None):
""" Get an n-length string and move the location n forward. If loc>len, return empty string. """
if n_bytes is None:
n_bytes = len(self._contents) - self._loc
if self._loc >= len(self._contents):
self._loc = len(self._contents)
return ''
peeked = self._contents[self._loc:self._loc+n_bytes]
self._loc += len(peeked)
return peeked
def write(self, substr):
""" Write a str or bytearray or char s to the current location. Overwrite, do not insert. """
if self._loc >= len(self._contents):
self._contents += substr
self._loc = len(self._contents)
else:
self._contents[self._loc:self._loc+len(substr)] = substr
self._loc += len(substr)
def truncate(self, n=None):
""" Clip off the bytearray after position n. """
if n is None:
n = self._loc
self._contents = self._contents[:n]
if self._loc >= len(self._contents):
self._loc = len(self._contents)
def close(self):
""" Close the stream. """
pass
def flush(self):
""" Flush the stream. """
pass