Source Code Cross Referenced for Base64.java in  » IDE-Eclipse » ui-workbench » org » eclipse » ui » internal » preferences » 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 » IDE Eclipse » ui workbench » org.eclipse.ui.internal.preferences 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*******************************************************************************
002:         * Copyright (c) 2005, 2006 IBM Corporation and others.
003:         * All rights reserved. This program and the accompanying materials
004:         * are made available under the terms of the Eclipse Public License v1.0
005:         * which accompanies this distribution, and is available at
006:         * http://www.eclipse.org/legal/epl-v10.html
007:         *
008:         * Contributors:
009:         *     IBM Corporation - initial API and implementation
010:         *******************************************************************************/package org.eclipse.ui.internal.preferences;
011:
012:        /**
013:         * Base64 is a helper class for converting byte arrays to and 
014:         * from base 64 encoded Strings.
015:         *
016:         */
017:        class Base64 {
018:
019:            private static final byte equalSign = (byte) '=';
020:
021:            static char digits[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
022:                    'I', 'J', 'K', 'L', 'M',
023:                    'N',
024:                    'O',
025:                    'P', //
026:                    'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b',
027:                    'c', 'd',
028:                    'e',
029:                    'f', //
030:                    'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
031:                    's', 't', 'u',
032:                    'v', //
033:                    'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
034:                    '8', '9', '+', '/' };
035:
036:            /**
037:             * This method decodes the byte array in base 64 encoding into a char array
038:             * Base 64 encoding has to be according to the specification given by the
039:             * RFC 1521 (5.2).
040:             * 
041:             * @param data the encoded byte array
042:             * @return the decoded byte array
043:             */
044:            public static byte[] decode(byte[] data) {
045:                if (data.length == 0) {
046:                    return data;
047:                }
048:                int lastRealDataIndex = data.length - 1;
049:                while (data[lastRealDataIndex] == equalSign) {
050:                    lastRealDataIndex--;
051:                }
052:                // original data digit is 8 bits long, but base64 digit is 6 bits long
053:                int padBytes = data.length - 1 - lastRealDataIndex;
054:                int byteLength = data.length * 6 / 8 - padBytes;
055:                byte[] result = new byte[byteLength];
056:                // Each 4 bytes of input (encoded) we end up with 3 bytes of output
057:                int dataIndex = 0;
058:                int resultIndex = 0;
059:                int allBits = 0;
060:                // how many result chunks we can process before getting to pad bytes
061:                int resultChunks = (lastRealDataIndex + 1) / 4;
062:                for (int i = 0; i < resultChunks; i++) {
063:                    allBits = 0;
064:                    // Loop 4 times gathering input bits (4 * 6 = 24)
065:                    for (int j = 0; j < 4; j++) {
066:                        allBits = (allBits << 6)
067:                                | decodeDigit(data[dataIndex++]);
068:                    }
069:                    // Loop 3 times generating output bits (3 * 8 = 24)
070:                    for (int j = resultIndex + 2; j >= resultIndex; j--) {
071:                        result[j] = (byte) (allBits & 0xff); // Bottom 8 bits
072:                        allBits = allBits >>> 8;
073:                    }
074:                    resultIndex += 3; // processed 3 result bytes
075:                }
076:                // Now we do the extra bytes in case the original (non-encoded) data
077:                // was not multiple of 3 bytes
078:                switch (padBytes) {
079:                case 1:
080:                    // 1 pad byte means 3 (4-1) extra Base64 bytes of input, 18
081:                    // bits, of which only 16 are meaningful
082:                    // Or: 2 bytes of result data
083:                    allBits = 0;
084:                    // Loop 3 times gathering input bits
085:                    for (int j = 0; j < 3; j++) {
086:                        allBits = (allBits << 6)
087:                                | decodeDigit(data[dataIndex++]);
088:                    }
089:                    // NOTE - The code below ends up being equivalent to allBits =
090:                    // allBits>>>2
091:                    // But we code it in a non-optimized way for clarity
092:                    // The 4th, missing 6 bits are all 0
093:                    allBits = allBits << 6;
094:                    // The 3rd, missing 8 bits are all 0
095:                    allBits = allBits >>> 8;
096:                    // Loop 2 times generating output bits
097:                    for (int j = resultIndex + 1; j >= resultIndex; j--) {
098:                        result[j] = (byte) (allBits & 0xff); // Bottom 8
099:                        // bits
100:                        allBits = allBits >>> 8;
101:                    }
102:                    break;
103:                case 2:
104:                    // 2 pad bytes mean 2 (4-2) extra Base64 bytes of input, 12 bits
105:                    // of data, of which only 8 are meaningful
106:                    // Or: 1 byte of result data
107:                    allBits = 0;
108:                    // Loop 2 times gathering input bits
109:                    for (int j = 0; j < 2; j++) {
110:                        allBits = (allBits << 6)
111:                                | decodeDigit(data[dataIndex++]);
112:                    }
113:                    // NOTE - The code below ends up being equivalent to allBits =
114:                    // allBits>>>4
115:                    // But we code it in a non-optimized way for clarity
116:                    // The 3rd and 4th, missing 6 bits are all 0
117:                    allBits = allBits << 6;
118:                    allBits = allBits << 6;
119:                    // The 3rd and 4th, missing 8 bits are all 0
120:                    allBits = allBits >>> 8;
121:                    allBits = allBits >>> 8;
122:                    result[resultIndex] = (byte) (allBits & 0xff); // Bottom
123:                    // 8
124:                    // bits
125:                    break;
126:                }
127:                return result;
128:            }
129:
130:            /**
131:             * This method converts a Base 64 digit to its numeric value.
132:             * 
133:             * @param data digit (character) to convert
134:             * @return value for the digit
135:             */
136:            static int decodeDigit(byte data) {
137:                char charData = (char) data;
138:                if (charData <= 'Z' && charData >= 'A') {
139:                    return charData - 'A';
140:                }
141:                if (charData <= 'z' && charData >= 'a') {
142:                    return charData - 'a' + 26;
143:                }
144:                if (charData <= '9' && charData >= '0') {
145:                    return charData - '0' + 52;
146:                }
147:                switch (charData) {
148:                case '+':
149:                    return 62;
150:                case '/':
151:                    return 63;
152:                default:
153:                    throw new IllegalArgumentException(
154:                            "Invalid char to decode: " + data); //$NON-NLS-1$
155:                }
156:            }
157:
158:            /**
159:             * This method encodes the byte array into a char array in base 64 according
160:             * to the specification given by the RFC 1521 (5.2).
161:             * 
162:             * @param data the encoded char array
163:             * @return the byte array that needs to be encoded
164:             */
165:            public static byte[] encode(byte[] data) {
166:                int sourceChunks = data.length / 3;
167:                int len = ((data.length + 2) / 3) * 4;
168:                byte[] result = new byte[len];
169:                int extraBytes = data.length - (sourceChunks * 3);
170:                // Each 4 bytes of input (encoded) we end up with 3 bytes of output
171:                int dataIndex = 0;
172:                int resultIndex = 0;
173:                int allBits = 0;
174:                for (int i = 0; i < sourceChunks; i++) {
175:                    allBits = 0;
176:                    // Loop 3 times gathering input bits (3 * 8 = 24)
177:                    for (int j = 0; j < 3; j++) {
178:                        allBits = (allBits << 8) | (data[dataIndex++] & 0xff);
179:                    }
180:                    // Loop 4 times generating output bits (4 * 6 = 24)
181:                    for (int j = resultIndex + 3; j >= resultIndex; j--) {
182:                        result[j] = (byte) digits[(allBits & 0x3f)]; // Bottom
183:                        // 6
184:                        // bits
185:                        allBits = allBits >>> 6;
186:                    }
187:                    resultIndex += 4; // processed 4 result bytes
188:                }
189:                // Now we do the extra bytes in case the original (non-encoded) data
190:                // is not multiple of 4 bytes
191:                switch (extraBytes) {
192:                case 1:
193:                    allBits = data[dataIndex++]; // actual byte
194:                    allBits = allBits << 8; // 8 bits of zeroes
195:                    allBits = allBits << 8; // 8 bits of zeroes
196:                    // Loop 4 times generating output bits (4 * 6 = 24)
197:                    for (int j = resultIndex + 3; j >= resultIndex; j--) {
198:                        result[j] = (byte) digits[(allBits & 0x3f)]; // Bottom
199:                        // 6
200:                        // bits
201:                        allBits = allBits >>> 6;
202:                    }
203:                    // 2 pad tags
204:                    result[result.length - 1] = (byte) '=';
205:                    result[result.length - 2] = (byte) '=';
206:                    break;
207:                case 2:
208:                    allBits = data[dataIndex++]; // actual byte
209:                    allBits = (allBits << 8) | (data[dataIndex++] & 0xff); // actual
210:                    // byte
211:                    allBits = allBits << 8; // 8 bits of zeroes
212:                    // Loop 4 times generating output bits (4 * 6 = 24)
213:                    for (int j = resultIndex + 3; j >= resultIndex; j--) {
214:                        result[j] = (byte) digits[(allBits & 0x3f)]; // Bottom
215:                        // 6
216:                        // bits
217:                        allBits = allBits >>> 6;
218:                    }
219:                    // 1 pad tag
220:                    result[result.length - 1] = (byte) '=';
221:                    break;
222:                }
223:                return result;
224:            }
225:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.