Source Code Cross Referenced for RFC1522Codec.java in  » Library » Apache-common-codec » org » apache » commons » codec » net » 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 » Library » Apache common codec » org.apache.commons.codec.net 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2001-2004 The Apache Software Foundation.
003:         * 
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         * 
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         * 
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:
017:        package org.apache.commons.codec.net;
018:
019:        import java.io.UnsupportedEncodingException;
020:
021:        import org.apache.commons.codec.DecoderException;
022:        import org.apache.commons.codec.EncoderException;
023:
024:        /**
025:         * <p>
026:         * Implements methods common to all codecs defined in RFC 1522.
027:         * </p>
028:         * 
029:         * <p>
030:         * <a href="http://www.ietf.org/rfc/rfc1522.txt">RFC 1522</a> 
031:         * describes techniques to allow the encoding of non-ASCII text in 
032:         * various portions of a RFC 822 [2] message header, in a manner which
033:         * is unlikely to confuse existing message handling software.
034:         * </p>
035:
036:         * @see <a href="http://www.ietf.org/rfc/rfc1522.txt">
037:         * MIME (Multipurpose Internet Mail Extensions) Part Two:
038:         * Message Header Extensions for Non-ASCII Text</a>
039:         * </p>
040:         * 
041:         * @author Apache Software Foundation
042:         * @since 1.3
043:         * @version $Id: RFC1522Codec.java,v 1.2 2004/04/09 22:21:43 ggregory Exp $
044:         */
045:        abstract class RFC1522Codec {
046:
047:            /**
048:             * Applies an RFC 1522 compliant encoding scheme to the given string of text with the 
049:             * given charset. This method constructs the "encoded-word" header common to all the 
050:             * RFC 1522 codecs and then invokes {@link #doEncoding(byte [])} method of a concrete 
051:             * class to perform the specific enconding.
052:             * 
053:             * @param text a string to encode
054:             * @param charset a charset to be used
055:             * 
056:             * @return RFC 1522 compliant "encoded-word"
057:             * 
058:             * @throws EncoderException thrown if there is an error conidition during the Encoding 
059:             *  process.
060:             * @throws UnsupportedEncodingException thrown if charset is not supported 
061:             * 
062:             * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
063:             *          encoding names</a>
064:             */
065:            protected String encodeText(final String text, final String charset)
066:                    throws EncoderException, UnsupportedEncodingException {
067:                if (text == null) {
068:                    return null;
069:                }
070:                StringBuffer buffer = new StringBuffer();
071:                buffer.append("=?");
072:                buffer.append(charset);
073:                buffer.append('?');
074:                buffer.append(getEncoding());
075:                buffer.append('?');
076:                byte[] rawdata = doEncoding(text.getBytes(charset));
077:                buffer.append(new String(rawdata, StringEncodings.US_ASCII));
078:                buffer.append("?=");
079:                return buffer.toString();
080:            }
081:
082:            /**
083:             * Applies an RFC 1522 compliant decoding scheme to the given string of text. This method 
084:             * processes the "encoded-word" header common to all the RFC 1522 codecs and then invokes 
085:             * {@link #doEncoding(byte [])} method of a concrete class to perform the specific deconding.
086:             * 
087:             * @param text a string to decode
088:             * 
089:             * @throws DecoderException thrown if there is an error conidition during the Decoding 
090:             *  process.
091:             * @throws UnsupportedEncodingException thrown if charset specified in the "encoded-word" 
092:             *  header is not supported 
093:             */
094:            protected String decodeText(final String text)
095:                    throws DecoderException, UnsupportedEncodingException {
096:                if (text == null) {
097:                    return null;
098:                }
099:                if ((!text.startsWith("=?")) || (!text.endsWith("?="))) {
100:                    throw new DecoderException(
101:                            "RFC 1522 violation: malformed encoded content");
102:                }
103:                int termnator = text.length() - 2;
104:                int from = 2;
105:                int to = text.indexOf("?", from);
106:                if ((to == -1) || (to == termnator)) {
107:                    throw new DecoderException(
108:                            "RFC 1522 violation: charset token not found");
109:                }
110:                String charset = text.substring(from, to);
111:                if (charset.equals("")) {
112:                    throw new DecoderException(
113:                            "RFC 1522 violation: charset not specified");
114:                }
115:                from = to + 1;
116:                to = text.indexOf("?", from);
117:                if ((to == -1) || (to == termnator)) {
118:                    throw new DecoderException(
119:                            "RFC 1522 violation: encoding token not found");
120:                }
121:                String encoding = text.substring(from, to);
122:                if (!getEncoding().equalsIgnoreCase(encoding)) {
123:                    throw new DecoderException("This codec cannot decode "
124:                            + encoding + " encoded content");
125:                }
126:                from = to + 1;
127:                to = text.indexOf("?", from);
128:                byte[] data = text.substring(from, to).getBytes(
129:                        StringEncodings.US_ASCII);
130:                data = doDecoding(data);
131:                return new String(data, charset);
132:            }
133:
134:            /**
135:             * Returns the codec name (referred to as encoding in the RFC 1522)
136:             * 
137:             * @return name of the codec
138:             */
139:            protected abstract String getEncoding();
140:
141:            /**
142:             * Encodes an array of bytes using the defined encoding scheme
143:             * 
144:             * @param bytes Data to be encoded
145:             *
146:             * @return A byte array containing the encoded data
147:             * 
148:             * @throws EncoderException thrown if the Encoder encounters a failure condition 
149:             *  during the encoding process.
150:             */
151:            protected abstract byte[] doEncoding(byte[] bytes)
152:                    throws EncoderException;
153:
154:            /**
155:             * Decodes an array of bytes using the defined encoding scheme
156:             * 
157:             * @param bytes Data to be decoded
158:             *
159:             * @return a byte array that contains decoded data
160:             * 
161:             * @throws DecoderException A decoder exception is thrown if a Decoder encounters a 
162:             *  failure condition during the decode process.
163:             */
164:            protected abstract byte[] doDecoding(byte[] bytes)
165:                    throws DecoderException;
166:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.