Source Code Cross Referenced for Base64.java in  » Apache-Harmony-Java-SE » org-package » org » apache » harmony » luni » 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 » Apache Harmony Java SE » org package » org.apache.harmony.luni.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
003:         *  contributor license agreements.  See the NOTICE file distributed with
004:         *  this work for additional information regarding copyright ownership.
005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
006:         *  (the "License"); you may not use this file except in compliance with
007:         *  the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         *  Unless required by applicable law or agreed to in writing, software
012:         *  distributed under the License is distributed on an "AS IS" BASIS,
013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         *  See the License for the specific language governing permissions and
015:         *  limitations under the License.
016:         */
017:
018:        /**
019:         * @author Alexander Y. Kleymenov
020:         * @version $Revision$
021:         */package org.apache.harmony.luni.util;
022:
023:        import java.io.UnsupportedEncodingException;
024:
025:        /**
026:         * This class implements Base64 encoding/decoding functionality
027:         * as specified in RFC 2045 (http://www.ietf.org/rfc/rfc2045.txt).
028:         */
029:        public class Base64 {
030:
031:            public static byte[] decode(byte[] in) {
032:                return decode(in, in.length);
033:            }
034:
035:            public static byte[] decode(byte[] in, int len) {
036:                // approximate output length
037:                int length = len / 4 * 3;
038:                // return an empty array on emtpy or short input without padding
039:                if (length == 0) {
040:                    return new byte[0];
041:                }
042:                // temporary array
043:                byte[] out = new byte[length];
044:                // number of padding characters ('=')
045:                int pad = 0;
046:                byte chr;
047:                // compute the number of the padding characters
048:                // and adjust the length of the input
049:                for (;; len--) {
050:                    chr = in[len - 1];
051:                    // skip the neutral characters
052:                    if ((chr == '\n') || (chr == '\r') || (chr == ' ')
053:                            || (chr == '\t')) {
054:                        continue;
055:                    }
056:                    if (chr == '=') {
057:                        pad++;
058:                    } else {
059:                        break;
060:                    }
061:                }
062:                // index in the output array
063:                int out_index = 0;
064:                // index in the input array
065:                int in_index = 0;
066:                // holds the value of the input character
067:                int bits = 0;
068:                // holds the value of the input quantum
069:                int quantum = 0;
070:                for (int i = 0; i < len; i++) {
071:                    chr = in[i];
072:                    // skip the neutral characters
073:                    if ((chr == '\n') || (chr == '\r') || (chr == ' ')
074:                            || (chr == '\t')) {
075:                        continue;
076:                    }
077:                    if ((chr >= 'A') && (chr <= 'Z')) {
078:                        // char ASCII value
079:                        //  A    65    0
080:                        //  Z    90    25 (ASCII - 65)
081:                        bits = chr - 65;
082:                    } else if ((chr >= 'a') && (chr <= 'z')) {
083:                        // char ASCII value
084:                        //  a    97    26
085:                        //  z    122   51 (ASCII - 71)
086:                        bits = chr - 71;
087:                    } else if ((chr >= '0') && (chr <= '9')) {
088:                        // char ASCII value
089:                        //  0    48    52
090:                        //  9    57    61 (ASCII + 4)
091:                        bits = chr + 4;
092:                    } else if (chr == '+') {
093:                        bits = 62;
094:                    } else if (chr == '/') {
095:                        bits = 63;
096:                    } else {
097:                        return null;
098:                    }
099:                    // append the value to the quantum
100:                    quantum = (quantum << 6) | (byte) bits;
101:                    if (in_index % 4 == 3) {
102:                        // 4 characters were read, so make the output:
103:                        out[out_index++] = (byte) ((quantum & 0x00FF0000) >> 16);
104:                        out[out_index++] = (byte) ((quantum & 0x0000FF00) >> 8);
105:                        out[out_index++] = (byte) (quantum & 0x000000FF);
106:                    }
107:                    in_index++;
108:                }
109:                if (pad > 0) {
110:                    // adjust the quantum value according to the padding
111:                    quantum = quantum << (6 * pad);
112:                    // make output
113:                    out[out_index++] = (byte) ((quantum & 0x00FF0000) >> 16);
114:                    if (pad == 1) {
115:                        out[out_index++] = (byte) ((quantum & 0x0000FF00) >> 8);
116:                    }
117:                }
118:                // create the resulting array
119:                byte[] result = new byte[out_index];
120:                System.arraycopy(out, 0, result, 0, out_index);
121:                return result;
122:            }
123:
124:            private static final byte[] map = new byte[] { 'A', 'B', 'C', 'D',
125:                    'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
126:                    'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b',
127:                    'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
128:                    'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
129:                    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };
130:
131:            public static String encode(byte[] in, String charsetName)
132:                    throws UnsupportedEncodingException {
133:                int length = in.length * 4 / 3;
134:                length += length / 76 + 3; // for crlr
135:                byte[] out = new byte[length];
136:                int index = 0, i, crlr = 0, end = in.length - in.length % 3;
137:                for (i = 0; i < end; i += 3) {
138:                    out[index++] = map[(in[i] & 0xff) >> 2];
139:                    out[index++] = map[((in[i] & 0x03) << 4)
140:                            | ((in[i + 1] & 0xff) >> 4)];
141:                    out[index++] = map[((in[i + 1] & 0x0f) << 2)
142:                            | ((in[i + 2] & 0xff) >> 6)];
143:                    out[index++] = map[(in[i + 2] & 0x3f)];
144:                    if (((index - crlr) % 76 == 0) && (index != 0)) {
145:                        out[index++] = '\n';
146:                        crlr++;
147:                        //out[index++] = '\r';
148:                        //crlr++;
149:                    }
150:                }
151:                switch (in.length % 3) {
152:                case 1:
153:                    out[index++] = map[(in[end] & 0xff) >> 2];
154:                    out[index++] = map[(in[end] & 0x03) << 4];
155:                    out[index++] = '=';
156:                    out[index++] = '=';
157:                    break;
158:                case 2:
159:                    out[index++] = map[(in[end] & 0xff) >> 2];
160:                    out[index++] = map[((in[end] & 0x03) << 4)
161:                            | ((in[end + 1] & 0xff) >> 4)];
162:                    out[index++] = map[((in[end + 1] & 0x0f) << 2)];
163:                    out[index++] = '=';
164:                    break;
165:                }
166:                return new String(out, 0, index, charsetName);
167:            }
168:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.