BASE64 encoder implementation : 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 
BASE64 encoder implementation
    
/*
 * Copyright © World Wide Web Consortium, (Massachusetts Institute of Technology, 
 * Institut National de Recherche en Informatique et en Automatique, Keio University).
 * All Rights Reserved. http://www.w3.org/Consortium/Legal/
 */

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

/**
 * BASE64 encoder implementation.
 * This object takes as parameter an input stream and an output stream. It
 * encodes the input stream, using the BASE64 encoding rules, as defined
 * in <a href="http://ds.internic.net/rfc/rfc1521.txt">MIME specification</a>
 * and emit the resulting data to the output stream.
 @see org.w3c.tools.codec.Base64Decoder
 */
public class Base64Encoder
{
  private static final int BUFFER_SIZE = 1024;
  private static byte encoding[] =
    {
      (byte'A'(byte'B'(byte'C'(byte'D'(byte'E'(byte'F'(byte'G'(byte'H'// 0-7
      (byte'I'(byte'J'(byte'K'(byte'L'(byte'M'(byte'N'(byte'O'(byte'P'// 8-15
      (byte'Q'(byte'R'(byte'S'(byte'T'(byte'U'(byte'V'(byte'W'(byte'X'// 16-23
      (byte'Y'(byte'Z'(byte'a'(byte'b'(byte'c'(byte'd'(byte'e'(byte'f'// 24-31
      (byte'g'(byte'h'(byte'i'(byte'j'(byte'k'(byte'l'(byte'm'(byte'n'// 32-39
      (byte'o'(byte'p'(byte'q'(byte'r'(byte's'(byte't'(byte'u'(byte'v'// 40-47
      (byte'w'(byte'x'(byte'y'(byte'z'(byte'0'(byte'1'(byte'2'(byte'3'// 48-55
      (byte'4'(byte'5'(byte'6'(byte'7'(byte'8'(byte'9'(byte'+'(byte'/'// 56-63
      (byte'=' // 64
  };

  InputStream in = null;
  OutputStream out = null;
  boolean stringp = false;

  private final int get1(byte buf[]int off)
  {
    return (buf[off0xfc>> 2;
  }

  private final int get2(byte buf[]int off)
  {
    return ((buf[off0x3<< 4((buf[off + 10xf0>>> 4);
  }

  private final int get3(byte buf[]int off)
  {
    return ((buf[off + 10x0f<< 2((buf[off + 20xc0>>> 6);
  }

  private static final int get4(byte buf[]int off)
  {
    return buf[off + 20x3f;
  }

  /**
   * Process the data: encode the input stream to the output stream.
   * This method runs through the input stream, encoding it to the output 
   * stream.
   @exception IOException If we weren't able to access the input stream or
   *    the output stream.
   */
  public void process() throws IOException
  {
    byte buffer[] new byte[BUFFER_SIZE];
    int got = -1;
    int off = 0;
    int count = 0;
    while ((got = in.read(buffer, off, BUFFER_SIZE - off)) 0)
    {
      if (got >= 3)
      {
        got += off;
        off = 0;
        while (off + <= got)
        {
          int c1 = get1(buffer, off);
          int c2 = get2(buffer, off);
          int c3 = get3(buffer, off);
          int c4 = get4(buffer, off);
          switch (count)
          {
            case 73 :
              out.write(encoding[c1]);
              out.write(encoding[c2]);
              out.write(encoding[c3]);
              out.write('\n');
              out.write(encoding[c4]);
              count = 1;
              break;
            case 74 :
              out.write(encoding[c1]);
              out.write(encoding[c2]);
              out.write('\n');
              out.write(encoding[c3]);
              out.write(encoding[c4]);
              count = 2;
              break;
            case 75 :
              out.write(encoding[c1]);
              out.write('\n');
              out.write(encoding[c2]);
              out.write(encoding[c3]);
              out.write(encoding[c4]);
              count = 3;
              break;
            case 76 :
              out.write('\n');
              out.write(encoding[c1]);
              out.write(encoding[c2]);
              out.write(encoding[c3]);
              out.write(encoding[c4]);
              count = 4;
              break;
            default :
              out.write(encoding[c1]);
              out.write(encoding[c2]);
              out.write(encoding[c3]);
              out.write(encoding[c4]);
              count += 4;
              break;
          }
          off += 3;
        }
        // Copy remaining bytes to beginning of buffer:
        for (int i = 0; i < 3; i++)
          buffer[i(i < got - off? buffer[off + i((byte0);
        off = got - off;
      }
      else
      {
        // Total read amount is less then 3 bytes:
        off += got;
      }
    }
    // Manage the last bytes, from 0 to off:
    switch (off)
    {
      case :
        out.write(encoding[get1(buffer, 0)]);
        out.write(encoding[get2(buffer, 0)]);
        out.write('=');
        out.write('=');
        break;
      case :
        out.write(encoding[get1(buffer, 0)]);
        out.write(encoding[get2(buffer, 0)]);
        out.write(encoding[get3(buffer, 0)]);
        out.write('=');
    }
    return;
  }

  /**
   * Encode the content of this encoder, as a string.
   * This methods encode the String content, that was provided at creation 
   * time, following the BASE64 rules, as specified in the rfc1521.
   @return A String, reprenting the encoded content of the input String.
   */
  public String processString()
  {
    if (!stringp)
      throw new RuntimeException(
        this.getClass().getName()
          "[processString]"
          "invalid call (not a String)");
    try
    {
      process();
    }
    catch (IOException e)
    {
    }
    return ((ByteArrayOutputStreamout).toString();
  }

  /**
   * Create a new Base64 encoder, to encode the given string.
   @param input The String to be encoded.
   */
  public Base64Encoder(String input)
  {
    byte bytes[];
    try
    {
      bytes = input.getBytes("ISO-8859-1");
    }
    catch (UnsupportedEncodingException ex)
    {
      throw new RuntimeException(
        this.getClass().getName()
          "[Constructor] Unable to convert"
          "properly char to bytes");
    }
    this.stringp = true;
    this.in = new ByteArrayInputStream(bytes);
    this.out = new ByteArrayOutputStream();
  }

  /**
   * Create a new Base64 encoder, encoding input to output.
   @param in The input stream to be encoded.
   @param out The output stream, to write encoded data to.
   */
  public Base64Encoder(InputStream in, OutputStream out)
  {
    this.in = in;
    this.out = out;
    this.stringp = false;
  }

  /**
   * Testing the encoder.
   * Run with one argument, prints the encoded version of it.
   */
  public static void main(String args[])
  {
    if (args.length != 1)
    {
      System.out.println("Base64Encoder <string>");
      System.exit(0);
    }
    Base64Encoder b = new Base64Encoder(args[0]);
    System.out.println("[" + b.processString() "]");
    // joe:eoj -> am9lOmVvag==
    // 12345678:87654321 -> MTIzNDU2Nzg6ODc2NTQzMjE=
  }
}

   
    
    
    
  
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. Base-64 Encoder - translates from base-64 text into binary
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.