Class PemReader (1.44.2)

public final class PemReader

Beta
PEM file reader.

Supports reading any PEM stream that contains Base64 encoded content stored inside "-----BEGIN ...-----" and "-----END ...-----" tags. Each call to #readNextSection() parses the next section in the PEM file. If you need a section of a certain title use #readNextSection(String), for example readNextSection("PRIVATE KEY"). To ensure that the stream is closed properly, call #close() in a finally block.

As a convenience, use #readFirstSectionAndClose(Reader) or #readFirstSectionAndClose(Reader, String) for the common case of only a single section in a PEM file (or only a single section of a given title).

Limitations:

  • Assumes the PEM file section content is not encrypted and cannot handle the case of any headers inside the BEGIN and END tag.
  • It also ignores any attributes associated with any PEM file section.

Inheritance

java.lang.Object > PemReader

Static Methods

readFirstSectionAndClose(Reader reader)

public static PemReader.Section readFirstSectionAndClose(Reader reader)

Reads the first section in the PEM file, and then closes the reader.

Parameter
Name Description
reader Reader

reader

Returns
Type Description
PemReader.Section

first section found or null for none found

Exceptions
Type Description
IOException

readFirstSectionAndClose(Reader reader, String titleToLookFor)

public static PemReader.Section readFirstSectionAndClose(Reader reader, String titleToLookFor)

Reads the first section in the PEM file, optionally based on a title to look for, and then closes the reader.

Parameters
Name Description
reader Reader

reader

titleToLookFor String

title to look for or null for any title

Returns
Type Description
PemReader.Section

first section found or null for none found

Exceptions
Type Description
IOException

Constructors

PemReader(Reader reader)

public PemReader(Reader reader)
Parameter
Name Description
reader Reader

reader

Methods

close()

public void close()

Closes the reader.

To ensure that the stream is closed properly, call #close() in a finally block.

Exceptions
Type Description
IOException

readNextSection()

public PemReader.Section readNextSection()

Reads the next section in the PEM file or null for end of file.

Returns
Type Description
PemReader.Section
Exceptions
Type Description
IOException

readNextSection(String titleToLookFor)

public PemReader.Section readNextSection(String titleToLookFor)

Reads the next section in the PEM file, optionally based on a title to look for.

Parameter
Name Description
titleToLookFor String

title to look for or null for any title

Returns
Type Description
PemReader.Section

next section or null for end of file

Exceptions
Type Description
IOException