001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://jwsdp.dev.java.net/CDDLv1.0.html
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * HEADER in each file and include the License file at
014: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
015: * add the following below this CDDL HEADER, with the
016: * fields enclosed by brackets "[]" replaced with your
017: * own identifying information: Portions Copyright [yyyy]
018: * [name of copyright owner]
019: */
020: /*
021: * @(#)QEncoderStream.java 1.4 02/03/27
022: */
023:
024: /*
025: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
026: *
027: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
028: *
029: * The contents of this file are subject to the terms of either the GNU
030: * General Public License Version 2 only ("GPL") or the Common Development
031: * and Distribution License("CDDL") (collectively, the "License"). You
032: * may not use this file except in compliance with the License. You can obtain
033: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
034: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
035: * language governing permissions and limitations under the License.
036: *
037: * When distributing the software, include this License Header Notice in each
038: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
039: * Sun designates this particular file as subject to the "Classpath" exception
040: * as provided by Sun in the GPL Version 2 section of the License file that
041: * accompanied this code. If applicable, add the following below the License
042: * Header, with the fields enclosed by brackets [] replaced by your own
043: * identifying information: "Portions Copyrighted [year]
044: * [name of copyright owner]"
045: *
046: * Contributor(s):
047: *
048: * If you wish your version of this file to be governed by only the CDDL or
049: * only the GPL Version 2, indicate your decision by adding "[Contributor]
050: * elects to include this software in this distribution under the [CDDL or GPL
051: * Version 2] license." If you don't indicate a single choice of license, a
052: * recipient has the option to distribute your version of this file under
053: * either the CDDL, the GPL Version 2 or to extend the choice of license to
054: * its licensees as provided above. However, if you add GPL Version 2 code
055: * and therefore, elected the GPL Version 2 license, then the option applies
056: * only if the new code is made subject to such option by the copyright
057: * holder.
058: */
059:
060: package com.sun.xml.messaging.saaj.packaging.mime.util;
061:
062: import java.io.IOException;
063: import java.io.OutputStream;
064:
065: /**
066: * This class implements a Q Encoder as defined by RFC 2047 for
067: * encoding MIME headers. It subclasses the QPEncoderStream class.
068: *
069: * @author John Mani
070: */
071:
072: public class QEncoderStream extends QPEncoderStream {
073:
074: private String specials;
075: private static String WORD_SPECIALS = "=_?\"#$%&'(),.:;<>@[\\]^`{|}~";
076: private static String TEXT_SPECIALS = "=_?";
077:
078: /**
079: * Create a Q encoder that encodes the specified input stream
080: * @param out the output stream
081: * @param encodingWord true if we are Q-encoding a word within a
082: * phrase.
083: */
084: public QEncoderStream(OutputStream out, boolean encodingWord) {
085: super (out, Integer.MAX_VALUE); // MAX_VALUE is 2^31, should
086: // suffice (!) to indicate that
087: // CRLFs should not be inserted
088: // when encoding rfc822 headers
089:
090: // a RFC822 "word" token has more restrictions than a
091: // RFC822 "text" token.
092: specials = encodingWord ? WORD_SPECIALS : TEXT_SPECIALS;
093: }
094:
095: /**
096: * Encodes the specified <code>byte</code> to this output stream.
097: * @param c the <code>byte</code>.
098: * @exception IOException if an I/O error occurs.
099: */
100: public void write(int c) throws IOException {
101: c = c & 0xff; // Turn off the MSB.
102: if (c == ' ')
103: output('_', false);
104: else if (c < 040 || c >= 0177 || specials.indexOf(c) >= 0)
105: // Encoding required.
106: output(c, true);
107: else
108: // No encoding required
109: output(c, false);
110: }
111:
112: /**
113: * Returns the length of the encoded version of this byte array.
114: */
115: public static int encodedLength(byte[] b, boolean encodingWord) {
116: int len = 0;
117: String specials = encodingWord ? WORD_SPECIALS : TEXT_SPECIALS;
118: for (int i = 0; i < b.length; i++) {
119: int c = b[i] & 0xff; // Mask off MSB
120: if (c < 040 || c >= 0177 || specials.indexOf(c) >= 0)
121: // needs encoding
122: len += 3; // Q-encoding is 1 -> 3 conversion
123: else
124: len++;
125: }
126: return len;
127: }
128:
129: /**** begin TEST program ***
130: public static void main(String argv[]) throws Exception {
131: FileInputStream infile = new FileInputStream(argv[0]);
132: QEncoderStream encoder = new QEncoderStream(System.out);
133: int c;
134:
135: while ((c = infile.read()) != -1)
136: encoder.write(c);
137: encoder.close();
138: }
139: *** end TEST program ***/
140: }
|