base64io

Base64 stream with context manager support.

Classes

Base64IO(wrapped) Base64 stream with context manager support.
class base64io.Base64IO(wrapped)

Bases: io.IOBase

Base64 stream with context manager support.

Wraps a stream, base64-decoding read results before returning them and base64-encoding written bytes before writing them to the stream. 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

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()

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

Note

This does not close the wrapped stream.

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.

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 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=-1)

Read bytes from wrapped stream, base64-decoding before return.

Note

The number of bytes requested from the wrapped stream is adjusted to return the requested number of bytes after decoding returned 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.