Base64 from Eric Glass jcifs at samba dot org : Base64 Stream « File Input Output « 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 » File Input Output » Base64 StreamScreenshots 
Base64 from Eric Glass jcifs at samba dot org
    
/* Encodes and decodes to and from Base64 notation.
 * Copyright (C) 2003 "Eric Glass" <jcifs at samba dot org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


public class Base64 {

    private static final String ALPHABET =
            "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    /**
     * Base-64 encodes the supplied block of data.  Line wrapping is not
     * applied on output.
     *
     @param bytes The block of data that is to be Base-64 encoded.
     @return A <code>String</code> containing the encoded data.
     */
    public static String encode(byte[] bytes) {
        int length = bytes.length;
        if (length == 0return "";
        StringBuffer buffer =
                new StringBuffer((intMath.ceil((doublelength / 3d4);
        int remainder = length % 3;
        length -= remainder;
        int block;
        int i = 0;
        while (i < length) {
            block = ((bytes[i++0xff<< 16((bytes[i++0xff<< 8|
                    (bytes[i++0xff);
            buffer.append(ALPHABET.charAt(block >>> 18));
            buffer.append(ALPHABET.charAt((block >>> 120x3f));
            buffer.append(ALPHABET.charAt((block >>> 60x3f));
            buffer.append(ALPHABET.charAt(block & 0x3f));
        }
        if (remainder == 0return buffer.toString();
        if (remainder == 1) {
            block = (bytes[i0xff<< 4;
            buffer.append(ALPHABET.charAt(block >>> 6));
            buffer.append(ALPHABET.charAt(block & 0x3f));
            buffer.append("==");
            return buffer.toString();
        }
        block = (((bytes[i++0xff<< 8((bytes[i]) 0xff)) << 2;
        buffer.append(ALPHABET.charAt(block >>> 12));
        buffer.append(ALPHABET.charAt((block >>> 60x3f));
        buffer.append(ALPHABET.charAt(block & 0x3f));
        buffer.append("=");
        return buffer.toString();
    }

    /**
     * Decodes the supplied Base-64 encoded string.
     *
     @param string The Base-64 encoded string that is to be decoded.
     @return A <code>byte[]</code> containing the decoded data block.
     */
    public static byte[] decode(String string) {
        int length = string.length();
        if (length == 0return new byte[0];
        int pad = (string.charAt(length - 2== '=':
                (string.charAt(length - 1== '='0;
        int size = length * - pad;
        byte[] buffer = new byte[size];
        int block;
        int i = 0;
        int index = 0;
        while (i < length) {
            block = (ALPHABET.indexOf(string.charAt(i++)) 0xff<< 18 |
                    (ALPHABET.indexOf(string.charAt(i++)) 0xff<< 12 |
                    (ALPHABET.indexOf(string.charAt(i++)) 0xff<< |
                    (ALPHABET.indexOf(string.charAt(i++)) 0xff);
            buffer[index++(byte) (block >>> 16);
            if (index < sizebuffer[index++(byte) ((block >>> 80xff);
            if (index < sizebuffer[index++(byte) (block & 0xff);
        }
        return buffer;
    }

}

   
    
    
    
  
Related examples in the same category
1. BASE64 Decoder Stream
2. BASE64 Encoder Stream from Sun Microsystems
3. Base64 Character decoder as specified in RFC1113.
4. Base64 Character encoder as specified in RFC1113.
5. Performs Base-64 decoding on an underlying stream.
6. Class encodes the bytes written to the OutPutStream to a Base64 encoded string.
7. BASE64 Decoder Stream from Sun Microsystems
8. BASE64 Encoder Stream
9. Decode a BASE64 encoded input stream to some output stream
10. Hex dump
11. Dumps data in hexadecimal format
12. Apply a ASCII Hex encoding to the stream
13. Base64 Codec
14. Base64 encoding from DbUnit.org
15. Base64 provides Base64 encoding/decoding of strings and streams
16. Base64 - encode/decode data using the Base64 encoding scheme
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.