APIdia Launch Partner
Top Description Fields Constructors Methods
java.security

public final Class PEM

extends Object
implements BinaryEncodable
Class Inheritance
All Implemented Interfaces
java.security.BinaryEncodable
Annotations
@PreviewFeature
feature:PEM_API
Imports
jdk.internal.javac.PreviewFeature, jdk.internal.ref.CleanerFactory, sun.security.util.KeyUtil, .Pem, java.io.InputStream, java.lang.ref.Reference, java.nio.charset.StandardCharsets, java.util.Base64, .Objects

Preview

Third Preview of PEM Encodings of Cryptographic Objects (JEP 538).

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 BinaryEncodable 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) and PEMDecoder#decode(InputStream) 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) or PEMDecoder#decode(InputStream, Class) with PEM.class as the type.

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

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 Specification
https://www.rfc-editor.org/info/rfc7468
See Also
PEMDecoder, PEMEncoder

Field Summary

Modifier and TypeField and Description
private final byte[]
private byte[]
private final String

Constructor Summary

AccessConstructor and Description
public
PEM(String
the PEM type identifier; must not contain PEM encapsulation syntax
type
,
String
the Base64-encoded content, excluding the PEM header and footer
base64Content
,
byte[]
data that precedes the PEM header. This array is defensively copied.
leadingData
)

Creates a PEM instance with the specified type, Base64-encoded content string, and leading data byte array.

public
PEM(String
the PEM type identifier; must not contain PEM encapsulation syntax
type
,
String
the Base64-encoded content, excluding the PEM header and footer
base64Content
)

Creates a PEM instance with the specified type and Base64-encoded content string.

public
PEM(String
the PEM type identifier; must not contain PEM encapsulation syntax
type
,
byte[]
the Base64-encoded content, excluding the PEM header and footer. This array is defensively copied.
base64Content
,
byte[]
data that precedes the PEM header. This array is defensively copied.
leadingData
)

Creates a PEM instance with the specified type and Base64-encoded content and leading data as byte arrays.

public
PEM(String
the PEM type identifier; must not contain PEM encapsulation syntax
type
,
byte[]
the Base64-encoded content, excluding the PEM header and footer. This array is defensively copied.
base64Content
)

Creates a PEM instance with the specified type and Base64-encoded content byte array.

Method Summary

Modifier and TypeMethod and Description
pack-priv void
public byte[]

Returns:

a newly-allocated byte array containing the Base64 content
content
()

Returns the Base64-encoded content.

public byte[]

Returns:

a newly-allocated byte array containing the decoded content
decode
()

Returns the Base64-decoded content as a byte array, using Base64#getMimeDecoder().

public byte[]

Returns:

a newly-allocated byte array containing leading data, or null if no leading data is present
leadingData
()

Returns the leading data that preceded the PEM header in the decoded input.

public String

Returns:

the PEM-formatted string
toString
()

Overrides java.lang.Object.toString.

Returns a PEM string representation of this object, using type for the header and footer lines and content for the Base64 body.

pack-priv byte[]
public String

Returns:

the PEM type identifier
type
()

Returns the PEM type identifier.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

contentback to summary
private final byte[] content
leadingDataback to summary
private byte[] leadingData
typeback to summary
private final String type

Constructor Detail

PEMback to summary
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:String

the PEM type identifier; must not contain PEM encapsulation syntax

base64Content:String

the Base64-encoded content, excluding the PEM header and footer

leadingData:byte[]

data that precedes the PEM header. This array is defensively copied.

Exceptions
IllegalArgumentException:
if type contains PEM encapsulation syntax
NullPointerException:
if any parameter is null
PEMback to summary
public PEM(String type, String base64Content)

Creates a PEM instance with the specified type and Base64-encoded content string.

Parameters
type:String

the PEM type identifier; must not contain PEM encapsulation syntax

base64Content:String

the Base64-encoded content, excluding the PEM header and footer

Exceptions
IllegalArgumentException:
if type contains PEM encapsulation syntax
NullPointerException:
if any parameter is null
PEMback to summary
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:String

the PEM type identifier; must not contain PEM encapsulation syntax

base64Content:byte[]

the Base64-encoded content, excluding the PEM header and footer. This array is defensively copied.

leadingData:byte[]

data that precedes the PEM header. This array is defensively copied.

Exceptions
IllegalArgumentException:
if type contains PEM encapsulation syntax
NullPointerException:
if any parameter is null
Since
27
PEMback to summary
public PEM(String type, byte[] base64Content)

Creates a PEM instance with the specified type and Base64-encoded content byte array.

Parameters
type:String

the PEM type identifier; must not contain PEM encapsulation syntax

base64Content:byte[]

the Base64-encoded content, excluding the PEM header and footer. This array is defensively copied.

Exceptions
IllegalArgumentException:
if type contains PEM encapsulation syntax
NullPointerException:
if any parameter is null
Since
27

Method Detail

clearback to summary
pack-priv void clear()
contentback to summary
public byte[] content()

Returns the Base64-encoded content.

Returns:byte[]

a newly-allocated byte array containing the Base64 content

Since
27
decodeback to summary
public byte[] decode()

Returns the Base64-decoded content as a byte array, using Base64#getMimeDecoder().

Returns:byte[]

a newly-allocated byte array containing the decoded content

Exceptions
IllegalArgumentException:
if decoding fails
leadingDataback to summary
public byte[] leadingData()

Returns the leading data that preceded the PEM header in the decoded input.

Returns:byte[]

a newly-allocated byte array containing leading data, or null if no leading data is present

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Returns a PEM string representation of this object, using type for the header and footer lines and content for the Base64 body.

Returns:String

the PEM-formatted string

Annotations
@Override
toTextualByteArrayback to summary
pack-priv byte[] toTextualByteArray()
typeback to summary
public String type()

Returns the PEM type identifier.

Returns:String

the PEM type identifier