Base64.java

  1. /**
  2.  *
  3.  * Copyright © 2014-2015 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.io.UnsupportedEncodingException;

  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.         try {
  29.             return encodeToString(string.getBytes(StringUtils.UTF8));
  30.         } catch (UnsupportedEncodingException e) {
  31.             throw new IllegalStateException("UTF-8 not supported", e);
  32.         }
  33.     }

  34.     public static final String encodeToString(byte[] input) {
  35.         byte[] bytes = encode(input);
  36.         try {
  37.             return new String(bytes, StringUtils.USASCII);
  38.         } catch (UnsupportedEncodingException e) {
  39.             throw new AssertionError(e);
  40.         }
  41.     }

  42.     public static final String encodeToString(byte[] input, int offset, int len) {
  43.         byte[] bytes = encode(input, offset, len);
  44.         try {
  45.             return new String(bytes, StringUtils.USASCII);
  46.         } catch (UnsupportedEncodingException e) {
  47.             throw new AssertionError(e);
  48.         }
  49.     }

  50.     public static final byte[] encode(byte[] input) {
  51.         return encode(input, 0, input.length);
  52.     }

  53.     public static final byte[] encode(byte[] input, int offset, int len) {
  54.         return base64encoder.encode(input, offset, len);
  55.     }

  56.     public static final String decodeToString(String string) {
  57.         byte[] bytes = decode(string);
  58.         try {
  59.             return new String(bytes, StringUtils.UTF8);
  60.         } catch (UnsupportedEncodingException e) {
  61.             throw new IllegalStateException("UTF-8 not supported", e);
  62.         }
  63.     }

  64.     public static final String decodeToString(byte[] input, int offset, int len) {
  65.         byte[] bytes = decode(input, offset, len);
  66.         try {
  67.             return new String(bytes, StringUtils.UTF8);
  68.         } catch (UnsupportedEncodingException e) {
  69.             throw new IllegalStateException("UTF-8 not supported", e);
  70.         }
  71.     }

  72.     public static final byte[] decode(String string) {
  73.         return base64encoder.decode(string);
  74.     }

  75.     public static final byte[] decode(byte[] input) {
  76.         return base64encoder.decode(input, 0, input.length);
  77.     }

  78.     public static final byte[] decode(byte[] input, int offset, int len) {
  79.         return base64encoder.decode(input, offset, len);
  80.     }

  81.     public interface Encoder {
  82.         byte[] decode(String string);

  83.         byte[] decode(byte[] input, int offset, int len);

  84.         String encodeToString(byte[] input, int offset, int len);

  85.         byte[] encode(byte[] input, int offset, int len);
  86.     }
  87. }