(
&self,
length: OptionalArg<c_int>,
vm: &VirtualMachine,
)
| 2365 | |
| 2366 | #[pymethod] |
| 2367 | fn read( |
| 2368 | &self, |
| 2369 | length: OptionalArg<c_int>, |
| 2370 | vm: &VirtualMachine, |
| 2371 | ) -> PyResult<PyRef<PyBytes>> { |
| 2372 | self.ensure_connection_open(vm)?; |
| 2373 | |
| 2374 | let mut length = length.unwrap_or(-1); |
| 2375 | let mut inner = self.inner(vm)?; |
| 2376 | let blob_len = inner.blob.bytes(); |
| 2377 | let max_read = blob_len - inner.offset; |
| 2378 | |
| 2379 | if length < 0 || length > max_read { |
| 2380 | length = max_read; |
| 2381 | } |
| 2382 | |
| 2383 | if length == 0 { |
| 2384 | Ok(vm.ctx.empty_bytes.clone()) |
| 2385 | } else { |
| 2386 | let mut buf = Vec::<u8>::with_capacity(length as usize); |
| 2387 | let ret = inner |
| 2388 | .blob |
| 2389 | .read(buf.as_mut_ptr().cast(), length, inner.offset); |
| 2390 | self.check(ret, vm)?; |
| 2391 | unsafe { buf.set_len(length as usize) }; |
| 2392 | inner.offset += length; |
| 2393 | Ok(vm.ctx.new_bytes(buf)) |
| 2394 | } |
| 2395 | } |
| 2396 | |
| 2397 | #[pymethod] |
| 2398 | fn write(&self, data: PyBuffer, vm: &VirtualMachine) -> PyResult<()> { |
no test coverage detected