1 /*
   2  * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * @(#)QEncoderStream.java    1.4 02/03/27
  28  */
  29 
  30 
  31 
  32 package com.sun.xml.internal.messaging.saaj.packaging.mime.util;
  33 
  34 import java.io.IOException;
  35 import java.io.OutputStream;
  36 
  37 /**
  38  * This class implements a Q Encoder as defined by RFC 2047 for
  39  * encoding MIME headers. It subclasses the QPEncoderStream class.
  40  *
  41  * @author John Mani
  42  */
  43 
  44 public class QEncoderStream extends QPEncoderStream {
  45 
  46     private String specials;
  47     private static String WORD_SPECIALS = "=_?\"#$%&'(),.:;<>@[\\]^`{|}~";
  48     private static String TEXT_SPECIALS = "=_?";
  49 
  50     /**
  51      * Create a Q encoder that encodes the specified input stream
  52      * @param out        the output stream
  53      * @param encodingWord true if we are Q-encoding a word within a
  54      *                  phrase.
  55      */
  56     public QEncoderStream(OutputStream out, boolean encodingWord) {
  57         super(out, Integer.MAX_VALUE); // MAX_VALUE is 2^31, should
  58                                        // suffice (!) to indicate that
  59                                        // CRLFs should not be inserted
  60                                        // when encoding rfc822 headers
  61 
  62         // a RFC822 "word" token has more restrictions than a
  63         // RFC822 "text" token.
  64         specials = encodingWord ? WORD_SPECIALS : TEXT_SPECIALS;
  65     }
  66 
  67     /**
  68      * Encodes the specified <code>byte</code> to this output stream.
  69      * @param      c   the <code>byte</code>.
  70      * @exception  IOException  if an I/O error occurs.
  71      */
  72     @Override
  73     public void write(int c) throws IOException {
  74         c = c & 0xff; // Turn off the MSB.
  75         if (c == ' ')
  76             output('_', false);
  77         else if (c < 040 || c >= 0177 || specials.indexOf(c) >= 0)
  78             // Encoding required.
  79             output(c, true);
  80         else // No encoding required
  81             output(c, false);
  82     }
  83 
  84     /**
  85      * Returns the length of the encoded version of this byte array.
  86      *
  87      * @param b byte array.
  88      * @param encodingWord whether use word or text specials.
  89      *
  90      * @return length.
  91      */
  92     public static int encodedLength(byte[] b, boolean encodingWord) {
  93         int len = 0;
  94         String specials = encodingWord ? WORD_SPECIALS: TEXT_SPECIALS;
  95         for (int i = 0; i < b.length; i++) {
  96             int c = b[i] & 0xff; // Mask off MSB
  97             if (c < 040 || c >= 0177 || specials.indexOf(c) >= 0)
  98                 // needs encoding
  99                 len += 3; // Q-encoding is 1 -> 3 conversion
 100             else
 101                 len++;
 102         }
 103         return len;
 104     }
 105 
 106     /**** begin TEST program ***
 107     public static void main(String argv[]) throws Exception {
 108         FileInputStream infile = new FileInputStream(argv[0]);
 109         QEncoderStream encoder = new QEncoderStream(System.out);
 110         int c;
 111 
 112         while ((c = infile.read()) != -1)
 113             encoder.write(c);
 114         encoder.close();
 115     }
 116     *** end TEST program ***/
 117 }