base64io

Base64 stream context manager.

Classes

Base64IO(wrapped[, close_wrapped_on_close]) Wraps a stream, base64-decoding read results before returning them and base64-encoding written bytes before writing them to the stream.
class base64io.Base64IO(wrapped, close_wrapped_on_close=False)

Bases: io.IOBase

Wraps a stream, base64-decoding read results before returning them and base64-encoding written bytes before writing them to the stream. Unless close_wrapped_on_close is set to True, the underlying stream is not closed when this object is closed. Instances of this class are not reusable in order maintain consistency with the io.IOBase behavior on close().

Note

Provides iterator and context manager interfaces.

Warning

Because up to two bytes of data must be buffered to ensure correct base64 encoding of all data written, this object must be closed after you are done writing to avoid data loss. If used as a context manager, we take care of that for you.

Parameters:
  • wrapped – Stream to wrap
  • close_wrapped_on_close (bool) – Should the wrapped stream be closed when this object is closed (default: False)

Check for required methods on wrapped stream and set up read buffer.

Raises:TypeError – if wrapped does not have attributes needed to determine the stream’s state
close()

Closes this stream, encoding and writing any buffered bytes is present.

Note

This does not close the wrapped stream unless otherwise specified when this object was created.

writable()

Determine if the stream can be written to. Delegates to wrapped stream when possible. Otherwise returns False.

Return type:bool
readable()

Determine if the stream can be read from. Delegates to wrapped stream when possible. Otherwise returns False.

Return type:bool
flush()

Flush the write buffer of the wrapped stream.

write(b)

Base64-encode the bytes and write them to the wrapped stream, buffering any bytes that would require padding for the next write call.

Warning

Because up to two bytes of data must be buffered to ensure correct base64 encoding of all data written, this object must be closed after you are done writing to avoid data loss. If used as a context manager, we take care of that for you.

Parameters:

b (bytes) – Bytes to write to wrapped stream

Raises:
  • ValueError – if called on closed Base64IO object
  • IOError – if underlying stream is not writable
writelines(lines)

Write a list of lines.

Parameters:lines (list) – Lines to write
read(b=None)

Read bytes from wrapped stream, base64-decoding before return, and adjusting read from wrapped stream to return correct number of bytes.

Parameters:b (int) – Number of bytes to read
Returns:Decoded bytes from wrapped stream
Return type:bytes
readline(limit=-1)

Read and return one line from the stream. If limit is specified, at most limit bytes will be read.

Note

Because the source that this reads from may not contain any OEL characters, we read “lines” in chunks of length io.DEFAULT_BUFFER_SIZE.

Return type:bytes
readlines(hint=-1)

Read and return a list of lines from the stream. hint can be specified to control the number of lines read: no more lines will be read if the total size (in bytes/ characters) of all lines so far exceeds hint.

Returns:Lines of data
Return type:list of bytes
next()

Python 2 iterator hook.