decodeToSequence

Lazily decodes all PEM documents found in text

The input should be in the form:

-----BEGIN {LABEL1}-----
Base64-encoded {content} with line breaks every 64 characters
-----END {LABEL1}-----

-----BEGIN {LABEL2}-----
Base64-encoded {content} with line breaks every 64 characters
-----END {LABEL2}-----

Any content before the first -----BEGIN {label}----- and after the last -----END {label}----- is ignored, as well as any content after each document and before the next -----BEGIN {label}-----. The sequence yields each discovered PemDocument in order

Return

a sequence of decoded PemDocuments, empty sequence if no PEM documents present

Parameters

text

the textual input that may contain multiple PEM documents

Throws

if the PEM encoding of any document is invalid

Samples

val pem: String = """
-----BEGIN A-----
YQ==
-----END A-----

-----BEGIN B-----
Yg==
-----END B-----

""".trimIndent()

val documents = PemDocument.decodeToSequence(pem).toList()

assertEquals(
    expected = listOf(
        PemDocument(PemLabel("A"), "a".encodeToByteString()),
        PemDocument(PemLabel("B"), "b".encodeToByteString()),
    ),
    actual = documents,
)

Lazily decodes all PEM documents found in bytes. The bytes are treated as an encoded string

The input should be in the form:

-----BEGIN {LABEL1}-----
Base64-encoded {content} with line breaks every 64 characters
-----END {LABEL1}-----

-----BEGIN {LABEL2}-----
Base64-encoded {content} with line breaks every 64 characters
-----END {LABEL2}-----

Any content before the first -----BEGIN {label}----- and after the last -----END {label}----- is ignored, as well as any content after each document and before the next -----BEGIN {label}-----. The sequence yields each discovered PemDocument in order

Return

a sequence of decoded PemDocuments, empty sequence if no PEM documents present

Parameters

bytes

the byte array that may contain multiple PEM documents

Throws

if the PEM encoding of any document is invalid

Samples

val pem: ByteArray = """
-----BEGIN A-----
YQ==
-----END A-----

-----BEGIN B-----
Yg==
-----END B-----

""".trimIndent().encodeToByteArray()

val documents = PemDocument.decodeToSequence(pem).toList()

assertEquals(
    expected = listOf(
        PemDocument(PemLabel("A"), "a".encodeToByteString()),
        PemDocument(PemLabel("B"), "b".encodeToByteString()),
    ),
    actual = documents,
)

Lazily decodes all PEM documents found in bytes. The bytes are treated as an encoded string

The input should be in the form:

-----BEGIN {LABEL1}-----
Base64-encoded {content} with line breaks every 64 characters
-----END {LABEL1}-----

-----BEGIN {LABEL2}-----
Base64-encoded {content} with line breaks every 64 characters
-----END {LABEL2}-----

Any content before the first -----BEGIN {label}----- and after the last -----END {label}----- is ignored, as well as any content after each document and before the next -----BEGIN {label}-----. The sequence yields each discovered PemDocument in order

Return

a sequence of decoded PemDocuments, empty sequence if no PEM documents present

Parameters

bytes

the byte array that may contain multiple PEM documents

Throws

if the PEM encoding of any document is invalid

Samples

val pem: ByteString = """
-----BEGIN A-----
YQ==
-----END A-----

-----BEGIN B-----
Yg==
-----END B-----

""".trimIndent().encodeToByteString()

val documents = PemDocument.decodeToSequence(pem).toList()

assertEquals(
    expected = listOf(
        PemDocument(PemLabel("A"), "a".encodeToByteString()),
        PemDocument(PemLabel("B"), "b".encodeToByteString()),
    ),
    actual = documents,
)

Lazily decodes all PEM documents found in source. The source is treated as an encoded string and is consumed up to and including the last decoded document, which was consumed from the sequence

The input should be in the form:

-----BEGIN {LABEL1}-----
Base64-encoded {content} with line breaks every 64 characters
-----END {LABEL1}-----

-----BEGIN {LABEL2}-----
Base64-encoded {content} with line breaks every 64 characters
-----END {LABEL2}-----

Any content before the first -----BEGIN {label}----- and after the last -----END {label}----- is ignored, as well as any content after each document and before the next -----BEGIN {label}-----. The sequence yields each discovered PemDocument in order

Return

a sequence of decoded PemDocuments, empty sequence if no PEM documents present

Parameters

source

the source to read from

Throws

if the PEM encoding of any document is invalid

Samples

val pem = """
-----BEGIN A-----
YQ==
-----END A-----

-----BEGIN B-----
Yg==
-----END B-----

""".trimIndent()

val buffer = Buffer()
buffer.writeString(pem)

val documents = PemDocument.decodeToSequence(buffer).toList()

assertEquals(
    expected = listOf(
        PemDocument(PemLabel("A"), "a".encodeToByteString()),
        PemDocument(PemLabel("B"), "b".encodeToByteString()),
    ),
    actual = documents,
)