Skip to content

Package: text_plain

text_plain

nameinstructionbranchcomplexitylinemethod
getCharset(String)
M: 19 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
getContent(DataSource)
M: 82 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 22 C: 0
0%
M: 1 C: 0
0%
getDataFlavors()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 13 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
text_plain()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
writeTo(Object, String, OutputStream)
M: 61 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 14 C: 0
0%
M: 1 C: 0
0%

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: public class text_plain extends handler_base {
35: private static ActivationDataFlavor[] myDF = {
36: new ActivationDataFlavor(String.class, "text/plain", "Text String")
37: };
38:
39: /**
40: * Creates a default {@code text_plain}.
41: */
42: public text_plain() {
43: }
44:
45: /**
46: * An OuputStream wrapper that doesn't close the underlying stream.
47: */
48: private static class NoCloseOutputStream extends FilterOutputStream {
49: NoCloseOutputStream(OutputStream os) {
50: super(os);
51: }
52:
53: @Override
54: public void close() {
55: // do nothing
56: }
57: }
58:
59: @Override
60: protected ActivationDataFlavor[] getDataFlavors() {
61: return myDF;
62: }
63:
64: @Override
65: public Object getContent(DataSource ds) throws IOException {
66: String enc = null;
67: InputStreamReader is = null;
68:
69: try {
70: enc = getCharset(ds.getContentType());
71: is = new InputStreamReader(ds.getInputStream(), enc);
72: } catch (IllegalArgumentException iex) {
73: /*
74: * An unknown charset of the form ISO-XXX-XXX will cause
75: * the JDK to throw an IllegalArgumentException. The
76: * JDK will attempt to create a classname using this string,
77: * but valid classnames must not contain the character '-',
78: * and this results in an IllegalArgumentException, rather than
79: * the expected UnsupportedEncodingException. Yikes.
80: */
81: throw new UnsupportedEncodingException(enc);
82: }
83:
84: try {
85: int pos = 0;
86: int count;
87: char[] buf = new char[1024];
88:
89:• while ((count = is.read(buf, pos, buf.length - pos)) != -1) {
90: pos += count;
91:• if (pos >= buf.length) {
92: int size = buf.length;
93: size += Math.min(size, 256 * 1024);
94: char[] tbuf = new char[size];
95: System.arraycopy(buf, 0, tbuf, 0, pos);
96: buf = tbuf;
97: }
98: }
99: return new String(buf, 0, pos);
100: } finally {
101: try {
102: is.close();
103: } catch (IOException ex) {
104: // ignore it
105: }
106: }
107: }
108:
109: /**
110: * Write the object to the output stream, using the specified MIME type.
111: */
112: @Override
113: public void writeTo(Object obj, String type, OutputStream os)
114: throws IOException {
115:• if (!(obj instanceof String))
116: throw new IOException("\"" + getDataFlavors()[0].getMimeType() +
117: "\" DataContentHandler requires String object, " +
118: "was given object of type " + obj.getClass().toString());
119:
120: String enc = null;
121: OutputStreamWriter osw = null;
122:
123: try {
124: enc = getCharset(type);
125: osw = new OutputStreamWriter(new NoCloseOutputStream(os), enc);
126: } catch (IllegalArgumentException iex) {
127: /*
128: * An unknown charset of the form ISO-XXX-XXX will cause
129: * the JDK to throw an IllegalArgumentException. The
130: * JDK will attempt to create a classname using this string,
131: * but valid classnames must not contain the character '-',
132: * and this results in an IllegalArgumentException, rather than
133: * the expected UnsupportedEncodingException. Yikes.
134: */
135: throw new UnsupportedEncodingException(enc);
136: }
137:
138: String s = (String) obj;
139: osw.write(s, 0, s.length());
140: /*
141: * Have to call osw.close() instead of osw.flush() because
142: * some charset converts, such as the iso-2022-jp converter,
143: * don't output the "shift out" sequence unless they're closed.
144: * The NoCloseOutputStream wrapper prevents the underlying
145: * stream from being closed.
146: */
147: osw.close();
148: }
149:
150: private String getCharset(String type) {
151: try {
152: ContentType ct = new ContentType(type);
153: String charset = ct.getParameter("charset");
154:• if (charset == null)
155: // If the charset parameter is absent, use US-ASCII.
156: charset = "us-ascii";
157: return MimeUtility.javaCharset(charset);
158: } catch (Exception ex) {
159: return null;
160: }
161: }
162: }