PEM (Java SE 26 & JDK 26)
Description: declaration: module: java.base, package: java.security, record: PEM
Keywords: java.security.PEM class

# Record Class PEM

java.lang.Object
java.lang.Record
java.security.PEM

Record Components:
  `type` - the type identifier from the PEM header, without PEM syntax labels; for example, for a public key, `type` would be "PUBLIC KEY"
  `content` - the Base64-encoded data, excluding the PEM header and footer
  `leadingData` - any non-PEM data that precedes the PEM header during decoding. This value may be `null`.

All Implemented Interfaces:
  `DEREncodablePREVIEW`

public record PEM(String type, String content, byte[] leadingData) extends Record implements DEREncodablePREVIEW
`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.
`PEM` is a `DEREncodable`PREVIEW that represents Privacy-Enhanced Mail (PEM) data by its type and Base64-encoded content.

The `PEMDecoder.decode(String)`PREVIEW and `PEMDecoder.decode(InputStream)`PREVIEW methods return a `PEM` object when the data type cannot be represented by a cryptographic object. If you need access to the leading data of a PEM text, or want to handle the text content directly, use the decoding methods `PEMDecoder.decode(String, Class)`PREVIEW or `PEMDecoder.decode(InputStream, Class)`PREVIEW with `PEM.class` as an argument type.

A `PEM` object can be encoded back to its textual format by calling `toString()` or by using the encode methods in `PEMEncoder`PREVIEW.

When constructing a `PEM` instance, both `type` and `content` must not be `null`.

No validation is performed during instantiation to ensure that `type` conforms to RFC 7468 or other legacy formats, that `content` is valid Base64 data, or that `content` matches the `type`.

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.

`leadingData` is `null` if there is no data preceding the PEM header during decoding. `leadingData` can be useful for reading metadata that accompanies the PEM data. Because the value may represent a large amount of data, it is not defensively copied by the constructor, and the `leadingData()` method does not return a clone. Modification of the passed-in or returned array changes the value stored in this record.

Since:
  26
External Specifications
  - RFC 7468: Textual Encodings of PKIX, PKCS, and CMS Structures
See Also:
  - `PEMDecoder`PREVIEW
  - `PEMEncoder`PREVIEW

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `PEM(String type, String content)`
  Creates a `PEM` instance with the specified type and content.
  `PEM(String type, String content, byte[] leadingData)`
  Creates a `PEM` instance with the specified parameters.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `String`
  `content()`
  Returns the value of the `content` record component.
  `final byte[]`
  `decode()`
  Returns a Base64-decoded byte array of `content`, using `Base64.getMimeDecoder()`.
  `final boolean`
  `equals(Object o)`
  Indicates whether some other object is "equal to" this one.
  `final int`
  `hashCode()`
  Returns a hash code value for this object.
  `byte[]`
  `leadingData()`
  Returns the value of the `leadingData` record component.
  `final String`
  `toString()`
  Returns the PEM formatted string containing the `type` and Base64-encoded `content`.
  `String`
  `type()`
  Returns the value of the `type` record component.

  ### Methods declared in class Object

  `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`
  Modifier and Type
  Method
  Description
  `protected Object`
  `clone()`
  Creates and returns a copy of this object.
  `protected void`
  `finalize()`
  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<?>`
  `getClass()`
  Returns the runtime class of this `Object`.
  `final void`
  `notify()`
  Wakes up a single thread that is waiting on this object's monitor.
  `final void`
  `notifyAll()`
  Wakes up all threads that are waiting on this object's monitor.
  `final void`
  `wait()`
  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 content, byte[] leadingData)
    Creates a `PEM` instance with the specified parameters.
    Parameters:
      `type` - the PEM type identifier
      `content` - the Base64-encoded data, excluding the PEM header and footer
      `leadingData` - any non-PEM data read during the decoding process before the PEM header. This value may be `null`.
    Throws:
      `IllegalArgumentException` - if `type` is incorrectly formatted
      `NullPointerException` - if `type` or `content` is `null`

  - ### PEM

    public PEM(String type, String content)
    Creates a `PEM` instance with the specified type and content. This constructor sets `leadingData` to `null`.
    Parameters:
      `type` - the PEM type identifier
      `content` - the Base64-encoded data, excluding the PEM header and footer
    Throws:
      `IllegalArgumentException` - if `type` is incorrectly formatted
      `NullPointerException` - if `type` or `content` is `null`

- ## Method Details

  - ### toString

    public final String toString()
    Returns the PEM formatted string containing the `type` and Base64-encoded `content`. `leadingData` is not included.
    Specified by:
      `toString` in class `Record`
    Returns:
      the PEM text representation
    See Also:
      - `Object.toString()`

  - ### decode

    public final byte[] decode()
    Returns a Base64-decoded byte array of `content`, using `Base64.getMimeDecoder()`.
    Returns:
      a decoded byte array
    Throws:
      `IllegalArgumentException` - if decoding fails

  - ### hashCode

    public final int hashCode()
    Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
    Specified by:
      `hashCode` in class `Record`
    Returns:
      a hash code value for this object
    See Also:
      - `Object.hashCode()`

  - ### equals

    public final boolean equals(Object o)
    Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with `Objects::equals(Object,Object)`.
    Specified by:
      `equals` in class `Record`
    Parameters:
      `o` - the object with which to compare
    Returns:
      `true` if this object is the same as the `o` argument; `false` otherwise.
    See Also:
      - `Objects.equals(Object,Object)`

  - ### type

    public String type()
    Returns the value of the `type` record component.
    Returns:
      the value of the `type` record component

  - ### content

    public String content()
    Returns the value of the `content` record component.
    Returns:
      the value of the `content` record component

  - ### leadingData

    public byte[] leadingData()
    Returns the value of the `leadingData` record component.
    Returns:
      the value of the `leadingData` record component