Java Base64 Encode base64Encode(int value)

Here you can find the source of base64Encode(int value)

Description

Utility method for producing a (psuedo) base 64 encoding of an int value, suitable for inclusion in a file name.

License

Apache License

Declaration

public static String base64Encode(int value) 

Method Source Code

//package com.java2s;
/*//from  w w  w . ja va 2 s  .c o  m
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you 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.
 */

public class Main {
    private static final char[] _BASE_64_CHARS = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', // 0-9
            'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', // 10-19
            'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', // 20-29
            'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', // 30-39
            'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', // 40-49
            'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', // 50-59
            '8', '9', '_', '-' // 60-63
    };

    /**
     * Utility method for producing a (psuedo) base 64 encoding
     * of an int value, suitable for inclusion in a file name.
     * This is used by NameProviders to include a semi-unique
     * identifier (ie. the base 64 encoded hash code) in the
     * image file names in order to avoid name collisions.
     */
    public static String base64Encode(int value) {
        StringBuffer buffer = new StringBuffer(6);

        // For now, let's just use the lower 3 bytes.  This increases
        // the possibility of collisions, but it allows us to go
        // from a 6 char to 4 char id.

        for (int i = 0; i < 4; i++)
            buffer.append(_BASE_64_CHARS[((value >> (6 * i)) & 0x3f)]);

        return buffer.toString();
    }
}

Related

  1. base64Encode(byte[] in)
  2. base64Encode(byte[] in)
  3. Base64Encode(byte[] input, boolean addLineBreaks)
  4. base64Encode(byte[] param)
  5. base64encode(final byte[] data)
  6. base64Encode(String _s, String _enc)
  7. base64Encode(String plaintext)
  8. base64Encode(String plainTextString)
  9. base64Encode(String s)