Class PEM

java.lang.Object
java.security.PEM
All Implemented Interfaces:
BinaryEncodablePREVIEW

public final class PEM extends Object implements BinaryEncodablePREVIEW
PEM is a preview API of the Java platform.
Programs can only use PEM when preview features are enabled.
Preview features may be removed in a future release, or upgraded to permanent features of the Java platform.
A BinaryEncodablePREVIEW representing a Privacy-Enhanced Mail (PEM) structure composed of a type identifier, Base64-encoded content, and optional leading data that precedes the PEM header.

The type is the label in the PEM header, following the BEGIN keyword and excluding the encapsulation boundaries. Common type values include, but are not limited to: CERTIFICATE, CERTIFICATE REQUEST, ATTRIBUTE CERTIFICATE, X509 CRL, PKCS7, CMS, PRIVATE KEY, ENCRYPTED PRIVATE KEY, and PUBLIC KEY.

Instances of this class are returned by PEMDecoder.decode(String)PREVIEW and PEMDecoder.decode(InputStream)PREVIEW when the content cannot be represented as a cryptographic object. To explicitly retrieve a PEM instance with access to the leading data, use PEMDecoder.decode(String, Class)PREVIEW or PEMDecoder.decode(InputStream, Class)PREVIEW with PEM.class as the type.

A PEM object can be encoded to its textual representation by invoking toString() or by using PEMEncoderPREVIEW.

To construct a PEM instance, type and base64Content must be non-null. For constructors that accept leadingData, it must also be non-null.

No validation is performed to ensure that the type conforms to RFC 7468 or legacy formats, or that the content corresponds to the declared type.

Since:
26
External Specifications
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    PEM(String type, byte[] base64Content)
    Creates a PEM instance with the specified type and Base64-encoded content byte array.
    PEM(String type, byte[] base64Content, byte[] leadingData)
    Creates a PEM instance with the specified type and Base64-encoded content and leading data as byte arrays.
    PEM(String type, String base64Content)
    Creates a PEM instance with the specified type and Base64-encoded content string.
    PEM(String type, String base64Content, byte[] leadingData)
    Creates a PEM instance with the specified type, Base64-encoded content string, and leading data byte array.
  • Method Summary

    Modifier and Type
    Method
    Description
    byte[]
    Returns the Base64-encoded content.
    byte[]
    Returns the Base64-decoded content as a byte array, using Base64.getMimeDecoder().
    byte[]
    Returns the leading data that preceded the PEM header in the decoded input.
    Returns a PEM string representation of this object, using type for the header and footer lines and content for the Base64 body.
    Returns the PEM type identifier.

    Methods declared in class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    Modifier and Type
    Method
    Description
    protected Object
    Creates and returns a copy of this object.
    boolean
    Indicates whether some other object is "equal to" this one.
    protected void
    Deprecated, for removal: This API element is subject to removal in a future version.
    Finalization is deprecated and subject to removal in a future release.
    final Class<?>
    Returns the runtime class of this Object.
    int
    Returns a hash code value for this object.
    final void
    Wakes up a single thread that is waiting on this object's monitor.
    final void
    Wakes up all threads that are waiting on this object's monitor.
    final void
    Causes the current thread to wait until it is awakened, typically by being notified or interrupted.
    final void
    wait(long timeoutMillis)
    Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.
    final void
    wait(long timeoutMillis, int nanos)
    Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.
  • Constructor Details

    • PEM

      public PEM(String type, String base64Content, byte[] leadingData)
      Creates a PEM instance with the specified type, Base64-encoded content string, and leading data byte array.
      Parameters:
      type - the PEM type identifier; must not contain PEM encapsulation syntax
      base64Content - the Base64-encoded content, excluding the PEM header and footer
      leadingData - data that precedes the PEM header. This array is defensively copied.
      Throws:
      IllegalArgumentException - if type contains PEM encapsulation syntax
      NullPointerException - if any parameter is null
    • PEM

      public PEM(String type, String base64Content)
      Creates a PEM instance with the specified type and Base64-encoded content string.
      Parameters:
      type - the PEM type identifier; must not contain PEM encapsulation syntax
      base64Content - the Base64-encoded content, excluding the PEM header and footer
      Throws:
      IllegalArgumentException - if type contains PEM encapsulation syntax
      NullPointerException - if any parameter is null
    • PEM

      public PEM(String type, byte[] base64Content, byte[] leadingData)
      Creates a PEM instance with the specified type and Base64-encoded content and leading data as byte arrays.
      Parameters:
      type - the PEM type identifier; must not contain PEM encapsulation syntax
      base64Content - the Base64-encoded content, excluding the PEM header and footer. This array is defensively copied.
      leadingData - data that precedes the PEM header. This array is defensively copied.
      Throws:
      IllegalArgumentException - if type contains PEM encapsulation syntax
      NullPointerException - if any parameter is null
      Since:
      27
    • PEM

      public PEM(String type, byte[] base64Content)
      Creates a PEM instance with the specified type and Base64-encoded content byte array.
      Parameters:
      type - the PEM type identifier; must not contain PEM encapsulation syntax
      base64Content - the Base64-encoded content, excluding the PEM header and footer. This array is defensively copied.
      Throws:
      IllegalArgumentException - if type contains PEM encapsulation syntax
      NullPointerException - if any parameter is null
      Since:
      27
  • Method Details

    • type

      public String type()
      Returns the PEM type identifier.
      Returns:
      the PEM type identifier
    • leadingData

      public byte[] leadingData()
      Returns the leading data that preceded the PEM header in the decoded input.
      Returns:
      a newly-allocated byte array containing leading data, or null if no leading data is present
    • content

      public byte[] content()
      Returns the Base64-encoded content.
      Returns:
      a newly-allocated byte array containing the Base64 content
      Since:
      27
    • decode

      public byte[] decode()
      Returns the Base64-decoded content as a byte array, using Base64.getMimeDecoder().
      Returns:
      a newly-allocated byte array containing the decoded content
      Throws:
      IllegalArgumentException - if decoding fails
    • toString

      public String toString()
      Returns a PEM string representation of this object, using type for the header and footer lines and content for the Base64 body.
      Overrides:
      toString in class Object
      Returns:
      the PEM-formatted string