Source Code Cross Referenced for QPDecoderStream.java in  » EJB-Server-GlassFish » mail » com » sun » mail » 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 » EJB Server GlassFish » mail » com.sun.mail.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003:         *
004:         * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005:         *
006:         * The contents of this file are subject to the terms of either the GNU
007:         * General Public License Version 2 only ("GPL") or the Common Development
008:         * and Distribution License("CDDL") (collectively, the "License").  You
009:         * may not use this file except in compliance with the License. You can obtain
010:         * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011:         * or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific
012:         * language governing permissions and limitations under the License.
013:         *
014:         * When distributing the software, include this License Header Notice in each
015:         * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016:         * Sun designates this particular file as subject to the "Classpath" exception
017:         * as provided by Sun in the GPL Version 2 section of the License file that
018:         * accompanied this code.  If applicable, add the following below the License
019:         * Header, with the fields enclosed by brackets [] replaced by your own
020:         * identifying information: "Portions Copyrighted [year]
021:         * [name of copyright owner]"
022:         *
023:         * Contributor(s):
024:         *
025:         * If you wish your version of this file to be governed by only the CDDL or
026:         * only the GPL Version 2, indicate your decision by adding "[Contributor]
027:         * elects to include this software in this distribution under the [CDDL or GPL
028:         * Version 2] license."  If you don't indicate a single choice of license, a
029:         * recipient has the option to distribute your version of this file under
030:         * either the CDDL, the GPL Version 2 or to extend the choice of license to
031:         * its licensees as provided above.  However, if you add GPL Version 2 code
032:         * and therefore, elected the GPL Version 2 license, then the option applies
033:         * only if the new code is made subject to such option by the copyright
034:         * holder.
035:         */
036:
037:        /*
038:         * @(#)QPDecoderStream.java	1.11 07/05/04
039:         */
040:
041:        package com.sun.mail.util;
042:
043:        import java.io.*;
044:
045:        /**
046:         * This class implements a QP Decoder. It is implemented as
047:         * a FilterInputStream, so one can just wrap this class around
048:         * any input stream and read bytes from this filter. The decoding
049:         * is done as the bytes are read out.
050:         * 
051:         * @author John Mani
052:         */
053:
054:        public class QPDecoderStream extends FilterInputStream {
055:            protected byte[] ba = new byte[2];
056:            protected int spaces = 0;
057:
058:            /**
059:             * Create a Quoted Printable decoder that decodes the specified 
060:             * input stream.
061:             * @param in        the input stream
062:             */
063:            public QPDecoderStream(InputStream in) {
064:                super (new PushbackInputStream(in, 2)); // pushback of size=2
065:            }
066:
067:            /**
068:             * Read the next decoded byte from this input stream. The byte
069:             * is returned as an <code>int</code> in the range <code>0</code>
070:             * to <code>255</code>. If no byte is available because the end of
071:             * the stream has been reached, the value <code>-1</code> is returned.
072:             * This method blocks until input data is available, the end of the
073:             * stream is detected, or an exception is thrown.
074:             *
075:             * @return     the next byte of data, or <code>-1</code> if the end of the
076:             *             stream is reached.
077:             * @exception  IOException  if an I/O error occurs.
078:             */
079:            public int read() throws IOException {
080:                if (spaces > 0) {
081:                    // We have cached space characters, return one
082:                    spaces--;
083:                    return ' ';
084:                }
085:
086:                int c = in.read();
087:
088:                if (c == ' ') {
089:                    // Got space, keep reading till we get a non-space char
090:                    while ((c = in.read()) == ' ')
091:                        spaces++;
092:
093:                    if (c == '\r' || c == '\n' || c == -1)
094:                        // If the non-space char is CR/LF/EOF, the spaces we got
095:                        // so far is junk introduced during transport. Junk 'em.
096:                        spaces = 0;
097:                    else {
098:                        // The non-space char is NOT CR/LF, the spaces are valid.
099:                        ((PushbackInputStream) in).unread(c);
100:                        c = ' ';
101:                    }
102:                    return c; // return either <SPACE> or <CR/LF>
103:                } else if (c == '=') {
104:                    // QP Encoded atom. Decode the next two bytes
105:                    int a = in.read();
106:
107:                    if (a == '\n') {
108:                        /* Hmm ... not really confirming QP encoding, but lets
109:                         * allow this as a LF terminated encoded line .. and
110:                         * consider this a soft linebreak and recurse to fetch 
111:                         * the next char.
112:                         */
113:                        return read();
114:                    } else if (a == '\r') {
115:                        // Expecting LF. This forms a soft linebreak to be ignored.
116:                        int b = in.read();
117:                        if (b != '\n')
118:                            /* Not really confirming QP encoding, but
119:                             * lets allow this as well.
120:                             */
121:                            ((PushbackInputStream) in).unread(b);
122:                        return read();
123:                    } else if (a == -1) {
124:                        // Not valid QP encoding, but we be nice and tolerant here !
125:                        return -1;
126:                    } else {
127:                        ba[0] = (byte) a;
128:                        ba[1] = (byte) in.read();
129:                        try {
130:                            return ASCIIUtility.parseInt(ba, 0, 2, 16);
131:                        } catch (NumberFormatException nex) {
132:                            /*
133:                            System.err.println(
134:                             	"Illegal characters in QP encoded stream: " + 
135:                             	ASCIIUtility.toString(ba, 0, 2)
136:                            );
137:                             */
138:
139:                            ((PushbackInputStream) in).unread(ba);
140:                            return c;
141:                        }
142:                    }
143:                }
144:                return c;
145:            }
146:
147:            /**
148:             * Reads up to <code>len</code> decoded bytes of data from this input stream
149:             * into an array of bytes. This method blocks until some input is
150:             * available.
151:             * <p>
152:             *
153:             * @param      buf   the buffer into which the data is read.
154:             * @param      off   the start offset of the data.
155:             * @param      len   the maximum number of bytes read.
156:             * @return     the total number of bytes read into the buffer, or
157:             *             <code>-1</code> if there is no more data because the end of
158:             *             the stream has been reached.
159:             * @exception  IOException  if an I/O error occurs.
160:             */
161:            public int read(byte[] buf, int off, int len) throws IOException {
162:                int i, c;
163:                for (i = 0; i < len; i++) {
164:                    if ((c = read()) == -1) {
165:                        if (i == 0) // At end of stream, so we should
166:                            i = -1; // return -1 , NOT 0.
167:                        break;
168:                    }
169:                    buf[off + i] = (byte) c;
170:                }
171:                return i;
172:            }
173:
174:            /**
175:             * Tests if this input stream supports marks. Currently this class
176:             * does not support marks
177:             */
178:            public boolean markSupported() {
179:                return false;
180:            }
181:
182:            /**
183:             * Returns the number of bytes that can be read from this input
184:             * stream without blocking. The QP algorithm does not permit
185:             * a priori knowledge of the number of bytes after decoding, so
186:             * this method just invokes the <code>available</code> method
187:             * of the original input stream.
188:             */
189:            public int available() throws IOException {
190:                // This is bogus ! We don't really know how much
191:                // bytes are available *after* decoding
192:                return in.available();
193:            }
194:
195:            /**** begin TEST program
196:            public static void main(String argv[]) throws Exception {
197:                FileInputStream infile = new FileInputStream(argv[0]);
198:                QPDecoderStream decoder = new QPDecoderStream(infile);
199:                int c;
200:            
201:                while ((c = decoder.read()) != -1)
202:                    System.out.print((char)c);
203:                System.out.println();
204:            }
205:             *** end TEST program ****/
206:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.