Convert object to seekable byte-stream. Parameters ---------- substrate: :py:class:`bytes` or :py:class:`io.IOBase` or :py:class:`univ.OctetString` Returns ------- : :py:class:`io.IOBase` Raises ------ : :py:class:`~pyasn1.error.PyAsn1Error` If the supp
(substrate)
| 79 | |
| 80 | |
| 81 | def asSeekableStream(substrate): |
| 82 | """Convert object to seekable byte-stream. |
| 83 | |
| 84 | Parameters |
| 85 | ---------- |
| 86 | substrate: :py:class:`bytes` or :py:class:`io.IOBase` or :py:class:`univ.OctetString` |
| 87 | |
| 88 | Returns |
| 89 | ------- |
| 90 | : :py:class:`io.IOBase` |
| 91 | |
| 92 | Raises |
| 93 | ------ |
| 94 | : :py:class:`~pyasn1.error.PyAsn1Error` |
| 95 | If the supplied substrate cannot be converted to a seekable stream. |
| 96 | """ |
| 97 | if isinstance(substrate, io.BytesIO): |
| 98 | return substrate |
| 99 | |
| 100 | elif isinstance(substrate, bytes): |
| 101 | return io.BytesIO(substrate) |
| 102 | |
| 103 | elif isinstance(substrate, univ.OctetString): |
| 104 | return io.BytesIO(substrate.asOctets()) |
| 105 | |
| 106 | try: |
| 107 | if substrate.seekable(): # Will fail for most invalid types |
| 108 | return substrate |
| 109 | else: |
| 110 | return CachingStreamWrapper(substrate) |
| 111 | |
| 112 | except AttributeError: |
| 113 | raise error.UnsupportedSubstrateError( |
| 114 | "Cannot convert " + substrate.__class__.__name__ + |
| 115 | " to a seekable bit stream.") |
| 116 | |
| 117 | |
| 118 | def isEndOfStream(substrate): |
no test coverage detected