# Class PemReader (2.1.0)

<button class="button button-with-icon cloud-select-dropdown__button " aria-haspopup="true">Version latest</button>

- [2.1.0 (latest)](https://docs.cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.util.PemReader)
- [2.0.3](https://docs.cloud.google.com/java/docs/reference/google-http-client/2.0.3/com.google.api.client.util.PemReader)
- [1.47.1](https://docs.cloud.google.com/java/docs/reference/google-http-client/1.47.1/com.google.api.client.util.PemReader)
- [1.46.3](https://docs.cloud.google.com/java/docs/reference/google-http-client/1.46.3/com.google.api.client.util.PemReader)
- [1.45.3](https://docs.cloud.google.com/java/docs/reference/google-http-client/1.45.3/com.google.api.client.util.PemReader)
- [1.44.2](https://docs.cloud.google.com/java/docs/reference/google-http-client/1.44.2/com.google.api.client.util.PemReader)
- [1.43.2](https://docs.cloud.google.com/java/docs/reference/google-http-client/1.43.2/com.google.api.client.util.PemReader)
- [1.42.3](https://docs.cloud.google.com/java/docs/reference/google-http-client/1.42.3/com.google.api.client.util.PemReader)
- [1.41.8](https://docs.cloud.google.com/java/docs/reference/google-http-client/1.41.8/com.google.api.client.util.PemReader)

    public final class PemReader

[Beta](https://docs.cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.util.Beta)   

PEM file reader.

Supports reading any PEM stream that contains Base64 encoded content stored inside `
"---BEGIN ...---"` and `"---END ...---"` tags. Each call to [#readNextSection()](https://docs.cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.util.PemReader#com_google_api_client_util_PemReader_readNextSection__) parses the next section in the PEM file. If you need a section of a certain
title use [#readNextSection(String)](https://docs.cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.util.PemReader#com_google_api_client_util_PemReader_readNextSection_java_lang_String_), for example `readNextSection("PRIVATE KEY")`.
To ensure that the stream is closed properly, call [#close()](https://docs.cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.util.PemReader#com_google_api_client_util_PemReader_close__) in a finally block.

As a convenience, use [#readFirstSectionAndClose(Reader)](https://docs.cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.util.PemReader#com_google_api_client_util_PemReader_readFirstSectionAndClose_java_io_Reader_) or [#readFirstSectionAndClose(Reader, String)](https://docs.cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.util.PemReader#com_google_api_client_util_PemReader_readFirstSectionAndClose_java_io_Reader_java_lang_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](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html) \> PemReader

## Inherited Members

[Object.clone()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#clone--) [Object.equals(Object)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object-) [Object.finalize()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#finalize--) [Object.getClass()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#getClass--) [Object.hashCode()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode--) [Object.notify()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notify--) [Object.notifyAll()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notifyAll--) [Object.toString()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#toString--) [Object.wait()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait--) [Object.wait(long)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-) [Object.wait(long,int)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-int-)

## 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` | `https://docs.oracle.com/javase/8/docs/api/java/io/Reader.html` reader |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.util.PemReader.Section` | first section found or `null` for none found |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/io/IOException.html` |   |

### 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` | `https://docs.oracle.com/javase/8/docs/api/java/io/Reader.html` reader |
| `titleToLookFor` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` title to look for or `null` for any title |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.util.PemReader.Section` | first section found or `null` for none found |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/io/IOException.html` |   |

## Constructors

### PemReader(Reader reader)

    public PemReader(Reader reader)

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `reader` | `https://docs.oracle.com/javase/8/docs/api/java/io/Reader.html` reader |

## Methods

### close()

    public void close()

Closes the reader.

To ensure that the stream is closed properly, call [#close()](https://docs.cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.util.PemReader#com_google_api_client_util_PemReader_close__) in a finally block.

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/io/IOException.html` |   |

### readNextSection()

    public PemReader.Section readNextSection()

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

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.util.PemReader.Section` |   |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/io/IOException.html` |   |

### 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` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` title to look for or `null` for any title |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.util.PemReader.Section` | next section or `null` for end of file |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/io/IOException.html` |   |