Skip to content

Commit

Permalink
Merge pull request andreafabrizi#16 from moshekaplan/patch-1
Browse files Browse the repository at this point in the history
Performance improvement in Stream handling
  • Loading branch information
andreafabrizi committed Oct 8, 2014
2 parents f83f8ab + dd547cd commit 813767c
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions lib/Stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,30 @@ def __init__(self, stream = ""):
self.offset = 0
self.size = len(stream)
self.dontScanAgain = False
self.unmergedData = []

#Appends new data to the stream
#Appends new data to the buffer
def appendData(self, data):
self.stream +=data
self.unmergedData.append(data)
self.size +=len(data)

#Merges the buffered data into the stream
def _mergeData(self):
if self.unmergedData:
self.stream += ''.join(self.unmergedData)
self.unmergedData = []

#Prints the stream
def dump(self):
self._mergeData()
hexdump(self.stream[self.offset:])

#Gets n bytes from the buffer and increments the offset
#Gets n bytes from the stream and increments the offset
def getBytes(self, n):
if self.offset + n> self.size:
raise StreamNoMoreBytes

self._mergeData()
bytes = self.stream[self.offset:self.offset+n]
self.offset = self.offset + n
return bytes
Expand All @@ -52,11 +61,12 @@ def getBytes(self, n):
def getByte(self):
return ord(self.getBytes(1))

#Reads n bytes from the buffer without increments the offset
#Reads n bytes from the stream without incrementing the offset
def readBytes(self, n):
if self.offset >= self.size:
return None

self._mergeData()
bytes = self.stream[self.offset:self.offset+n]
return bytes

Expand Down

0 comments on commit 813767c

Please sign in to comment.