| 1147 | |
| 1148 | |
| 1149 | class _BytesIOWrapper: |
| 1150 | # Wrapper that wraps a StringIO buffer and reads bytes from it |
| 1151 | # Created for compat with pyarrow read_csv |
| 1152 | def __init__(self, buffer: StringIO | TextIOBase, encoding: str = "utf-8") -> None: |
| 1153 | self.buffer = buffer |
| 1154 | self.encoding = encoding |
| 1155 | # Because a character can be represented by more than 1 byte, |
| 1156 | # it is possible that reading will produce more bytes than n |
| 1157 | # We store the extra bytes in this overflow variable, and append the |
| 1158 | # overflow to the front of the bytestring the next time reading is performed |
| 1159 | self.overflow = b"" |
| 1160 | |
| 1161 | def __getattr__(self, attr: str) -> Any: |
| 1162 | return getattr(self.buffer, attr) |
| 1163 | |
| 1164 | def read(self, n: int | None = -1) -> bytes: |
| 1165 | assert self.buffer is not None |
| 1166 | bytestring = self.buffer.read(n).encode(self.encoding) |
| 1167 | # When n=-1/n greater than remaining bytes: Read entire file/rest of file |
| 1168 | combined_bytestring = self.overflow + bytestring |
| 1169 | if n is None or n < 0 or n >= len(combined_bytestring): |
| 1170 | self.overflow = b"" |
| 1171 | return combined_bytestring |
| 1172 | else: |
| 1173 | to_return = combined_bytestring[:n] |
| 1174 | self.overflow = combined_bytestring[n:] |
| 1175 | return to_return |
| 1176 | |
| 1177 | |
| 1178 | def _maybe_memory_map( |