Base64.java

  1. /**
  2.  *
  3.  * Copyright © 2014-2019 Florian Schmaus
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smack.util.stringencoder;

  18. import java.nio.charset.StandardCharsets;

  19. import org.jivesoftware.smack.util.Objects;
  20. import org.jivesoftware.smack.util.StringUtils;

  21. public class Base64 {

  22.     private static Base64.Encoder base64encoder;

  23.     public static void setEncoder(Base64.Encoder encoder) {
  24.         Objects.requireNonNull(encoder, "encoder must no be null");
  25.         base64encoder = encoder;
  26.     }

  27.     public static final String encode(String string) {
  28.         return encodeToString(string.getBytes(StandardCharsets.UTF_8));
  29.     }

  30.     public static final String encodeToString(byte[] input) {
  31.         return base64encoder.encodeToString(input);
  32.     }

  33.     public static final String encodeToString(byte[] input, int offset, int len) {
  34.         return encodeToString(slice(input, offset, len));
  35.     }

  36.     public static final String encodeToStringWithoutPadding(byte[] input) {
  37.         return base64encoder.encodeToStringWithoutPadding(input);
  38.     }

  39.     public static final byte[] encode(byte[] input) {
  40.         return base64encoder.encode(input);
  41.     }

  42.     public static final String decodeToString(String string) {
  43.         byte[] bytes = decode(string);
  44.         return new String(bytes, StandardCharsets.UTF_8);
  45.     }

  46.     // TODO: We really should not mask the IllegalArgumentException. But some unit test depend on this behavior, like
  47.     // ibb.packet.DataPacketExtension.shouldReturnNullIfDataIsInvalid().
  48.     public static final byte[] decode(String string) {
  49.         // xs:base64Binary may include XML whitespace which we need to delete before feeding the string into the Base64
  50.         // decoder. See also XML Schema Part 2: Datatypes Second Edition § 3.2.16.
  51.         string = StringUtils.deleteXmlWhitespace(string);
  52.         try {
  53.             return base64encoder.decode(string);
  54.         } catch (IllegalArgumentException e) {
  55.             return null;
  56.         }
  57.     }

  58.     public static final byte[] decode(byte[] input) {
  59.         String string = new String(input, StandardCharsets.US_ASCII);
  60.         return decode(string);
  61.     }

  62.     private static byte[] slice(byte[] input, int offset, int len) {
  63.         if (offset == 0 && len == input.length) {
  64.             return input;
  65.         }

  66.         byte[] res = new byte[len];
  67.         System.arraycopy(input, offset, res, 0, len);
  68.         return res;
  69.     }

  70.     public interface Encoder {
  71.         byte[] decode(String string);

  72.         String encodeToString(byte[] input);

  73.         String encodeToStringWithoutPadding(byte[] input);

  74.         byte[] encode(byte[] input);
  75.     }
  76. }