diff --git a/Bass4Py/bass/stream.pyx b/Bass4Py/bass/stream.pyx index 8b4898a7..4602b456 100644 --- a/Bass4Py/bass/stream.pyx +++ b/Bass4Py/bass/stream.pyx @@ -1,5 +1,4 @@ from libc.string cimport memmove - from ..bindings.bass cimport ( _BASS_ATTRIB_BITRATE, _BASS_ATTRIB_NET_RESUME, @@ -97,18 +96,15 @@ cdef QWORD __stdcall CFILELENPROC_STD(void *user) with gil: cdef DWORD CFILEREADPROC(void *buffer, DWORD length, void *user) with gil: cdef Stream strm = user - cdef bytes data cdef DWORD blen - + cdef unsigned char[:] buffer_memoryview = buffer # make memoryview, using buffer as the memory + cdef const unsigned char[:] bytes_memoryview # if readinto() is not available, we'll put the result of read() in this variable and copy the data try: - data = strm._file.read(length) - blen = len(data) - - if blen > length: - data = data[:length] - blen = length - - memmove(buffer, data, blen) + if hasattr(strm._file, "readinto"): blen = strm._file.readinto(buffer_memoryview) # read data directly from file into the buffer + elif hasattr(strm._file, "read"): + bytes_memoryview = strm._file.read(length) + len = bytes_memoryview.shape[0] # we might not've gotten as much data as requested + buffer_memoryview[:len] = bytes_memoryview[:len] # let Cython copy the data for us return blen except Exception: warnings.warn(traceback.format_exc(), RuntimeWarning)