Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2012 the project-owners 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.
 
 package inti.util;
 
An utility for fast Base64 decoding - not thread-safe.
 
 public class Base64Util {
    
    
Transfer-buffer.
 
     byte[] chars = new byte[4];
    
base64 alphabet for decoding.
 
     byte[] alphabet;
    
    
 
     public Base64Util() {
     }
    
    
Initializes with the given alphabet.
 
     public Base64Util(String alphabet) {
         this. = decodeAlphabet(alphabet);
     }
    
    
Transforms the given alphabet into a more efficient byte-array version.

Parameters:
str
Returns:
 
     byte[] decodeAlphabet(String str) {
         byte[] result = new byte[256];
         int index = 0;
         
         Arrays.fill(result, (byte) -1);
         for (char letter : str.toCharArray()) {
             result[letter] = (byteindex++;
         }
         
         return result;
     }
    
    
Decodes the base64 string into the java.nio.ByteBuffer without any object allocations.

Parameters:
source
buffer
 
     public void decodeBase64(String sourceByteBuffer buffer) {
         int index = 0, end = source.length() - 4;
         
         for (; index < endindex += 4) {
             [0] = [source.charAt(index)];
             [1] = [source.charAt(index + 1)];
             [2] = [source.charAt(index + 2)];
             [3] = [source.charAt(index + 3)];
             
             buffer.put((byte) ([0] << 2 | [1] >> 4));
             buffer.put((byte) (([1] & 15) << 4 | [2] >> 2));
             buffer.put((byte) (([2] & 3) << 6 | [3]));
         }
         
         [0] = [source.charAt(index)];
         [1] = [source.charAt(index + 1)];
         [2] = [source.charAt(index + 2)];
         [3] = [source.charAt(index + 3)];
         
         buffer.put((byte) ([0] << 2 | [1] >> 4));
         if ([2] != -1) {
             buffer.put((byte) (([1] & 15) << 4 | [2] >> 2));
         }
         if ([3] != -1) {
             buffer.put((byte) (([2] & 3) << 6 | [3]));
         }
     }
    
    
Decodes the base64 source buffer into the dest-java.nio.ByteBuffer without any object allocations.

Parameters:
source
length
dest
    public void decodeBase64(ByteBuffer sourceint lengthByteBuffer dest) {
        int index = 0, end = length - 4;
        
        for (; index < end && source.position() < source.limit() - 4; index += 4) {
            [0] = [source.get()];
            [1] = [source.get()];
            [2] = [source.get()];
            [3] = [source.get()];
            
            dest.put((byte) ([0] << 2 | [1] >> 4));
            dest.put((byte) (([1] & 15) << 4 | [2] >> 2));
            dest.put((byte) (([2] & 3) << 6 | [3]));
        }
        
        [0] = [source.get()];
        [1] = [source.get()];
        [2] = [source.get()];
        [3] = [source.get()];
        
        dest.put((byte) ([0] << 2 | [1] >> 4));
        if ([2] != -1) {
            dest.put((byte) (([1] & 15) << 4 | [2] >> 2));
        }
        if ([3] != -1) {
            dest.put((byte) (([2] & 3) << 6 | [3]));
        }
    }
    
New to GrepCode? Check out our FAQ X