Package: text_plain$NoCloseOutputStream
text_plain$NoCloseOutputStream
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
close() |
|
|
|
|
|
||||||||||||||||||||
text_plain.NoCloseOutputStream(OutputStream) |
|
|
|
|
|
Coverage
1: /*
2: * Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Public License v. 2.0, which is available at
6: * http://www.eclipse.org/legal/epl-2.0.
7: *
8: * This Source Code may also be made available under the following Secondary
9: * Licenses when the conditions for such availability set forth in the
10: * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11: * version 2 with the GNU Classpath Exception, which is available at
12: * https://www.gnu.org/software/classpath/license.html.
13: *
14: * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15: */
16:
17: package org.eclipse.angus.mail.handlers;
18:
19: import jakarta.activation.ActivationDataFlavor;
20: import jakarta.activation.DataSource;
21: import jakarta.mail.internet.ContentType;
22: import jakarta.mail.internet.MimeUtility;
23:
24: import java.io.FilterOutputStream;
25: import java.io.IOException;
26: import java.io.InputStreamReader;
27: import java.io.OutputStream;
28: import java.io.OutputStreamWriter;
29: import java.io.UnsupportedEncodingException;
30:
31: /**
32: * DataContentHandler for text/plain.
33: *
34: */
35: public class text_plain extends handler_base {
36: private static ActivationDataFlavor[] myDF = {
37:         new ActivationDataFlavor(String.class, "text/plain", "Text String")
38: };
39:
40: /**
41: * Creates a default {@code text_plain}.
42: */
43: public text_plain() {
44: }
45:
46: /**
47: * An OuputStream wrapper that doesn't close the underlying stream.
48: */
49: private static class NoCloseOutputStream extends FilterOutputStream {
50:         NoCloseOutputStream(OutputStream os) {
51:          super(os);
52:         }
53:
54:         @Override
55:         public void close() {
56:          // do nothing
57:         }
58: }
59:
60: @Override
61: protected ActivationDataFlavor[] getDataFlavors() {
62:         return myDF;
63: }
64:
65: @Override
66: public Object getContent(DataSource ds) throws IOException {
67:         String enc = null;
68:         InputStreamReader is = null;
69:
70:         try {
71:          enc = getCharset(ds.getContentType());
72:          is = new InputStreamReader(ds.getInputStream(), enc);
73:         } catch (IllegalArgumentException iex) {
74:          /*
75:          * An unknown charset of the form ISO-XXX-XXX will cause
76:          * the JDK to throw an IllegalArgumentException. The
77:          * JDK will attempt to create a classname using this string,
78:          * but valid classnames must not contain the character '-',
79:          * and this results in an IllegalArgumentException, rather than
80:          * the expected UnsupportedEncodingException. Yikes.
81:          */
82:          throw new UnsupportedEncodingException(enc);
83:         }
84:
85:         try {
86:          int pos = 0;
87:          int count;
88:          char buf[] = new char[1024];
89:
90:          while ((count = is.read(buf, pos, buf.length - pos)) != -1) {
91:                 pos += count;
92:                 if (pos >= buf.length) {
93:                  int size = buf.length;
94:                  if (size < 256*1024)
95:                         size += size;
96:                  else
97:                         size += 256*1024;
98:                  char tbuf[] = new char[size];
99:                  System.arraycopy(buf, 0, tbuf, 0, pos);
100:                  buf = tbuf;
101:                 }
102:          }
103:          return new String(buf, 0, pos);
104:         } finally {
105:          try {
106:                 is.close();
107:          } catch (IOException ex) {
108:                 // ignore it
109:          }
110:         }
111: }
112:
113: /**
114: * Write the object to the output stream, using the specified MIME type.
115: */
116: @Override
117: public void writeTo(Object obj, String type, OutputStream os)
118:                         throws IOException {
119:         if (!(obj instanceof String))
120:          throw new IOException("\"" + getDataFlavors()[0].getMimeType() +
121:                 "\" DataContentHandler requires String object, " +
122:                 "was given object of type " + obj.getClass().toString());
123:
124:         String enc = null;
125:         OutputStreamWriter osw = null;
126:
127:         try {
128:          enc = getCharset(type);
129:          osw = new OutputStreamWriter(new NoCloseOutputStream(os), enc);
130:         } catch (IllegalArgumentException iex) {
131:          /*
132:          * An unknown charset of the form ISO-XXX-XXX will cause
133:          * the JDK to throw an IllegalArgumentException. The
134:          * JDK will attempt to create a classname using this string,
135:          * but valid classnames must not contain the character '-',
136:          * and this results in an IllegalArgumentException, rather than
137:          * the expected UnsupportedEncodingException. Yikes.
138:          */
139:          throw new UnsupportedEncodingException(enc);
140:         }
141:
142:         String s = (String)obj;
143:         osw.write(s, 0, s.length());
144:         /*
145:          * Have to call osw.close() instead of osw.flush() because
146:          * some charset converts, such as the iso-2022-jp converter,
147:          * don't output the "shift out" sequence unless they're closed.
148:          * The NoCloseOutputStream wrapper prevents the underlying
149:          * stream from being closed.
150:          */
151:         osw.close();
152: }
153:
154: private String getCharset(String type) {
155:         try {
156:          ContentType ct = new ContentType(type);
157:          String charset = ct.getParameter("charset");
158:          if (charset == null)
159:                 // If the charset parameter is absent, use US-ASCII.
160:                 charset = "us-ascii";
161:          return MimeUtility.javaCharset(charset);
162:         } catch (Exception ex) {
163:          return null;
164:         }
165: }
166: }