Base64 (Java SE 26 & JDK 26)
Description: declaration: module: java.base, package: java.util, class: Base64
Keywords: java.util.Base64 class

# Class Base64

java.lang.Object
java.util.Base64

public final class Base64 extends Object
This class consists exclusively of static methods for obtaining encoders and decoders for the Base64 encoding scheme. The implementation of this class supports the following types of Base64 as specified in RFC 4648 and RFC 2045.

- Basic

  Uses "The Base64 Alphabet" as specified in Table 1 of RFC 4648 and RFC 2045 for encoding and decoding operation. The encoder does not add any line feed (line separator) character. The decoder rejects data that contains characters outside the base64 alphabet.

- URL and Filename safe

  Uses the "URL and Filename safe Base64 Alphabet" as specified in Table 2 of RFC 4648 for encoding and decoding. The encoder does not add any line feed (line separator) character. The decoder rejects data that contains characters outside the base64 alphabet.

- MIME

  Uses "The Base64 Alphabet" as specified in Table 1 of RFC 2045 for encoding and decoding operation. The encoded output must be represented in lines of no more than 76 characters each and uses a carriage return `'\r'` followed immediately by a linefeed `'\n'` as the line separator. No line separator is added to the end of the encoded output. All line separators or other characters not found in the base64 alphabet table are ignored in decoding operation.

Unless otherwise noted, passing a `null` argument to a method of this class will cause a `NullPointerException` to be thrown.

Since:
  1.8
External Specifications
  - RFC 2045: Multipurpose Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies
  - RFC 4648: The Base16, Base32, and Base64 Data Encodings

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Class
  Description
  `static class`
  `Base64.Decoder`
  This class implements a decoder for decoding byte data using the Base64 encoding scheme as specified in RFC 4648 and RFC 2045.
  `static class`
  `Base64.Encoder`
  This class implements an encoder for encoding byte data using the Base64 encoding scheme as specified in RFC 4648 and RFC 2045.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static Base64.Decoder`
  `getDecoder()`
  Returns a `Base64.Decoder` that decodes using the Basic type base64 encoding scheme.
  `static Base64.Encoder`
  `getEncoder()`
  Returns a `Base64.Encoder` that encodes using the Basic type base64 encoding scheme.
  `static Base64.Decoder`
  `getMimeDecoder()`
  Returns a `Base64.Decoder` that decodes using the MIME type base64 decoding scheme.
  `static Base64.Encoder`
  `getMimeEncoder()`
  Returns a `Base64.Encoder` that encodes using the MIME type base64 encoding scheme.
  `static Base64.Encoder`
  `getMimeEncoder(int lineLength, byte[] lineSeparator)`
  Returns a `Base64.Encoder` that encodes using the MIME type base64 encoding scheme with specified line length and line separators.
  `static Base64.Decoder`
  `getUrlDecoder()`
  Returns a `Base64.Decoder` that decodes using the URL and Filename safe type base64 encoding scheme.
  `static Base64.Encoder`
  `getUrlEncoder()`
  Returns a `Base64.Encoder` that encodes using the URL and Filename safe type base64 encoding scheme.

  ### Methods declared in class Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`
  Modifier and Type
  Method
  Description
  `protected Object`
  `clone()`
  Creates and returns a copy of this object.
  `boolean`
  `equals(Object obj)`
  Indicates whether some other object is "equal to" this one.
  `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`.
  `int`
  `hashCode()`
  Returns a hash code value for 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.
  `String`
  `toString()`
  Returns a string representation of the object.
  `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.

- ## Method Details

  - ### getEncoder

    public static Base64.Encoder getEncoder()
    Returns a `Base64.Encoder` that encodes using the Basic type base64 encoding scheme.
    Returns:
      A Base64 encoder.

  - ### getUrlEncoder

    public static Base64.Encoder getUrlEncoder()
    Returns a `Base64.Encoder` that encodes using the URL and Filename safe type base64 encoding scheme.
    Returns:
      A Base64 encoder.

  - ### getMimeEncoder

    public static Base64.Encoder getMimeEncoder()
    Returns a `Base64.Encoder` that encodes using the MIME type base64 encoding scheme.
    Returns:
      A Base64 encoder.

  - ### getMimeEncoder

    public static Base64.Encoder getMimeEncoder(int lineLength, byte[] lineSeparator)
    Returns a `Base64.Encoder` that encodes using the MIME type base64 encoding scheme with specified line length and line separators.
    Parameters:
      `lineLength` - the length of each output line (rounded down to nearest multiple of 4). If the rounded down line length is not a positive value, the output will not be separated in lines
      `lineSeparator` - the line separator for each output line
    Returns:
      A Base64 encoder.
    Throws:
      `IllegalArgumentException` - if `lineSeparator` includes any character of "The Base64 Alphabet" as specified in Table 1 of RFC 2045.

  - ### getDecoder

    public static Base64.Decoder getDecoder()
    Returns a `Base64.Decoder` that decodes using the Basic type base64 encoding scheme.
    Returns:
      A Base64 decoder.

  - ### getUrlDecoder

    public static Base64.Decoder getUrlDecoder()
    Returns a `Base64.Decoder` that decodes using the URL and Filename safe type base64 encoding scheme.
    Returns:
      A Base64 decoder.

  - ### getMimeDecoder

    public static Base64.Decoder getMimeDecoder()
    Returns a `Base64.Decoder` that decodes using the MIME type base64 decoding scheme.
    Returns:
      A Base64 decoder.