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;
 
A fast base64 encoding-OutputStream - not thread-safe.

Basic Usage:
 Base64OutputByteBuffer buffer = new Base64OutputByteBuffer(
     Base64OutputByteBuffer.DEFAULT_ALPHABET,
     Base64OutputByteBuffer.DEFAULT_PADDING,
     Base64OutputByteBuffer.DEFAULT_BUFFER_SIZE);
 ...
 buffer.reset();
 ...
 buffer.write(myData); // x times
 ...
 buffer.flush();
 ...
 ByteBuffer dest = buffer.getByteBuffer();
 ...
 
 
 public class Base64OutputByteBuffer extends OutputStream {
    
    
64K buffer.
 
     public static final int DEFAULT_BUFFER_SIZE = 65536;
    
Default Base64 alphabet (+, /).
 
     public static final String DEFAULT_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    
URL-safe Base64 alphabet (-, _).
 
     public static final String BASE64_URL_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
    
Default Base64 padding sign (=).
 
     public static final char DEFAULT_PADDING = '=';
    
    
encoding alphabet.
 
     protected char[] alphabet = .toCharArray();
    
Padding sign.
 
     protected byte padding = ;
    
Buffer for the encoded data.
 
     protected ByteBuffer outputBuffer;
    
Transfer-buffer for encoding.
 
     protected ByteBuffer inputBuffer = ByteBuffer.wrap(new byte[3]);
     
     public Base64OutputByteBuffer(String alphabetchar paddingint bufferSize) {
         if (bufferSize % 4 != 0) {
             throw new IllegalArgumentException("bufferSize needs to be restless dividable through 4");
         }
         
         this. = alphabet.toCharArray();
         this. = (bytepadding;
          = ByteBuffer.wrap(new byte[bufferSize]);
     }
     
     public ByteBuffer getByteBuffer() {
         return ;
     }
    
    
Clears input- and output-Buffer
 
     public void reset() {
         .clear();
         .clear();
     }
    
    
Fills the input-buffer and eventually encodes it.
    @Override
    public void write(int bthrows IOException {
        .put((byteb);
        if (.position() == .limit()) {
            encode();
        }
    }
    
    
Fills the input-buffer and eventually encodes it.
    @Override
    public void write(byte[] dataint offsetint lengththrows IOException {
        int currentOffset = offsetrestremainingLength = length;
        
        if (.position() != 0) {
            for (int i = 0; i < lengthi++) {
                .put(data[currentOffset++]);
                remainingLength--;
                if (.position() == .limit()) {
                    encode();
                    break;
                }
            }
        }
        
        rest = remainingLength % 3;
        remainingLength -= rest;
        if (remainingLength >= 3) {
            for (int i = 0; i < remainingLengthi += 3, currentOffset += 3) {
                .put(datacurrentOffset, 3);
                encode();
            }
        }
        
        while (rest-- > 0) {
            .put(data[currentOffset++]);
            if (.position() == .limit()) {
                encode();
            }
        }
    }
    
    
encodes and clears the input buffer.
    protected void encode() throws IOException {
        int index;
        
        index = (.get(0) & 0xfc) >> 2;
        .put((byte[index]);
        
        index = (.get(0) & 0x03) << 4 | (.get(1) & 0xf0) >> 4;
        .put((byte[index]);
        
        index = (.get(1) & 0x0f) << 2 | (.get(2) & 0xc0) >> 6;
        .put((byte[index]);
        
        index = .get(2) & 0x3f;
        .put((byte[index]);
        
        .clear();
    }
    
    @Override
    public void flush() throws IOException {
        int index;
        
        if (.position() == 1) {
            index = (.get(0) & 0xfc) >> 2;
            .put((byte[index]);
            
            index = (.get(0) & 0x03) << 4;
            .put((byte[index]);
            
            .put();
            .put();
        } else if (.position() == 2) {
            index = (.get(0) & 0xfc) >> 2;
            .put((byte[index]);
            
            index = (.get(0) & 0x03) << 4 | (.get(1) & 0xf0) >> 4;
            .put((byte[index]);
            
            index = (.get(1) & 0x0f) << 2;
            .put((byte[index]);
            
            .put();
        }
        
        .clear();
        .flip();
    }
    
New to GrepCode? Check out our FAQ X