Class PemReader


  • @Beta
    public final class PemReader
    extends java.lang.Object
    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.
    Since:
    1.14
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  PemReader.Section
      Section in the PEM file.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.util.regex.Pattern BEGIN_PATTERN  
      private static java.util.regex.Pattern END_PATTERN  
      private java.io.BufferedReader reader
      Reader.
    • Constructor Summary

      Constructors 
      Constructor Description
      PemReader​(java.io.Reader reader)  
    • Field Detail

      • BEGIN_PATTERN

        private static final java.util.regex.Pattern BEGIN_PATTERN
      • END_PATTERN

        private static final java.util.regex.Pattern END_PATTERN
      • reader

        private java.io.BufferedReader reader
        Reader.
    • Constructor Detail

      • PemReader

        public PemReader​(java.io.Reader reader)
        Parameters:
        reader - reader
    • Method Detail

      • readNextSection

        public PemReader.Section readNextSection()
                                          throws java.io.IOException
        Reads the next section in the PEM file or null for end of file.
        Throws:
        java.io.IOException
      • readNextSection

        public PemReader.Section readNextSection​(java.lang.String titleToLookFor)
                                          throws java.io.IOException
        Reads the next section in the PEM file, optionally based on a title to look for.
        Parameters:
        titleToLookFor - title to look for or null for any title
        Returns:
        next section or null for end of file
        Throws:
        java.io.IOException
      • readFirstSectionAndClose

        public static PemReader.Section readFirstSectionAndClose​(java.io.Reader reader)
                                                          throws java.io.IOException
        Reads the first section in the PEM file, and then closes the reader.
        Parameters:
        reader - reader
        Returns:
        first section found or null for none found
        Throws:
        java.io.IOException
      • readFirstSectionAndClose

        public static PemReader.Section readFirstSectionAndClose​(java.io.Reader reader,
                                                                 java.lang.String titleToLookFor)
                                                          throws java.io.IOException
        Reads the first section in the PEM file, optionally based on a title to look for, and then closes the reader.
        Parameters:
        titleToLookFor - title to look for or null for any title
        reader - reader
        Returns:
        first section found or null for none found
        Throws:
        java.io.IOException
      • close

        public void close()
                   throws java.io.IOException
        Closes the reader.

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

        Throws:
        java.io.IOException