View Javadoc
1   /*
2    * DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
3    * Copyright (C) 2009  Mickael Guessant
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18   */
19  package davmail.exchange;
20  
21  import javax.mail.internet.MimeUtility;
22  import java.io.IOException;
23  import java.io.OutputStream;
24  import java.io.OutputStreamWriter;
25  import java.io.UnsupportedEncodingException;
26  import java.text.SimpleDateFormat;
27  import java.util.Date;
28  import java.util.Locale;
29  
30  /**
31   * Mime OutputStreamWriter to build in memory Mime message.
32   */
33  public class MimeOutputStreamWriter extends OutputStreamWriter {
34      /**
35       * Build MIME outputStreamWriter
36       *
37       * @param out outputstream
38       * @throws UnsupportedEncodingException on error
39       */
40      public MimeOutputStreamWriter(OutputStream out) throws UnsupportedEncodingException {
41          super(out, "ASCII");
42      }
43  
44      /**
45       * Write MIME header
46       *
47       * @param header header name
48       * @param value  header value
49       * @throws IOException on error
50       */
51      public void writeHeader(String header, String value) throws IOException {
52          // do not write empty headers
53          if (value != null && value.length() > 0) {
54              write(header);
55              write(": ");
56              write(MimeUtility.encodeText(value, "UTF-8", null));
57              writeLn();
58          }
59      }
60  
61      /**
62       * Write MIME header
63       *
64       * @param header header name
65       * @param value  header value
66       * @throws IOException on error
67       */
68      public void writeHeader(String header, Date value) throws IOException {
69          SimpleDateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss Z", Locale.ENGLISH);
70          writeHeader(header, formatter.format(value));
71      }
72  
73      /**
74       * Write line.
75       *
76       * @param line line content
77       * @throws IOException on error
78       */
79      public void writeLn(String line) throws IOException {
80          write(line);
81          write("\r\n");
82      }
83  
84      /**
85       * Write CRLF.
86       *
87       * @throws IOException on error
88       */
89      public void writeLn() throws IOException {
90          write("\r\n");
91      }
92  
93  }