Provides utility methods to Base64 encode data : 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 
Provides utility methods to Base64 encode data
    
/* 
 *
 
 * Copyright 2005 Vincent Massol.
 *
 * 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.io.ByteArrayOutputStream;

/**
 * Provides utility methods to Base64 encode data. This class uses the Base64 encoding as specified
 * in RFC 2045, 6.8. Base64 Content-Transfer-Encoding.
 
 @version $Id: Base64.java 1705 2008-09-02 13:14:55Z adriana $
 */
public final class Base64
{
    // constants --------------------------------------------------------------

    /**
     * The Base64 character set look-up table. This consists of the following ordered alphanumerics:
     * A-Z, a-z, 0-9, + and /.
     */
    private static final char[] ENCODE = new char[64];

    /**
     * The character to pad the output with if not a multiple of 24-bits.
     */
    private static final char PAD_CHAR = '=';

    // static -----------------------------------------------------------------

    static
    {
        // create Base64 character look-up table
        for (int i = 0; i < 26; i++)
        {
            ENCODE[i(char) ('A' + i);
            ENCODE[i + 26(char) ('a' + i);
        }
        for (int i = 0; i < 10; i++)
        {
            ENCODE[i + 52(char) ('0' + i);
        }
        ENCODE[62'+';
        ENCODE[63'/';
    }

    // constructors -----------------------------------------------------------

    /**
     * Private to prevent unnecessary instantation.
     */
    private Base64()
    {
        // Private to prevent unnecessary instantation
    }

    // public methods ---------------------------------------------------------

    /**
     * Base64 encodes the specified bytes. This method is provided for signature compatibility with
     * commons-codec.
     
     @param bytes the bytes to encode
     @return the encoded bytes
     */
    public static byte[] encodeBase64(byte[] bytes)
    {
        return encode(bytes);
    }

    /**
     * Base64 encodes the specified string using the platform's default encoding.
     
     @param string the string to encode
     @return the encoded string
     */
    public static String encode(String string)
    {
        return new String(encode(string.getBytes()));
    }

    /**
     * Base64 encodes the specified bytes.
     
     @param bytes the bytes to encode
     @return the encoded bytes
     */
    public static byte[] encode(byte[] bytes)
    {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int count = 0;
        int carry = 0;

        for (int i = 0; i < bytes.length; i++)
        {
            byte b = bytes[i];

            switch (count++ % 3)
            {
                // first byte of 24-bits: write 6-bits and carry 2-bits
                case 0:
                    out.write(ENCODE[b >> 2]);
                    carry = b & 0x03;
                    break;

                // second byte of 24-bits: write carry + 4-bits, carry 4-bits
                case 1:
                    out.write(ENCODE[(carry << 4(b >> 4)]);
                    carry = b & 0x0F;
                    break;

                // third byte of 24-bits: write carry + 2-bits, write 6-bits
                case 2:
                    out.write(ENCODE[(carry << 2(b >> 6)]);
                    out.write(ENCODE[b & 0x3F]);
                    break;

                default:
                    throw new InternalError();
            }
        }

        switch (count % 3)
        {
            // third byte of 24-bits: 24-bit aligned
            case 0:
                break;

            // first byte of 24-bits: write 4-bit carry and pad 16-bits
            case 1:
                out.write(ENCODE[carry << 4]);
                out.write(PAD_CHAR);
                out.write(PAD_CHAR);
                break;

            // second byte of 24-bits: write 2-bit carry and pad 8-bits
            case 2:
                out.write(ENCODE[carry << 2]);
                out.write(PAD_CHAR);
                break;

            default:
                throw new InternalError();
        }

        return out.toByteArray();
    }
}

   
    
    
    
  
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. Base-64 Encoder - translates from base-64 text into binary
19. Base64 Character encoder as specified in RFC1113
20. Base64 Utils
21. Base64 encoder/decoder
22. Base64 from by Funambol, Inc.
23. Convert to hex from byte arrays and back
24. Converting hexadecimal strings
25. Encode and decode data in Base64 format as described in RFC 1521
26. Encode and decode integers, times, and internationalized strings to and from popular binary formats
27. Encoding of raw bytes to base64-encoded characters, and decoding of base64 characters to raw bytes
28. Performs Base64 encoding and/or decoding
29. Provides Base64 encoding and decoding as defined by RFC 2045
30. Provides Base64 encoding and decoding with URL and filename safe alphabet as defined by RFC 3548, section 4.
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.