Source Code Cross Referenced for Base64.java in  » XML-UI » xui32 » org » apache » catalina » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
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 Source Code / Java Documentation » XML UI » xui32 » org.apache.catalina.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *
003:         * ====================================================================
004:         *
005:         * The Apache Software License, Version 1.1
006:         *
007:         * Copyright (c) 1999 The Apache Software Foundation.  All rights
008:         * reserved.
009:         *
010:         * Redistribution and use in source and binary forms, with or without
011:         * modification, are permitted provided that the following conditions
012:         * are met:
013:         *
014:         * 1. Redistributions of source code must retain the above copyright
015:         *    notice, this list of conditions and the following disclaimer.
016:         *
017:         * 2. Redistributions in binary form must reproduce the above copyright
018:         *    notice, this list of conditions and the following disclaimer in
019:         *    the documentation and/or other materials provided with the
020:         *    distribution.
021:         *
022:         * 3. The end-user documentation included with the redistribution, if
023:         *    any, must include the following acknowlegement:
024:         *       "This product includes software developed by the
025:         *        Apache Software Foundation (http://www.apache.org/)."
026:         *    Alternately, this acknowlegement may appear in the software itself,
027:         *    if and wherever such third-party acknowlegements normally appear.
028:         *
029:         * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
030:         *    Foundation" must not be used to endorse or promote products derived
031:         *    from this software without prior written permission. For written
032:         *    permission, please contact apache@apache.org.
033:         *
034:         * 5. Products derived from this software may not be called "Apache"
035:         *    nor may "Apache" appear in their names without prior written
036:         *    permission of the Apache Group.
037:         *
038:         * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
039:         * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
040:         * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
041:         * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
042:         * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
043:         * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
044:         * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
045:         * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
046:         * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
047:         * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
048:         * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
049:         * SUCH DAMAGE.
050:         * ====================================================================
051:         *
052:         * This software consists of voluntary contributions made by many
053:         * individuals on behalf of the Apache Software Foundation.  For more
054:         * information on the Apache Software Foundation, please see
055:         * <http://www.apache.org/>.
056:         *
057:         * [Additional notices, if required by prior licensing conditions]
058:         *
059:         */
060:
061:        package org.apache.catalina.util;
062:
063:        /**
064:         * This class provides encode/decode for RFC 2045 Base64 as defined by
065:         * RFC 2045, N. Freed and N. Borenstein.  <a
066:         * href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a>:
067:         * Multipurpose Internet Mail Extensions (MIME) Part One: Format of
068:         * Internet Message Bodies. Reference 1996
069:         *
070:         * @author Jeffrey Rodriguez
071:         * @version $Id: Base64.java,v 1.3 2006/08/31 09:28:27 val Exp $
072:         */
073:        public final class Base64 {
074:            static private final int BASELENGTH = 255;
075:            static private final int LOOKUPLENGTH = 64;
076:            static private final int TWENTYFOURBITGROUP = 24;
077:            static private final int EIGHTBIT = 8;
078:            static private final int SIXTEENBIT = 16;
079:            static private final int SIXBIT = 6;
080:            static private final int FOURBYTE = 4;
081:            static private final int SIGN = -128;
082:            static private final byte PAD = (byte) '=';
083:            static private byte[] base64Alphabet = new byte[BASELENGTH];
084:            static private byte[] lookUpBase64Alphabet = new byte[LOOKUPLENGTH];
085:            //static private final Log log = LogSource.getInstance("org.apache.commons.util.Base64");
086:
087:            static {
088:                for (int i = 0; i < BASELENGTH; i++) {
089:                    base64Alphabet[i] = -1;
090:                }
091:                for (int i = 'Z'; i >= 'A'; i--) {
092:                    base64Alphabet[i] = (byte) (i - 'A');
093:                }
094:                for (int i = 'z'; i >= 'a'; i--) {
095:                    base64Alphabet[i] = (byte) (i - 'a' + 26);
096:                }
097:                for (int i = '9'; i >= '0'; i--) {
098:                    base64Alphabet[i] = (byte) (i - '0' + 52);
099:                }
100:
101:                base64Alphabet['+'] = 62;
102:                base64Alphabet['/'] = 63;
103:
104:                for (int i = 0; i <= 25; i++)
105:                    lookUpBase64Alphabet[i] = (byte) ('A' + i);
106:
107:                for (int i = 26, j = 0; i <= 51; i++, j++)
108:                    lookUpBase64Alphabet[i] = (byte) ('a' + j);
109:
110:                for (int i = 52, j = 0; i <= 61; i++, j++)
111:                    lookUpBase64Alphabet[i] = (byte) ('0' + j);
112:
113:                lookUpBase64Alphabet[62] = (byte) '+';
114:                lookUpBase64Alphabet[63] = (byte) '/';
115:            }
116:
117:            public static boolean isBase64(String isValidString) {
118:                return isArrayByteBase64(isValidString.getBytes());
119:            }
120:
121:            public static boolean isBase64(byte octect) {
122:                //shall we ignore white space? JEFF??
123:                return (octect == PAD || base64Alphabet[octect] != -1);
124:            }
125:
126:            public static boolean isArrayByteBase64(byte[] arrayOctect) {
127:                int length = arrayOctect.length;
128:                if (length == 0) {
129:                    // shouldn't a 0 length array be valid base64 data?
130:                    // return false;
131:                    return true;
132:                }
133:                for (int i = 0; i < length; i++) {
134:                    if (!Base64.isBase64(arrayOctect[i]))
135:                        return false;
136:                }
137:                return true;
138:            }
139:
140:            /**
141:             * Encodes hex octects into Base64.
142:             *
143:             * @param binaryData Array containing binary data to encode.
144:             * @return Base64-encoded data.
145:             */
146:            public static byte[] encode(byte[] binaryData) {
147:                int lengthDataBits = binaryData.length * EIGHTBIT;
148:                int fewerThan24bits = lengthDataBits % TWENTYFOURBITGROUP;
149:                int numberTriplets = lengthDataBits / TWENTYFOURBITGROUP;
150:                byte encodedData[] = null;
151:
152:                if (fewerThan24bits != 0) {
153:                    //data not divisible by 24 bit
154:                    encodedData = new byte[(numberTriplets + 1) * 4];
155:                } else {
156:                    // 16 or 8 bit
157:                    encodedData = new byte[numberTriplets * 4];
158:                }
159:
160:                byte k = 0, l = 0, b1 = 0, b2 = 0, b3 = 0;
161:
162:                int encodedIndex = 0;
163:                int dataIndex = 0;
164:                int i = 0;
165:                //log.debug("number of triplets = " + numberTriplets);
166:                for (i = 0; i < numberTriplets; i++) {
167:                    dataIndex = i * 3;
168:                    b1 = binaryData[dataIndex];
169:                    b2 = binaryData[dataIndex + 1];
170:                    b3 = binaryData[dataIndex + 2];
171:
172:                    //log.debug("b1= " + b1 +", b2= " + b2 + ", b3= " + b3);
173:
174:                    l = (byte) (b2 & 0x0f);
175:                    k = (byte) (b1 & 0x03);
176:
177:                    encodedIndex = i * 4;
178:                    byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2)
179:                            : (byte) ((b1) >> 2 ^ 0xc0);
180:                    byte val2 = ((b2 & SIGN) == 0) ? (byte) (b2 >> 4)
181:                            : (byte) ((b2) >> 4 ^ 0xf0);
182:                    byte val3 = ((b3 & SIGN) == 0) ? (byte) (b3 >> 6)
183:                            : (byte) ((b3) >> 6 ^ 0xfc);
184:
185:                    encodedData[encodedIndex] = lookUpBase64Alphabet[val1];
186:                    //log.debug( "val2 = " + val2 );
187:                    //log.debug( "k4   = " + (k<<4) );
188:                    //log.debug(  "vak  = " + (val2 | (k<<4)) );
189:                    encodedData[encodedIndex + 1] = lookUpBase64Alphabet[val2
190:                            | (k << 4)];
191:                    encodedData[encodedIndex + 2] = lookUpBase64Alphabet[(l << 2)
192:                            | val3];
193:                    encodedData[encodedIndex + 3] = lookUpBase64Alphabet[b3 & 0x3f];
194:                }
195:
196:                // form integral number of 6-bit groups
197:                dataIndex = i * 3;
198:                encodedIndex = i * 4;
199:                if (fewerThan24bits == EIGHTBIT) {
200:                    b1 = binaryData[dataIndex];
201:                    k = (byte) (b1 & 0x03);
202:                    //log.debug("b1=" + b1);
203:                    //log.debug("b1<<2 = " + (b1>>2) );
204:                    byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2)
205:                            : (byte) ((b1) >> 2 ^ 0xc0);
206:                    encodedData[encodedIndex] = lookUpBase64Alphabet[val1];
207:                    encodedData[encodedIndex + 1] = lookUpBase64Alphabet[k << 4];
208:                    encodedData[encodedIndex + 2] = PAD;
209:                    encodedData[encodedIndex + 3] = PAD;
210:                } else if (fewerThan24bits == SIXTEENBIT) {
211:
212:                    b1 = binaryData[dataIndex];
213:                    b2 = binaryData[dataIndex + 1];
214:                    l = (byte) (b2 & 0x0f);
215:                    k = (byte) (b1 & 0x03);
216:
217:                    byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2)
218:                            : (byte) ((b1) >> 2 ^ 0xc0);
219:                    byte val2 = ((b2 & SIGN) == 0) ? (byte) (b2 >> 4)
220:                            : (byte) ((b2) >> 4 ^ 0xf0);
221:
222:                    encodedData[encodedIndex] = lookUpBase64Alphabet[val1];
223:                    encodedData[encodedIndex + 1] = lookUpBase64Alphabet[val2
224:                            | (k << 4)];
225:                    encodedData[encodedIndex + 2] = lookUpBase64Alphabet[l << 2];
226:                    encodedData[encodedIndex + 3] = PAD;
227:                }
228:
229:                return encodedData;
230:            }
231:
232:            /**
233:             * Decodes Base64 data into octects
234:             *
235:             * @param base64Data Byte array containing Base64 data
236:             * @return Array containing decoded data.
237:             */
238:            public static byte[] decode(byte[] base64Data) {
239:                // handle the edge case, so we don't have to worry about it later
240:                if (base64Data.length == 0) {
241:                    return new byte[0];
242:                }
243:
244:                int numberQuadruple = base64Data.length / FOURBYTE;
245:                byte decodedData[] = null;
246:                byte b1 = 0, b2 = 0, b3 = 0, b4 = 0, marker0 = 0, marker1 = 0;
247:
248:                // Throw away anything not in base64Data
249:
250:                int encodedIndex = 0;
251:                int dataIndex = 0;
252:                {
253:                    // this sizes the output array properly - rlw
254:                    int lastData = base64Data.length;
255:                    // ignore the '=' padding
256:                    while (base64Data[lastData - 1] == PAD) {
257:                        if (--lastData == 0) {
258:                            return new byte[0];
259:                        }
260:                    }
261:                    decodedData = new byte[lastData - numberQuadruple];
262:                }
263:
264:                for (int i = 0; i < numberQuadruple; i++) {
265:                    dataIndex = i * 4;
266:                    marker0 = base64Data[dataIndex + 2];
267:                    marker1 = base64Data[dataIndex + 3];
268:
269:                    b1 = base64Alphabet[base64Data[dataIndex]];
270:                    b2 = base64Alphabet[base64Data[dataIndex + 1]];
271:
272:                    if (marker0 != PAD && marker1 != PAD) {
273:                        //No PAD e.g 3cQl
274:                        b3 = base64Alphabet[marker0];
275:                        b4 = base64Alphabet[marker1];
276:
277:                        decodedData[encodedIndex] = (byte) (b1 << 2 | b2 >> 4);
278:                        decodedData[encodedIndex + 1] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));
279:                        decodedData[encodedIndex + 2] = (byte) (b3 << 6 | b4);
280:                    } else if (marker0 == PAD) {
281:                        //Two PAD e.g. 3c[Pad][Pad]
282:                        decodedData[encodedIndex] = (byte) (b1 << 2 | b2 >> 4);
283:                    } else if (marker1 == PAD) {
284:                        //One PAD e.g. 3cQ[Pad]
285:                        b3 = base64Alphabet[marker0];
286:
287:                        decodedData[encodedIndex] = (byte) (b1 << 2 | b2 >> 4);
288:                        decodedData[encodedIndex + 1] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));
289:                    }
290:                    encodedIndex += 3;
291:                }
292:                return decodedData;
293:            }
294:
295:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.