Commit 3ead886f by devttys0

Consolidated BlockFile code.

parent ef653b30
...@@ -336,6 +336,7 @@ class BlockFile(BLOCK_FILE_PARENT_CLASS): ...@@ -336,6 +336,7 @@ class BlockFile(BLOCK_FILE_PARENT_CLASS):
def read(self, n=-1): def read(self, n=-1):
'''' ''''
Reads up to n bytes of data (or to EOF if n is not specified). Reads up to n bytes of data (or to EOF if n is not specified).
Will not read more than self.length bytes.
io.FileIO.read does not guaruntee that all requested data will be read; io.FileIO.read does not guaruntee that all requested data will be read;
this method overrides io.FileIO.read and does guaruntee that all data will be read. this method overrides io.FileIO.read and does guaruntee that all data will be read.
...@@ -345,6 +346,11 @@ class BlockFile(BLOCK_FILE_PARENT_CLASS): ...@@ -345,6 +346,11 @@ class BlockFile(BLOCK_FILE_PARENT_CLASS):
l = 0 l = 0
data = b'' data = b''
if self.total_read < self.length:
# Don't read more than self.length bytes from the file
if (self.total_read + n) > self.length:
n = self.length - self.total_read
while n < 0 or l < n: while n < 0 or l < n:
tmp = super(self.__class__, self).read(n-l) tmp = super(self.__class__, self).read(n-l)
if tmp: if tmp:
...@@ -354,6 +360,7 @@ class BlockFile(BLOCK_FILE_PARENT_CLASS): ...@@ -354,6 +360,7 @@ class BlockFile(BLOCK_FILE_PARENT_CLASS):
break break
self.total_read += len(data) self.total_read += len(data)
return self._swap_data_block(bytes2str(data)) return self._swap_data_block(bytes2str(data))
def _internal_read(self, n=-1): def _internal_read(self, n=-1):
...@@ -383,31 +390,26 @@ class BlockFile(BLOCK_FILE_PARENT_CLASS): ...@@ -383,31 +390,26 @@ class BlockFile(BLOCK_FILE_PARENT_CLASS):
''' '''
dlen = 0 dlen = 0
data = None data = None
if self.total_read < self.length:
# Read in self.READ_BLOCK_SIZE + self.MAX_TRAILING_SIZE, unless this exceeds self.length
rsize = self.READ_BLOCK_SIZE + self.MAX_TRAILING_SIZE rsize = self.READ_BLOCK_SIZE + self.MAX_TRAILING_SIZE
if (self.total_read + rsize) > self.length:
rsize = self.length - self.total_read
# Do the read. Must use self._internal_read so that the total_read value is untouched (we update this ourselves later) # Do the read. Must use self._internal_read so that the total_read value is untouched (we update this ourselves later)
data = self._internal_read(rsize) data = self._internal_read(rsize)
if data and data is not None: if data:
# Get the actual length of the read in data # Get the actual length of the read in data
dlen = len(data) dlen = len(data)
# Calculate how far back we need to seek to pick up at the self.READ_BLOCK_SIZE offset # Calculate how far back we need to seek to pick up at the self.READ_BLOCK_SIZE offset
seek_offset = dlen - self.READ_BLOCK_SIZE seek_offset = dlen - self.READ_BLOCK_SIZE
# If rsize was less than self.READ_BLOCK_SIZE seek backwards zero bytes # If the actual read size was less than self.READ_BLOCK_SIZE seek backwards zero bytes
if seek_offset < 0: if seek_offset < 0:
seek_offset = 0 seek_offset = 0
# Read in READ_BLOCK_SIZE plus MAX_TRAILING_SIZE bytes, but return a max dlen value # Read in READ_BLOCK_SIZE plus MAX_TRAILING_SIZE bytes, but return a max dlen value
# Return a max dlen value of READ_BLOCK_SIZE. This ensures that there is a MAX_TRAILING_SIZE # Return a max dlen value of READ_BLOCK_SIZE. This ensures that there is a MAX_TRAILING_SIZE
# buffer at the end of the returned data in case a signature is found at or near data[READ_BLOCK_SIZE]. # buffer at the end of the returned data in case a signature is found at or near data[READ_BLOCK_SIZE].
if dlen == (self.READ_BLOCK_SIZE + self.MAX_TRAILING_SIZE): if dlen == rsize:
dlen = self.READ_BLOCK_SIZE dlen = self.READ_BLOCK_SIZE
# Seek to the self.total_read offset so the next read can pick up where this one left off. # Seek to the self.total_read offset so the next read can pick up where this one left off.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment