Source Code Cross Referenced for Base64DecodeStream.java in  » Graphic-Library » batik » org » apache » batik » 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 » Graphic Library » batik » org.apache.batik.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:
003:           Licensed to the Apache Software Foundation (ASF) under one or more
004:           contributor license agreements.  See the NOTICE file distributed with
005:           this work for additional information regarding copyright ownership.
006:           The ASF licenses this file to You under the Apache License, Version 2.0
007:           (the "License"); you may not use this file except in compliance with
008:           the License.  You may obtain a copy of the License at
009:
010:               http://www.apache.org/licenses/LICENSE-2.0
011:
012:           Unless required by applicable law or agreed to in writing, software
013:           distributed under the License is distributed on an "AS IS" BASIS,
014:           WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015:           See the License for the specific language governing permissions and
016:           limitations under the License.
017:
018:         */
019:        package org.apache.batik.util;
020:
021:        import java.io.IOException;
022:        import java.io.InputStream;
023:
024:        /**
025:         * This class implements a Base64 Character decoder as specified in RFC1113.
026:         * Unlike some other encoding schemes there is nothing in this encoding that
027:         * tells the decoder where a buffer starts or stops, so to use it you will need
028:         * to isolate your encoded data into a single chunk and then feed them
029:         * this decoder. The simplest way to do that is to read all of the encoded
030:         * data into a string and then use:
031:         * <pre>
032:         *      byte    data[];
033:         *      InputStream is = new ByteArrayInputStream(data);
034:         *      is = new Base64DecodeStream(is);
035:         * </pre>
036:         *
037:         * On errors, this class throws a IOException with the following detail
038:         * strings:
039:         * <pre>
040:         *    "Base64DecodeStream: Bad Padding byte (2)."
041:         *    "Base64DecodeStream: Bad Padding byte (1)."
042:         * </pre>
043:         *
044:         * @author <a href="mailto:thomas.deweese@kodak.com">Thomas DeWeese</a>
045:         * @author <a href="mailto:vincent.hardy@eng.sun.com">Vincent Hardy</a>
046:         * @author      Chuck McManis
047:         * @version $Id: Base64DecodeStream.java 501495 2007-01-30 18:00:36Z dvholten $
048:         */
049:        public class Base64DecodeStream extends InputStream {
050:
051:            InputStream src;
052:
053:            public Base64DecodeStream(InputStream src) {
054:                this .src = src;
055:            }
056:
057:            private static final byte[] pem_array = new byte[256];
058:
059:            static {
060:                for (int i = 0; i < pem_array.length; i++)
061:                    pem_array[i] = -1;
062:
063:                int idx = 0;
064:                for (char c = 'A'; c <= 'Z'; c++) {
065:                    pem_array[c] = (byte) idx++;
066:                }
067:                for (char c = 'a'; c <= 'z'; c++) {
068:                    pem_array[c] = (byte) idx++;
069:                }
070:
071:                for (char c = '0'; c <= '9'; c++) {
072:                    pem_array[c] = (byte) idx++;
073:                }
074:
075:                pem_array['+'] = (byte) idx++;
076:                pem_array['/'] = (byte) idx++;
077:            }
078:
079:            public boolean markSupported() {
080:                return false;
081:            }
082:
083:            public void close() throws IOException {
084:                EOF = true;
085:            }
086:
087:            public int available() throws IOException {
088:                return 3 - out_offset;
089:            }
090:
091:            byte[] decode_buffer = new byte[4];
092:            byte[] out_buffer = new byte[3];
093:            int out_offset = 3;
094:            boolean EOF = false;
095:
096:            public int read() throws IOException {
097:
098:                if (out_offset == 3) {
099:                    if (EOF || getNextAtom()) {
100:                        EOF = true;
101:                        return -1;
102:                    }
103:                }
104:
105:                return ((int) out_buffer[out_offset++]) & 0xFF;
106:            }
107:
108:            public int read(byte[] out, int offset, int len) throws IOException {
109:
110:                int idx = 0;
111:                while (idx < len) {
112:                    if (out_offset == 3) {
113:                        if (EOF || getNextAtom()) {
114:                            EOF = true;
115:                            if (idx == 0)
116:                                return -1;
117:                            else
118:                                return idx;
119:                        }
120:                    }
121:
122:                    out[offset + idx] = out_buffer[out_offset++];
123:
124:                    idx++;
125:                }
126:                return idx;
127:            }
128:
129:            final boolean getNextAtom() throws IOException {
130:                int count, a, b, c, d;
131:
132:                int off = 0;
133:                while (off != 4) {
134:                    count = src.read(decode_buffer, off, 4 - off);
135:                    if (count == -1)
136:                        return true;
137:
138:                    int in = off, out = off;
139:                    while (in < off + count) {
140:                        if ((decode_buffer[in] != '\n')
141:                                && (decode_buffer[in] != '\r')
142:                                && (decode_buffer[in] != ' '))
143:                            decode_buffer[out++] = decode_buffer[in];
144:                        in++;
145:                    }
146:
147:                    off = out;
148:                }
149:
150:                a = pem_array[((int) decode_buffer[0]) & 0xFF];
151:                b = pem_array[((int) decode_buffer[1]) & 0xFF];
152:                c = pem_array[((int) decode_buffer[2]) & 0xFF];
153:                d = pem_array[((int) decode_buffer[3]) & 0xFF];
154:
155:                out_buffer[0] = (byte) ((a << 2) | (b >>> 4));
156:                out_buffer[1] = (byte) ((b << 4) | (c >>> 2));
157:                out_buffer[2] = (byte) ((c << 6) | d);
158:
159:                if (decode_buffer[3] != '=') {
160:                    // All three bytes are good.
161:                    out_offset = 0;
162:                } else if (decode_buffer[2] == '=') {
163:                    // Only one byte of output.
164:                    out_buffer[2] = out_buffer[0];
165:                    out_offset = 2;
166:                    EOF = true;
167:                } else {
168:                    // Only two bytes of output.
169:                    out_buffer[2] = out_buffer[1];
170:                    out_buffer[1] = out_buffer[0];
171:                    out_offset = 1;
172:                    EOF = true;
173:                }
174:
175:                return false;
176:            }
177:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.