mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2024-11-25 10:53:44 +03:00
Slightly simplify BinaryReader
There was no need for the BufferedReader, since everything is already in memory. Further, the stream parameter was never used, so it was also unnecessary. The check for None when reading length was also unnecessary, since we could just pass -1 to begin with.
This commit is contained in:
parent
45d82f2a85
commit
c0e4d6c8b6
|
@ -2,10 +2,10 @@
|
||||||
This module contains the BinaryReader utility class.
|
This module contains the BinaryReader utility class.
|
||||||
"""
|
"""
|
||||||
import os
|
import os
|
||||||
from datetime import datetime, timezone, timedelta
|
|
||||||
from io import BufferedReader, BytesIO
|
|
||||||
from struct import unpack
|
|
||||||
import time
|
import time
|
||||||
|
from datetime import datetime, timezone, timedelta
|
||||||
|
from io import BytesIO
|
||||||
|
from struct import unpack
|
||||||
|
|
||||||
from ..errors import TypeNotFoundError
|
from ..errors import TypeNotFoundError
|
||||||
from ..tl.alltlobjects import tlobjects
|
from ..tl.alltlobjects import tlobjects
|
||||||
|
@ -18,18 +18,10 @@ _EPOCH = _EPOCH_NAIVE.replace(tzinfo=timezone.utc)
|
||||||
class BinaryReader:
|
class BinaryReader:
|
||||||
"""
|
"""
|
||||||
Small utility class to read binary data.
|
Small utility class to read binary data.
|
||||||
Also creates a "Memory Stream" if necessary
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, data=None, stream=None):
|
def __init__(self, data):
|
||||||
if data:
|
|
||||||
self.stream = BytesIO(data)
|
self.stream = BytesIO(data)
|
||||||
elif stream:
|
|
||||||
self.stream = stream
|
|
||||||
else:
|
|
||||||
raise ValueError('Either bytes or a stream must be provided')
|
|
||||||
|
|
||||||
self.reader = BufferedReader(self.stream)
|
|
||||||
self._last = None # Should come in handy to spot -404 errors
|
self._last = None # Should come in handy to spot -404 errors
|
||||||
|
|
||||||
# region Reading
|
# region Reading
|
||||||
|
@ -61,13 +53,10 @@ class BinaryReader:
|
||||||
return int.from_bytes(
|
return int.from_bytes(
|
||||||
self.read(bits // 8), byteorder='little', signed=signed)
|
self.read(bits // 8), byteorder='little', signed=signed)
|
||||||
|
|
||||||
def read(self, length=None):
|
def read(self, length=-1):
|
||||||
"""Read the given amount of bytes."""
|
"""Read the given amount of bytes, or -1 to read all remaining."""
|
||||||
if length is None:
|
result = self.stream.read(length)
|
||||||
return self.reader.read()
|
if (length >= 0) and (len(result) != length):
|
||||||
|
|
||||||
result = self.reader.read(length)
|
|
||||||
if len(result) != length:
|
|
||||||
raise BufferError(
|
raise BufferError(
|
||||||
'No more data left to read (need {}, got {}: {}); last read {}'
|
'No more data left to read (need {}, got {}: {}); last read {}'
|
||||||
.format(length, len(result), repr(result), repr(self._last))
|
.format(length, len(result), repr(result), repr(self._last))
|
||||||
|
@ -164,24 +153,24 @@ class BinaryReader:
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
"""Closes the reader, freeing the BytesIO stream."""
|
"""Closes the reader, freeing the BytesIO stream."""
|
||||||
self.reader.close()
|
self.stream.close()
|
||||||
|
|
||||||
# region Position related
|
# region Position related
|
||||||
|
|
||||||
def tell_position(self):
|
def tell_position(self):
|
||||||
"""Tells the current position on the stream."""
|
"""Tells the current position on the stream."""
|
||||||
return self.reader.tell()
|
return self.stream.tell()
|
||||||
|
|
||||||
def set_position(self, position):
|
def set_position(self, position):
|
||||||
"""Sets the current position on the stream."""
|
"""Sets the current position on the stream."""
|
||||||
self.reader.seek(position)
|
self.stream.seek(position)
|
||||||
|
|
||||||
def seek(self, offset):
|
def seek(self, offset):
|
||||||
"""
|
"""
|
||||||
Seeks the stream position given an offset from the current position.
|
Seeks the stream position given an offset from the current position.
|
||||||
The offset may be negative.
|
The offset may be negative.
|
||||||
"""
|
"""
|
||||||
self.reader.seek(offset, os.SEEK_CUR)
|
self.stream.seek(offset, os.SEEK_CUR)
|
||||||
|
|
||||||
# endregion
|
# endregion
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user