KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > Base64


1 /*
2  * Copyright (c) 2002, 2005 Gargoyle Software Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  * 3. The end-user documentation included with the redistribution, if any, must
13  * include the following acknowledgment:
14  *
15  * "This product includes software developed by Gargoyle Software Inc.
16  * (http://www.GargoyleSoftware.com/)."
17  *
18  * Alternately, this acknowledgment may appear in the software itself, if
19  * and wherever such third-party acknowledgments normally appear.
20  * 4. The name "Gargoyle Software" must not be used to endorse or promote
21  * products derived from this software without prior written permission.
22  * For written permission, please contact info@GargoyleSoftware.com.
23  * 5. Products derived from this software may not be called "HtmlUnit", nor may
24  * "HtmlUnit" appear in their name, without prior written permission of
25  * Gargoyle Software Inc.
26  *
27  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30  * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */

38 package com.gargoylesoftware.htmlunit;
39
40 import java.io.UnsupportedEncodingException JavaDoc;
41
42 /**
43  * A utility class for base 64 encoding.
44  *
45  * @version $Revision: 100 $
46  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
47  * @deprecated Use {@link org.apache.commons.codec.binary.Base64} instead. Will be removed in a future version.
48  */

49 public final class Base64 {
50     /** The encoding table */
51     private static final byte[] ENCODING_TABLE;
52     /** The padding byte */
53     private static final byte PADDING_BYTE;
54
55     static {
56         final String JavaDoc table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
57         try {
58             ENCODING_TABLE = table.getBytes( "ISO-8859-1" );
59             PADDING_BYTE = "=".getBytes( "ISO-8859-1" )[0];
60         }
61         catch( final UnsupportedEncodingException JavaDoc e ) {
62             throw new IllegalStateException JavaDoc(
63                 "Theoretically impossible: ISO-8859-1 (us-ascii) is not a supported encoding" );
64         }
65
66         if( ENCODING_TABLE.length != 64 ) {
67             throw new IllegalStateException JavaDoc( "Encoding table doesn't contain 64 values" );
68         }
69     }
70
71     /** Private constructor to prevent instantiation */
72     private Base64() {
73     }
74
75
76     /**
77      * Encode the specified string as base 64.
78      *
79      * @param input The input string
80      * @return The encoded string
81      */

82     public static String JavaDoc encode( final String JavaDoc input ) {
83         try {
84             return encode( input, "ISO-8859-1" );
85         }
86         catch( final UnsupportedEncodingException JavaDoc e ) {
87             throw new IllegalStateException JavaDoc(
88                 "Theoretically impossible: ISO-8859-1 (us-ascii) is not a supported encoding" );
89         }
90     }
91
92
93     /**
94      * Encode the string as base 64 using the specified encoding
95      *
96      * @param input The input string
97      * @param characterEncoding The encoding to use for the input string
98      * @return The encoded string
99      * @exception UnsupportedEncodingException If the specified encoding is invalid.
100      */

101     public static String JavaDoc encode( final String JavaDoc input, final String JavaDoc characterEncoding )
102         throws UnsupportedEncodingException JavaDoc {
103
104         return new String JavaDoc( encode( input.getBytes( characterEncoding ) ), characterEncoding );
105     }
106
107
108     /**
109      * Encode the specified byte as base 64.
110      *
111      * @param array The input array
112      * @return The encoded byte array
113      */

114     public static byte[] encode( final byte[] array ) {
115         final int paddingCharCount = ( 3 - ( array.length % 3 ) ) % 3;
116
117         final byte[] input;
118
119         if( paddingCharCount == 0 ) {
120             input = array;
121         }
122         else {
123             input = new byte[array.length + paddingCharCount];
124             System.arraycopy( array, 0, input, 0, array.length );
125         }
126
127         final byte output[] = new byte[( input.length * 4 / 3 )];
128         int outputIndex = 0;
129
130         byte byte1;
131         byte byte2;
132         byte byte3;
133
134         for( int i = 0; i < input.length; i += 3 ) {
135             byte1 = input[i];
136             byte2 = input[i + 1];
137             byte3 = input[i + 2];
138
139             output[outputIndex++] = ENCODING_TABLE[( byte1 & 0xFC ) >>> 2];
140             output[outputIndex++] = ENCODING_TABLE[( ( byte1 & 0x03 ) << 4 )
141                 | ( ( byte2 & 0xF0 ) >>> 4 )];
142             output[outputIndex++] = ENCODING_TABLE[( ( byte2 & 0x0F ) << 2 )
143                 | ( ( byte3 & 0xC0 ) >>> 6 )];
144             output[outputIndex++] = ENCODING_TABLE[byte3 & 0x3F];
145         }
146
147         if( paddingCharCount > 1 ) {
148             output[--outputIndex] = PADDING_BYTE;
149         }
150         if( paddingCharCount > 0 ) {
151             output[--outputIndex] = PADDING_BYTE;
152         }
153
154         return output;
155     }
156 }
157
158
Popular Tags