Base-64 Encoder - translates from base-64 text into binary : Base64 « Development Class « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Development Class » Base64Screenshots 
Base-64 Encoder - translates from base-64 text into binary
    
/*
 * Base64Encoder.java
 *
 * Created on 20 December 2005, 11:15
 *
 */


import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CoderResult;

/**
 * <p>Base-64 Encoder - translates from base-64 text into binary.</p>
 * <p>Whitespace is ignored, so is anything following a terminating = 
 * character.</p>
 @author Malcolm McMahon
 @version $Revision: 1.1 $
 */
public class Base64Encoder extends CharsetEncoder {
    final static int CHARCODE_INVALID = -1;
    final static int CHARCODE_PADDER = -2;
    final static int CHARCODE_WHITESPACE = -3;
    
    int[] encTable;
    int encState;
    Byte excessByte;
    int bits;
    /** Creates a new instance of Base64Encoder
     @param cs The Charset which created this
     @param encTable Table which maps chacacters onto 6 bit values.
     */
    public Base64Encoder(Charset cs, int[] encTable) {
        super(cs, 1.4f2f);
        this.encTable = encTable;
    }
    
    private boolean out(ByteBuffer bb, int outValue) {
        if(bb.remaining() 0) {
            bb.put((byte)outValue);
            return true;
        else {
            excessByte = Byte.valueOf((byte)outValue);
            return false;
        }
    }
    
    
    
    
    /**
     * Flushes this encoder.
     *
     * <p> The default implementation of this method does nothing, and always
     * returns {@link CoderResult#UNDERFLOW}.  This method should be overridden
     * by encoders that may need to write final bytes to the output buffer
     * once the entire input sequence has been read. </p>
     *
     @param  out
     *         The output byte buffer
     *
     @return  A coder-result object, either {@link CoderResult#UNDERFLOW} or
     *          {@link CoderResult#OVERFLOW}
     */
    protected java.nio.charset.CoderResult implFlush(java.nio.ByteBuffer out) {
        if(encState != && encState != 4)
            throw new IllegalArgumentException("Base-64 text ends prematurely");
        if(excessByte == null) {
            implReset();
            return CoderResult.UNDERFLOW;
        }
        if(out.remaining() 0) {
            out.put(excessByte.byteValue());
            implReset();
            return CoderResult.UNDERFLOW;
        else
            return CoderResult.OVERFLOW;
        
    }
    
    /**
     * Encodes one or more characters into one or more bytes.
     *
     * <p> This method encapsulates the basic encoding loop, encoding as many
     * characters as possible until it either runs out of input, runs out of room
     * in the output buffer, or encounters an encoding error.  This method is
     * invoked by the {@link #encode encode} method, which handles result
     * interpretation and error recovery.
     *
     * <p> The buffers are read from, and written to, starting at their current
     * positions.  At most {@link Buffer#remaining in.remaining()} characters
     * will be read, and at most {@link Buffer#remaining out.remaining()}
     * bytes will be written.  The buffers' positions will be advanced to
     * reflect the characters read and the bytes written, but their marks and
     * limits will not be modified.
     *
     * <p> This method returns a {@link CoderResult} object to describe its
     * reason for termination, in the same manner as the {@link #encode encode}
     * method.  Most implementations of this method will handle encoding errors
     * by returning an appropriate result object for interpretation by the
     {@link #encode encode} method.  An optimized implementation may instead
     * examine the relevant error action and implement that action itself.
     *
     * <p> An implementation of this method may perform arbitrary lookahead by
     * returning {@link CoderResult#UNDERFLOW} until it receives sufficient
     * input.  </p>
     *
     @param  in
     *         The input character buffer
     *
     @param  out
     *         The output byte buffer
     *
     @return  A coder-result object describing the reason for termination
     */
    public java.nio.charset.CoderResult encodeLoop(java.nio.CharBuffer in, java.nio.ByteBuffer out) {
        if(excessByte != null) {
            if(out.remaining() 0) {
                out.put(excessByte.byteValue());
                excessByte = null;
            else
                return CoderResult.OVERFLOW;
            
        }
        while(in.remaining() 0) {
            char inch = in.get();
            int code = (int)inch >= encTable.length ? CHARCODE_INVALID : encTable[(int)inch];
            if(encState < 4) {
                switch(code) {
                    case CHARCODE_INVALID:
                        throw new IllegalArgumentException("Invalid base-64 character'" + inch + "'" );
                    case CHARCODE_WHITESPACE:
                        break;
                    case CHARCODE_PADDER:
                        if(encState == 1)
                            throw new IllegalArgumentException("Mal-formed base-64 (= after one character");
                        encState = 4;
                        break;
                    default:
                        switch(encState) {
                            case 0:
                                bits = code << 2;
                                encState = 1;
                                break;
                            case 1:
                                encState = 2;
                                int v = bits | ((code >> 43);
                                bits = (code << 40xF0;
                                if(!out(out, v))
                                    return CoderResult.OVERFLOW;
                                break;
                            case 2:
                                encState = 3;
                                v = bits | (code >> 20x0f;
                                bits = (code << 60xC0;
                                if(!out(out, v))
                                    return CoderResult.OVERFLOW;
                                break;
                            case 3:
                                encState = 0;
                                bits |= (code & 0x3f);
                                if(!out(out, bits))
                                    return CoderResult.OVERFLOW;
                                break;
                        }
                        
                        break;
                }
                
                
            }
        }
        return CoderResult.UNDERFLOW;
    }

    /**
     * Reset - clear encoder state
     */
    protected void implReset() {
        encState = 0;
        bits = 0;
        excessByte = null;
    }
    
}

   
    
    
    
  
Related examples in the same category
1. Base64 encoding/decoding.
2. Decodes Base64 data into octects
3. Implementation of MIME's Base64 encoding and decoding conversions.
4. Encode/decode for RFC 2045 Base64 as defined by RFC 2045
5. Encode/decode for RFC 2045 Base64 as defined by RFC 2045, N. Freed and N. Borenstein.
6. Encodes and decodes to and from Base64 notation.
7. Encodes hex octects into Base64
8. Helper class to provide Base64 encoding routines.
9. Represents a collection of 64 boolean (on/off) flags.
10. byte to be tested if it is Base64 alphabet
11. to Base64
12. One of the fastest implementation of the Base64 encoding. Jakarta and others are slower
13. array of byte to encode
14. Codes number up to radix 62
15. A Base64 Encoder/Decoder
16. A fast and memory efficient class to encode and decode to and from BASE64 in full accordance with RFC 2045
17. BASE64 encoder implementation
18. Base64 Character encoder as specified in RFC1113
19. Base64 Utils
20. Base64 encoder/decoder
21. Base64 from by Funambol, Inc.
22. Convert to hex from byte arrays and back
23. Converting hexadecimal strings
24. Encode and decode data in Base64 format as described in RFC 1521
25. Encode and decode integers, times, and internationalized strings to and from popular binary formats
26. Encoding of raw bytes to base64-encoded characters, and decoding of base64 characters to raw bytes
27. Performs Base64 encoding and/or decoding
28. Provides Base64 encoding and decoding as defined by RFC 2045
29. Provides Base64 encoding and decoding with URL and filename safe alphabet as defined by RFC 3548, section 4.
30. Provides utility methods to Base64 encode data
31. QP Decoder Stream
32. QP Encoder Stream
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.