Source Code Cross Referenced for Base64.java in  » J2EE » JOnAS-4.8.6 » org » objectweb » jonas » security » realm » lib » 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 » J2EE » JOnAS 4.8.6 » org.objectweb.jonas.security.realm.lib 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * Base64 - encode/decode data using the Base64 encoding scheme
003:         * Copyright (c) 1998 by Kevin Kelley
004:         * Contact: Kevin Kelley <kelley@ruralnet.net>
005:         * 30718 Rd. 28, La Junta, CO, 81050  USA.
006:         *
007:         * This library is free software; you can redistribute it and/or
008:         * modify it under the terms of the GNU Lesser General Public
009:         * License as published by the Free Software Foundation; either
010:         * version 2.1 of the License, or 1any later version.
011:         *
012:         * This library is distributed in the hope that it will be useful,
013:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
014:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015:         * Lesser General Public License for more details.
016:         *
017:         * You should have received a copy of the GNU Lesser General Public
018:         * License along with this library; if not, write to the Free Software
019:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
020:         * USA
021:         *
022:         */package org.objectweb.jonas.security.realm.lib;
023:
024:        /**
025:         *   Provides encoding of raw bytes to base64-encoded characters, and
026:         *  decoding of base64 characters to raw bytes.
027:         *
028:         * @author Kevin Kelley (kelley@ruralnet.net)
029:         * @version 1.3
030:         * @date 06 August 1998
031:         * @modified 14 February 2000
032:         * @modified 22 September 2000
033:         */
034:        public class Base64 {
035:
036:            /**
037:             * code characters for values 0..63
038:             */
039:            private static char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
040:                    .toCharArray();
041:
042:            /**
043:             * lookup table for converting base64 characters to value in range 0..63
044:             */
045:            private static byte[] codes = new byte[256];
046:
047:            static {
048:                for (int i = 0; i < 256; i++) {
049:                    codes[i] = -1;
050:                }
051:                for (int i = 'A'; i <= 'Z'; i++) {
052:                    codes[i] = (byte) (i - 'A');
053:                }
054:                for (int i = 'a'; i <= 'z'; i++) {
055:                    codes[i] = (byte) (26 + i - 'a');
056:                }
057:                for (int i = '0'; i <= '9'; i++) {
058:                    codes[i] = (byte) (52 + i - '0');
059:                }
060:                codes['+'] = 62;
061:                codes['/'] = 63;
062:            }
063:
064:            /**
065:             * returns an array of base64-encoded characters to represent the
066:             * passed data array.
067:             *
068:             * @param data the array of bytes to encode
069:             * @return base64-coded character array.
070:             */
071:            public static char[] encode(byte[] data) {
072:                char[] out = new char[((data.length + 2) / 3) * 4];
073:
074:                //
075:                // 3 bytes encode to 4 chars.  Output is always an even
076:                // multiple of 4 characters.
077:                //
078:                for (int i = 0, index = 0; i < data.length; i += 3, index += 4) {
079:                    boolean quad = false;
080:                    boolean trip = false;
081:
082:                    int val = (0xFF & (int) data[i]);
083:                    val <<= 8;
084:                    if ((i + 1) < data.length) {
085:                        val |= (0xFF & (int) data[i + 1]);
086:                        trip = true;
087:                    }
088:                    val <<= 8;
089:                    if ((i + 2) < data.length) {
090:                        val |= (0xFF & (int) data[i + 2]);
091:                        quad = true;
092:                    }
093:                    out[index + 3] = alphabet[(quad ? (val & 0x3F) : 64)];
094:                    val >>= 6;
095:                    out[index + 2] = alphabet[(trip ? (val & 0x3F) : 64)];
096:                    val >>= 6;
097:                    out[index + 1] = alphabet[val & 0x3F];
098:                    val >>= 6;
099:                    out[index + 0] = alphabet[val & 0x3F];
100:                }
101:                return out;
102:            }
103:
104:            /**
105:             * Decodes a BASE-64 encoded stream to recover the original
106:             * data. White space before and after will be trimmed away,
107:             * but no other manipulation of the input will be performed.
108:             *
109:             * As of version 1.2 this method will properly handle input
110:             * containing junk characters (newlines and the like) rather
111:             * than throwing an error. It does this by pre-parsing the
112:             * input and generating from that a count of VALID input
113:             * characters.
114:             * @param data BASE-64 encoded stream
115:             * @return original data
116:             **/
117:            public static byte[] decode(char[] data) {
118:                // as our input could contain non-BASE64 data (newlines,
119:                // whitespace of any sort, whatever) we must first adjust
120:                // our count of USABLE data so that...
121:                // (a) we don't misallocate the output array, and
122:                // (b) think that we miscalculated our data length
123:                //     just because of extraneous throw-away junk
124:
125:                int tempLen = data.length;
126:                for (int ix = 0; ix < data.length; ix++) {
127:                    if ((data[ix] > 255) || codes[data[ix]] < 0) {
128:                        --tempLen; // ignore non-valid chars and padding
129:                    }
130:                }
131:                // calculate required length:
132:                //  -- 3 bytes for every 4 valid base64 chars
133:                //  -- plus 2 bytes if there are 3 extra base64 chars,
134:                //     or plus 1 byte if there are 2 extra.
135:
136:                int len = (tempLen / 4) * 3;
137:                if ((tempLen % 4) == 3) {
138:                    len += 2;
139:                }
140:                if ((tempLen % 4) == 2) {
141:                    len += 1;
142:                }
143:
144:                byte[] out = new byte[len];
145:
146:                int shift = 0; // # of excess bits stored in accum
147:                int accum = 0; // excess bits
148:                int index = 0;
149:
150:                // we now go through the entire array (NOT using the 'tempLen' value)
151:                for (int ix = 0; ix < data.length; ix++) {
152:                    int value = (data[ix] > 255) ? -1 : codes[data[ix]];
153:
154:                    if (value >= 0) { // skip over non-code
155:                        accum <<= 6; // bits shift up by 6 each time thru
156:                        shift += 6; // loop, with new bits being put in
157:                        accum |= value; // at the bottom.
158:                        if (shift >= 8) { // whenever there are 8 or more shifted in,
159:                            shift -= 8; // write them out (from the top, leaving any
160:                            out[index++] = // excess at the bottom for next iteration.
161:                            (byte) ((accum >> shift) & 0xff);
162:                        }
163:                    }
164:                    // we will also have skipped processing a padding null byte ('=') here;
165:                    // these are used ONLY for padding to an even length and do not legally
166:                    // occur as encoded data. for this reason we can ignore the fact that
167:                    // no index++ operation occurs in that special case: the out[] array is
168:                    // initialized to all-zero bytes to start with and that works to our
169:                    // advantage in this combination.
170:                }
171:
172:                // if there is STILL something wrong we just have to throw up now!
173:                if (index != out.length) {
174:                    throw new Error("Miscalculated data length (wrote " + index
175:                            + " instead of " + out.length + ")");
176:                }
177:
178:                return out;
179:            }
180:
181:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.