Base64-encode the bytes and write them to the wrapped stream. Any bytes that would require padding for the next write call are buffered until the next write or close. .. warning:: Because up to two bytes of data must be buffered to ensure correct base64 encodin
(self, b)
| 190 | return self.__wrapped.flush() |
| 191 | |
| 192 | def write(self, b): |
| 193 | # type: (bytes) -> int |
| 194 | """Base64-encode the bytes and write them to the wrapped stream. |
| 195 | |
| 196 | Any bytes that would require padding for the next write call are buffered until the |
| 197 | next write or close. |
| 198 | |
| 199 | .. warning:: |
| 200 | |
| 201 | Because up to two bytes of data must be buffered to ensure correct base64 encoding |
| 202 | of all data written, this object **must** be closed after you are done writing to |
| 203 | avoid data loss. If used as a context manager, we take care of that for you. |
| 204 | |
| 205 | :param bytes b: Bytes to write to wrapped stream |
| 206 | :raises ValueError: if called on closed Base64IO object |
| 207 | :raises IOError: if underlying stream is not writable |
| 208 | """ |
| 209 | if self.closed: |
| 210 | raise ValueError("I/O operation on closed file.") |
| 211 | |
| 212 | if not self.writable(): |
| 213 | raise IOError("Stream is not writable") |
| 214 | |
| 215 | # Load any stashed bytes and clear the buffer |
| 216 | _bytes_to_write = self.__write_buffer + b |
| 217 | self.__write_buffer = b"" |
| 218 | |
| 219 | # If an even base64 chunk or finalizing the stream, write through. |
| 220 | if len(_bytes_to_write) % 3 == 0: |
| 221 | return self.__wrapped.write(base64.b64encode(_bytes_to_write)) |
| 222 | |
| 223 | # We're not finalizing the stream, so stash the trailing bytes and encode the rest. |
| 224 | trailing_byte_pos = -1 * (len(_bytes_to_write) % 3) |
| 225 | self.__write_buffer = _bytes_to_write[trailing_byte_pos:] |
| 226 | return self.__wrapped.write(base64.b64encode(_bytes_to_write[:trailing_byte_pos])) |
| 227 | |
| 228 | def writelines(self, lines): |
| 229 | # type: (Iterable[bytes]) -> None |
no test coverage detected