# Base64.Encoder

Added in[API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Summary:[Methods](https://developer.android.com/reference/java/util/Base64.Encoder#pubmethods)\|[Inherited Methods](https://developer.android.com/reference/java/util/Base64.Encoder#inhmethods)  

# Base64.Encoder

`
public
static


class
Base64.Encoder
`  
`

extends `[Object](https://developer.android.com/reference/java/lang/Object)`


``


`

|---|--------------------------|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | java.util.Base64.Encoder |

<br />

*** ** * ** ***

This class implements an encoder for encoding byte data using the Base64 encoding scheme as specified in RFC 4648 and RFC 2045.

Instances of[Encoder](https://developer.android.com/reference/java/util/Base64.Encoder)class are safe for use by multiple concurrent threads.

Unless otherwise noted, passing a`null`argument to a method of this class will cause a[NullPointerException](https://developer.android.com/reference/java/lang/NullPointerException)to be thrown.

If the encoded byte output of the needed size can not be allocated, the encode methods of this class will cause an[OutOfMemoryError](https://developer.android.com/reference/java/lang/OutOfMemoryError)to be thrown.  
**See also:**

- [Base64.Decoder](https://developer.android.com/reference/java/util/Base64.Decoder)

## Summary

|                                                                                                                                                                                                                                ### Public methods                                                                                                                                                                                                                                ||
|---------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` int`                                                                                | ` `[encode](https://developer.android.com/reference/java/util/Base64.Encoder#encode(byte[],%20byte[]))`(byte[] src, byte[] dst) ` Encodes all bytes from the specified byte array using the[Base64](https://developer.android.com/reference/java/util/Base64)encoding scheme, writing the resulting bytes to the given output byte array, starting at offset 0.                           |
| ` byte[]`                                                                             | ` `[encode](https://developer.android.com/reference/java/util/Base64.Encoder#encode(byte[]))`(byte[] src) ` Encodes all bytes from the specified byte array into a newly-allocated byte array using the[Base64](https://developer.android.com/reference/java/util/Base64)encoding scheme.                                                                                                 |
| ` `[ByteBuffer](https://developer.android.com/reference/java/nio/ByteBuffer)          | ` `[encode](https://developer.android.com/reference/java/util/Base64.Encoder#encode(java.nio.ByteBuffer))`(`[ByteBuffer](https://developer.android.com/reference/java/nio/ByteBuffer)` buffer) ` Encodes all remaining bytes from the specified byte buffer into a newly-allocated ByteBuffer using the[Base64](https://developer.android.com/reference/java/util/Base64)encoding scheme. |
| ` `[String](https://developer.android.com/reference/java/lang/String)                 | ` `[encodeToString](https://developer.android.com/reference/java/util/Base64.Encoder#encodeToString(byte[]))`(byte[] src) ` Encodes the specified byte array into a String using the[Base64](https://developer.android.com/reference/java/util/Base64)encoding scheme.                                                                                                                    |
| ` `[Base64.Encoder](https://developer.android.com/reference/java/util/Base64.Encoder) | ` `[withoutPadding](https://developer.android.com/reference/java/util/Base64.Encoder#withoutPadding())`() ` Returns an encoder instance that encodes equivalently to this one, but without adding any padding character at the end of the encoded byte data.                                                                                                                              |
| ` `[OutputStream](https://developer.android.com/reference/java/io/OutputStream)       | ` `[wrap](https://developer.android.com/reference/java/util/Base64.Encoder#wrap(java.io.OutputStream))`(`[OutputStream](https://developer.android.com/reference/java/io/OutputStream)` os) ` Wraps an output stream for encoding byte data using the[Base64](https://developer.android.com/reference/java/util/Base64)encoding scheme.                                                    |

| ### Inherited methods |
|-----------------------|---|
| From class` `[java.lang.Object](https://developer.android.com/reference/java/lang/Object)` ` |--------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` `[Object](https://developer.android.com/reference/java/lang/Object)          | ` `[clone](https://developer.android.com/reference/java/lang/Object#clone())`() ` Creates and returns a copy of this object.                                                                                                                                                      | | ` boolean`                                                                     | ` `[equals](https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object))`(`[Object](https://developer.android.com/reference/java/lang/Object)` obj) ` Indicates whether some other object is "equal to" this one.                                           | | ` void`                                                                        | ` `[finalize](https://developer.android.com/reference/java/lang/Object#finalize())`() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.                                                          | | ` final `[Class](https://developer.android.com/reference/java/lang/Class)`<?>` | ` `[getClass](https://developer.android.com/reference/java/lang/Object#getClass())`() ` Returns the runtime class of this`Object`.                                                                                                                                                | | ` int`                                                                         | ` `[hashCode](https://developer.android.com/reference/java/lang/Object#hashCode())`() ` Returns a hash code value for the object.                                                                                                                                                 | | ` final void`                                                                  | ` `[notify](https://developer.android.com/reference/java/lang/Object#notify())`() ` Wakes up a single thread that is waiting on this object's monitor.                                                                                                                            | | ` final void`                                                                  | ` `[notifyAll](https://developer.android.com/reference/java/lang/Object#notifyAll())`() ` Wakes up all threads that are waiting on this object's monitor.                                                                                                                         | | ` `[String](https://developer.android.com/reference/java/lang/String)          | ` `[toString](https://developer.android.com/reference/java/lang/Object#toString())`() ` Returns a string representation of the object.                                                                                                                                            | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long,%20int))`(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. | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long))`(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](https://developer.android.com/reference/java/lang/Object#wait())`() ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*.                                                                                             | ||

## Public methods

### encode

Added in[API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public int encode (byte[] src, 
                byte[] dst)
```

Encodes all bytes from the specified byte array using the[Base64](https://developer.android.com/reference/java/util/Base64)encoding scheme, writing the resulting bytes to the given output byte array, starting at offset 0.

It is the responsibility of the invoker of this method to make sure the output byte array`dst`has enough space for encoding all bytes from the input byte array. No bytes will be written to the output byte array if the output byte array is not big enough.

<br />

|                   Parameters                   ||
|-------|-----------------------------------------|
| `src` | `byte`: the byte array to encode <br /> |
| `dst` | `byte`: the output byte array <br />    |

|                              Returns                               ||
|-------|-------------------------------------------------------------|
| `int` | The number of bytes written to the output byte array <br /> |

|                                                                                 Throws                                                                                  ||
|--------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------|
| [IllegalArgumentException](https://developer.android.com/reference/java/lang/IllegalArgumentException) | if`dst`does not have enough space for encoding all input bytes. |

### encode

Added in[API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public byte[] encode (byte[] src)
```

Encodes all bytes from the specified byte array into a newly-allocated byte array using the[Base64](https://developer.android.com/reference/java/util/Base64)encoding scheme. The returned byte array is of the length of the resulting bytes.

<br />

|                   Parameters                   ||
|-------|-----------------------------------------|
| `src` | `byte`: the byte array to encode <br /> |

|                                        Returns                                        ||
|----------|-----------------------------------------------------------------------------|
| `byte[]` | A newly-allocated byte array containing the resulting encoded bytes. <br /> |

### encode

Added in[API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public ByteBuffer encode (ByteBuffer buffer)
```

Encodes all remaining bytes from the specified byte buffer into a newly-allocated ByteBuffer using the[Base64](https://developer.android.com/reference/java/util/Base64)encoding scheme. Upon return, the source buffer's position will be updated to its limit; its limit will not have been changed. The returned output buffer's position will be zero and its limit will be the number of resulting encoded bytes.

<br />

|                           Parameters                           ||
|----------|------------------------------------------------------|
| `buffer` | `ByteBuffer`: the source ByteBuffer to encode <br /> |

|                                                                    Returns                                                                    ||
|---------------------------------------------------------------------------|--------------------------------------------------------------------|
| [ByteBuffer](https://developer.android.com/reference/java/nio/ByteBuffer) | A newly-allocated byte buffer containing the encoded bytes. <br /> |

### encodeToString

Added in[API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public String encodeToString (byte[] src)
```

Encodes the specified byte array into a String using the[Base64](https://developer.android.com/reference/java/util/Base64)encoding scheme.

This method first encodes all input bytes into a base64 encoded byte array and then constructs a new String by using the encoded byte array and the[ISO-8859-1](https://developer.android.com/reference/java/nio/charset/StandardCharsets#ISO_8859_1)charset.

In other words, an invocation of this method has exactly the same effect as invoking`new String(encode(src), StandardCharsets.ISO_8859_1)`.

<br />

|                   Parameters                   ||
|-------|-----------------------------------------|
| `src` | `byte`: the byte array to encode <br /> |

|                                                                Returns                                                                 ||
|--------------------------------------------------------------------|--------------------------------------------------------------------|
| [String](https://developer.android.com/reference/java/lang/String) | A String containing the resulting Base64 encoded characters <br /> |

### withoutPadding

Added in[API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public Base64.Encoder withoutPadding ()
```

Returns an encoder instance that encodes equivalently to this one, but without adding any padding character at the end of the encoded byte data.

The encoding scheme of this encoder instance is unaffected by this invocation. The returned encoder instance should be used for non-padding encoding operation.

<br />

|                                                                                    Returns                                                                                    ||
|------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------|
| [Base64.Encoder](https://developer.android.com/reference/java/util/Base64.Encoder) | an equivalent encoder that encodes without adding any padding character at the end <br /> |

### wrap

Added in[API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public OutputStream wrap (OutputStream os)
```

Wraps an output stream for encoding byte data using the[Base64](https://developer.android.com/reference/java/util/Base64)encoding scheme.

It is recommended to promptly close the returned output stream after use, during which it will flush all possible leftover bytes to the underlying output stream. Closing the returned output stream will close the underlying output stream.

<br />

|                   Parameters                    ||
|------|-------------------------------------------|
| `os` | `OutputStream`: the output stream. <br /> |

|                                                                                  Returns                                                                                   ||
|------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------|
| [OutputStream](https://developer.android.com/reference/java/io/OutputStream) | the output stream for encoding the byte data into the specified Base64 encoded format <br /> |