Android Byte Array Encode encode(@Nonnull byte[] input)

Here you can find the source of encode(@Nonnull byte[] input)

Description

encode

License

Apache License

Declaration

public static String encode(@Nonnull byte[] input) 

Method Source Code

//package com.java2s;
/**/*w w  w  .  ja  v a  2 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 java.nio.charset.Charset;
import javax.annotation.Nonnull;

public class Main {
    private static final char[] ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ$*+-./:"
            .toCharArray();

    public static String encode(@Nonnull byte[] input) {
        if (input.length == 0)
            return "";

        input = copyOfRange(input, 0, input.length);

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

        // The actual encoding.
        final byte[] temp = new byte[input.length * 2];
        int j = temp.length;

        int startAt = zeroCount;
        while (startAt < input.length) {
            byte mod = divmod43(input, startAt);
            if (input[startAt] == 0)
                ++startAt;
            temp[--j] = (byte) ALPHABET[mod];
        }

        // Strip extra '1' if there are some after decoding.
        while (j < temp.length && temp[j] == ALPHABET[0])
            ++j;

        // Add as many leading '1' as there were leading zeros.
        while (--zeroCount >= 0)
            temp[--j] = (byte) ALPHABET[0];

        final byte[] output = copyOfRange(temp, j, temp.length);

        return new String(output, Charset.forName("US-ASCII"));
    }

    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;
    }

    private static byte divmod43(final byte[] number, final int startAt) {
        int remainder = 0;
        for (int i = startAt; i < number.length; i++) {
            final int digit256 = (int) number[i] & 0xFF;
            final int temp = remainder * 256 + digit256;

            number[i] = (byte) (temp / 43);

            remainder = temp % 43;
        }

        return (byte) remainder;
    }
}

Related

  1. encode(@Nonnull byte[] input)
  2. encode(@Nonnull byte[] input)
  3. encode(byte[] input, int flags)
  4. encode(byte[] input, int offset, int len, int flags)
  5. encode(byte[] source)
  6. encode(byte[] source)