Android String Decode decode(@Nonnull final String input)

Here you can find the source of decode(@Nonnull final String input)

Description

decode

License

Apache License

Declaration

public static byte[] decode(@Nonnull final String input)
            throws IllegalArgumentException 

Method Source Code

//package com.java2s;
/**/*  w ww .java2 s.c  om*/
 * Copyright 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import javax.annotation.Nonnull;

public class Main {
    private static final int[] INDEXES = new int[128];

    public static byte[] decode(@Nonnull final String input)
            throws IllegalArgumentException {
        if (input.length() == 0)
            return new byte[0];

        final byte[] input43 = new byte[input.length()];
        // Transform the String to a base43 byte sequence
        for (int i = 0; i < input.length(); ++i) {
            final char c = input.charAt(i);

            int digit43 = -1;

            if (c >= 0 && c < 128)
                digit43 = INDEXES[c];

            if (digit43 < 0)
                throw new IllegalArgumentException("Illegal character " + c
                        + " at " + i);

            input43[i] = (byte) digit43;
        }

        // Count leading zeroes
        int zeroCount = 0;
        while (zeroCount < input43.length && input43[zeroCount] == 0)
            ++zeroCount;

        // The encoding
        final byte[] temp = new byte[input.length()];
        int j = temp.length;

        int startAt = zeroCount;
        while (startAt < input43.length) {
            byte mod = divmod256(input43, startAt);
            if (input43[startAt] == 0)
                ++startAt;

            temp[--j] = mod;
        }

        // Do no add extra leading zeroes, move j to first non null byte.
        while (j < temp.length && temp[j] == 0)
            ++j;

        return copyOfRange(temp, j - zeroCount, temp.length);
    }

    private static byte divmod256(final byte[] number43, final int startAt) {
        int remainder = 0;
        for (int i = startAt; i < number43.length; i++) {
            final int digit58 = (int) number43[i] & 0xFF;
            final int temp = remainder * 43 + digit58;

            number43[i] = (byte) (temp / 256);

            remainder = temp % 256;
        }

        return (byte) remainder;
    }

    private static byte[] copyOfRange(final byte[] source, final int from,
            final int to) {
        final byte[] range = new byte[to - from];
        System.arraycopy(source, from, range, 0, range.length);

        return range;
    }
}

Related

  1. decode(@Nonnull final String input)
  2. decode(@Nonnull final String input)
  3. decode(String s)
  4. decode(String str, int flags)
  5. decodeWebSafe(String s)
  6. decode(String sourceStr, String compareStr, String returnStr)