|
1 | 1 | /* |
2 | | - * Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 1997, 2024 Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * |
4 | 4 | * This program and the accompanying materials are made available under the |
5 | 5 | * terms of the Eclipse Public License v. 2.0, which is available at |
|
29 | 29 | import jakarta.mail.Multipart; |
30 | 30 | import jakarta.mail.Session; |
31 | 31 | import jakarta.mail.util.LineOutputStream; |
| 32 | +import jakarta.mail.util.StreamProvider; |
32 | 33 |
|
33 | 34 | import java.io.BufferedInputStream; |
34 | 35 | import java.io.ByteArrayInputStream; |
|
44 | 45 | import java.util.Enumeration; |
45 | 46 | import java.util.List; |
46 | 47 | import java.util.Properties; |
| 48 | +import java.util.ServiceConfigurationError; |
47 | 49 |
|
48 | 50 |
|
49 | 51 | /** |
@@ -243,9 +245,9 @@ public MimeMessage(MimeMessage source) throws MessagingException { |
243 | 245 | strict = source.strict; |
244 | 246 | source.writeTo(bos); |
245 | 247 | bos.close(); |
246 | | - InputStream bis = session.getStreamProvider().inputSharedByteArray(bos.toByteArray()); |
247 | | - parse(bis); |
248 | | - bis.close(); |
| 248 | + try (InputStream bis = provider().inputSharedByteArray(bos.toByteArray())) { |
| 249 | + parse(bis); |
| 250 | + } |
249 | 251 | saved = true; |
250 | 252 | } catch (IOException ex) { |
251 | 253 | // should never happen, but just in case... |
@@ -1408,7 +1410,7 @@ protected InputStream getContentStream() throws MessagingException { |
1408 | 1410 | if (contentStream != null) |
1409 | 1411 | return ((SharedInputStream) contentStream).newStream(0, -1); |
1410 | 1412 | if (content != null) { |
1411 | | - return session.getStreamProvider().inputSharedByteArray(content); |
| 1413 | + return provider().inputSharedByteArray(content); |
1412 | 1414 | } |
1413 | 1415 | throw new MessagingException("No MimeMessage content"); |
1414 | 1416 | } |
@@ -1915,7 +1917,7 @@ public void writeTo(OutputStream os, String[] ignoreList) |
1915 | 1917 | // Else, the content is untouched, so we can just output it |
1916 | 1918 | // First, write out the header |
1917 | 1919 | Enumeration<String> hdrLines = getNonMatchingHeaderLines(ignoreList); |
1918 | | - LineOutputStream los = session.getStreamProvider().outputLineStream(os, allowutf8); |
| 1920 | + LineOutputStream los = provider().outputLineStream(os, allowutf8); |
1919 | 1921 | while (hdrLines.hasMoreElements()) |
1920 | 1922 | los.writeln(hdrLines.nextElement()); |
1921 | 1923 |
|
@@ -2320,4 +2322,23 @@ protected MimeMessage createMimeMessage(Session session) |
2320 | 2322 | throws MessagingException { |
2321 | 2323 | return new MimeMessage(session); |
2322 | 2324 | } |
| 2325 | + |
| 2326 | + private StreamProvider provider() throws MessagingException { |
| 2327 | + try { |
| 2328 | + try { |
| 2329 | + final Session s = this.session; |
| 2330 | + if (s != null) { |
| 2331 | + return s.getStreamProvider(); |
| 2332 | + } else { |
| 2333 | + return Session.getDefaultInstance(System.getProperties(), |
| 2334 | + null).getStreamProvider(); |
| 2335 | + } |
| 2336 | + } catch (ServiceConfigurationError sce) { |
| 2337 | + throw new IllegalStateException(sce); |
| 2338 | + } |
| 2339 | + } catch (RuntimeException re) { |
| 2340 | + throw new MessagingException("Unable to get " |
| 2341 | + + StreamProvider.class.getName(), re); |
| 2342 | + } |
| 2343 | + } |
2323 | 2344 | } |
0 commit comments