All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.util.Base62Utils Maven / Gradle / Ivy

package org.springframework.util;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @see Base64Utils
 */
public class Base62Utils {
  private static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
  private static final String toBase62 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  private static final char NINE = '9';
  private static final Map FLAG = new HashMap();
  static {
    FLAG.put('A', 61);
    FLAG.put('B', 62);
    FLAG.put('C', 63);
  }

  public static byte[] encode(byte[] src) {
    String encodeToString = encodeToString(src);
    return encodeToString == null ? null : encodeToString.getBytes(DEFAULT_CHARSET);
  }

  public static byte[] decode(byte[] src) {
    if (src == null || src.length == 0) {
      return src;
    }
    return decodeFromString(new String(src, DEFAULT_CHARSET));
  }

  public static byte[] encodeUrlSafe(byte[] src) {
    if (src == null || src.length == 0) {
      return src;
    }
    String encodeToString = encodeToString(src);
    return encodeToString == null ? null : encodeToString.getBytes(DEFAULT_CHARSET);
  }

  public static byte[] decodeUrlSafe(byte[] src) {
    if (src == null || src.length == 0) {
      return src;
    }
    return decodeFromString(new String(src, DEFAULT_CHARSET));
  }

  public static String encodeToString(byte[] src) {
    if (src == null) {
      return null;
    }
    if (src.length == 0) {
      return "";
    }

    StringBuilder builder = new StringBuilder();
    builder.setLength(0);
    int b;
    for (int i = 0; i < src.length; i += 3) {
      b = (src[i] & 0xFC) >> 2;
      append(builder, b);
      b = (src[i] & 0x03) << 4;
      if (i + 1 < src.length) {
        b |= (src[i + 1] & 0xF0) >> 4;
        append(builder, b);
        b = (src[i + 1] & 0x0F) << 2;
        if (i + 2 < src.length) {
          b |= (src[i + 2] & 0xC0) >> 6;
          append(builder, b);
          b = src[i + 2] & 0x3F;
          append(builder, b);
        }
        else {
          append(builder, b);
        }
      }
      else {
        append(builder, b);
      }
    }
    return new String(builder);
  }

  public static byte[] decodeFromString(String src) {
    if (src == null) {
      return null;
    }
    if (src.isEmpty()) {
      return new byte[0];
    }
    char[] chars = src.toCharArray();

    List decodedList = new ArrayList();
    int[] unit = new int[4];

    int inputLen = chars.length;
    int n = 0;
    int m = 0;
    char ch1 = 0;
    char ch2 = 0;
    Byte b = 0;
    while (n < inputLen) {
      ch1 = chars[n];
      if (ch1 != NINE) {
        unit[m] = toBase62.indexOf(ch1);
        m++;
        n++;
      }
      else {
        n++;
        if (n < inputLen) {
          ch2 = chars[n];
          if (ch2 != NINE) {
            unit[m] = FLAG.get(ch2);
            m++;
            n++;
          }
        }
      }

      if (m == 4) {
        b = new Byte((byte) ((unit[0] << 2) | (unit[1] >> 4)));
        decodedList.add(b);
        b = new Byte((byte) ((unit[1] << 4) | (unit[2] >> 2)));
        decodedList.add(b);
        b = new Byte((byte) ((unit[2] << 6) | unit[3]));
        decodedList.add(b);
        m = 0;
      }
    }

    if (m != 0) {
      if (m == 1) {
        b = new Byte((byte) ((unit[0] << 2)));
        decodedList.add(b);
      }
      else if (m == 2) {
        b = new Byte((byte) ((unit[0] << 2) | (unit[1] >> 4)));
        decodedList.add(b);
      }
      else if (m == 3) {
        b = new Byte((byte) ((unit[0] << 2) | (unit[1] >> 4)));
        decodedList.add(b);
        b = new Byte((byte) ((unit[1] << 4) | (unit[2] >> 2)));
        decodedList.add(b);
      }
    }

    Byte[] decodedObj = decodedList.toArray(new Byte[decodedList.size()]);
    byte[] decoded = new byte[decodedObj.length];
    for (int i = 0; i < decodedObj.length; i++) {
      decoded[i] = decodedObj[i];
    }
    return decoded;
  }

  public static String encodeToUrlSafeString(byte[] src) {
    return encodeToString(src);
  }

  public static byte[] decodeFromUrlSafeString(String src) {
    return decodeFromString(src);
  }

  private static void append(StringBuilder builder, int index) {
    if (index < 61) {
      builder.append(toBase62.charAt(index));
    }
    else {
      builder.append(NINE);
      builder.append(toBase62.charAt(index - 61));
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy