Skip to content

Commit ef7483e

Browse files
jbescoslukasj
authored andcommitted
Revert "Multipart performs blocking call in every instantiation #699 (#716)"
This reverts commit 892fae4.
1 parent abe990f commit ef7483e

File tree

10 files changed

+44
-273
lines changed

10 files changed

+44
-273
lines changed

api/src/main/java/jakarta/mail/BodyPart.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ public abstract class BodyPart implements Part {
3939
*/
4040
protected Multipart parent;
4141

42+
/**
43+
* Instance of stream provider.
44+
*
45+
* @since JavaMail 2.1
46+
*/
47+
protected final StreamProvider streamProvider = StreamProvider.provider();
48+
4249
/**
4350
* Creates a default {@code BodyPart}.
4451
*/
@@ -67,14 +74,4 @@ public Multipart getParent() {
6774
void setParent(Multipart parent) {
6875
this.parent = parent;
6976
}
70-
71-
@Override
72-
public StreamProvider getStreamProvider() throws MessagingException {
73-
if (parent != null) {
74-
return parent.getStreamProvider();
75-
} else {
76-
return Part.super.getStreamProvider();
77-
}
78-
}
79-
8077
}

api/src/main/java/jakarta/mail/Message.java

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@
1717
package jakarta.mail;
1818

1919
import jakarta.mail.search.SearchTerm;
20-
import jakarta.mail.util.StreamProvider;
2120

2221
import java.io.InvalidObjectException;
2322
import java.io.ObjectStreamException;
2423
import java.io.Serializable;
2524
import java.util.Date;
26-
import java.util.ServiceConfigurationError;
2725

2826
/**
2927
* This class models an email message. This is an abstract class.
@@ -707,22 +705,4 @@ protected void setExpunged(boolean expunged) {
707705
public boolean match(SearchTerm term) throws MessagingException {
708706
return term.match(this);
709707
}
710-
711-
@Override
712-
public StreamProvider getStreamProvider() throws MessagingException {
713-
try {
714-
try {
715-
final Session s = this.session;
716-
if (s != null) {
717-
return s.getStreamProvider();
718-
} else {
719-
return Session.getDefaultInstance(System.getProperties(), null).getStreamProvider();
720-
}
721-
} catch (ServiceConfigurationError sce) {
722-
throw new IllegalStateException(sce);
723-
}
724-
} catch (RuntimeException re) {
725-
throw new NoSuchProviderException("Unable to get " + StreamProvider.class.getName(), re);
726-
}
727-
}
728708
}

api/src/main/java/jakarta/mail/Multipart.java

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import java.io.IOException;
2222
import java.io.OutputStream;
23-
import java.util.ServiceConfigurationError;
2423
import java.util.Vector;
2524

2625
/**
@@ -62,6 +61,13 @@ public abstract class Multipart {
6261
*/
6362
protected Part parent;
6463

64+
/**
65+
* Instance of stream provider.
66+
*
67+
* @since JavaMail 2.1
68+
*/
69+
protected final StreamProvider streamProvider = StreamProvider.provider();
70+
6571
/**
6672
* Default constructor. An empty Multipart object is created.
6773
*/
@@ -260,32 +266,4 @@ public synchronized Part getParent() {
260266
public synchronized void setParent(Part parent) {
261267
this.parent = parent;
262268
}
263-
264-
/**
265-
* Obtains the {@link StreamProvider} from the parent, if possible.
266-
* Otherwise it obtains it from
267-
* {@link Session#getDefaultInstance(java.util.Properties, Authenticator)}.
268-
*
269-
* @return the StreamProvider implementation from the session.
270-
* @throws MessagingException if errors.
271-
*
272-
* @since JavaMail 2.2
273-
*/
274-
protected StreamProvider getStreamProvider() throws MessagingException {
275-
Part parent = this.parent;
276-
if (parent != null) {
277-
return parent.getStreamProvider();
278-
} else {
279-
try {
280-
try {
281-
return Session.getDefaultInstance(System.getProperties(), null).getStreamProvider();
282-
} catch (ServiceConfigurationError sce) {
283-
throw new IllegalStateException(sce);
284-
}
285-
} catch (RuntimeException re) {
286-
throw new NoSuchProviderException("Unable to get " + StreamProvider.class.getName(), re);
287-
}
288-
}
289-
}
290-
291269
}

api/src/main/java/jakarta/mail/Part.java

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@
1717
package jakarta.mail;
1818

1919
import jakarta.activation.DataHandler;
20-
import jakarta.mail.util.StreamProvider;
2120

2221
import java.io.IOException;
2322
import java.io.InputStream;
2423
import java.io.OutputStream;
2524
import java.util.Enumeration;
26-
import java.util.ServiceConfigurationError;
2725

2826
/**
2927
* The <code>Part</code> interface is the common base interface for
@@ -455,25 +453,4 @@ Enumeration<Header> getMatchingHeaders(String[] header_names)
455453
*/
456454
Enumeration<Header> getNonMatchingHeaders(String[] header_names)
457455
throws MessagingException;
458-
459-
/**
460-
* Obtains the {@link StreamProvider}.
461-
* It defaults to {@link Session#getDefaultInstance(java.util.Properties, Authenticator)}.
462-
*
463-
* @return the StreamProvider.
464-
* @throws MessagingException if errors.
465-
*
466-
* @since JavaMail 2.2
467-
*/
468-
default StreamProvider getStreamProvider() throws MessagingException {
469-
try {
470-
try {
471-
return Session.getDefaultInstance(System.getProperties(), null).getStreamProvider();
472-
} catch (ServiceConfigurationError sce) {
473-
throw new IllegalStateException(sce);
474-
}
475-
} catch (RuntimeException re) {
476-
throw new NoSuchProviderException("Unable to get " + StreamProvider.class.getName(), re);
477-
}
478-
}
479456
}

api/src/main/java/jakarta/mail/internet/MimeBodyPart.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import jakarta.mail.Multipart;
3131
import jakarta.mail.Part;
3232
import jakarta.mail.util.LineOutputStream;
33+
import jakarta.mail.util.StreamProvider;
3334
import jakarta.mail.util.StreamProvider.EncoderTypes;
3435

3536
import java.io.BufferedInputStream;
@@ -1640,7 +1641,7 @@ static void writeTo(MimePart part, OutputStream os, String[] ignoreList)
16401641
} else {
16411642
Map<String, Object> params = new HashMap<>();
16421643
params.put("allowutf8", allowutf8);
1643-
los = part.getStreamProvider().outputLineStream(os, allowutf8);
1644+
los = StreamProvider.provider().outputLineStream(os, allowutf8);
16441645
}
16451646

16461647
// First, write out the header

api/src/main/java/jakarta/mail/internet/MimeMessage.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import jakarta.mail.Multipart;
3030
import jakarta.mail.Session;
3131
import jakarta.mail.util.LineOutputStream;
32+
import jakarta.mail.util.StreamProvider;
3233

3334
import java.io.BufferedInputStream;
3435
import java.io.ByteArrayInputStream;
@@ -44,6 +45,8 @@
4445
import java.util.Enumeration;
4546
import java.util.List;
4647
import java.util.Properties;
48+
import java.util.ServiceConfigurationError;
49+
4750

4851
/**
4952
* This class represents a MIME style email message. It implements
@@ -243,7 +246,7 @@ public MimeMessage(MimeMessage source) throws MessagingException {
243246
allowutf8 = source.allowutf8;
244247
source.writeTo(bos);
245248
bos.close();
246-
try (InputStream bis = getStreamProvider().inputSharedByteArray(bos.toByteArray())) {
249+
try (InputStream bis = provider().inputSharedByteArray(bos.toByteArray())) {
247250
parse(bis);
248251
}
249252
saved = true;
@@ -1406,7 +1409,7 @@ protected InputStream getContentStream() throws MessagingException {
14061409
if (contentStream != null)
14071410
return ((SharedInputStream) contentStream).newStream(0, -1);
14081411
if (content != null) {
1409-
return getStreamProvider().inputSharedByteArray(content);
1412+
return provider().inputSharedByteArray(content);
14101413
}
14111414
throw new MessagingException("No MimeMessage content");
14121415
}
@@ -1913,7 +1916,7 @@ public void writeTo(OutputStream os, String[] ignoreList)
19131916
// Else, the content is untouched, so we can just output it
19141917
// First, write out the header
19151918
Enumeration<String> hdrLines = getNonMatchingHeaderLines(ignoreList);
1916-
LineOutputStream los = getStreamProvider().outputLineStream(os, allowutf8);
1919+
LineOutputStream los = provider().outputLineStream(os, allowutf8);
19171920
while (hdrLines.hasMoreElements())
19181921
los.writeln(hdrLines.nextElement());
19191922

@@ -2319,6 +2322,25 @@ protected MimeMessage createMimeMessage(Session session)
23192322
return new MimeMessage(session);
23202323
}
23212324

2325+
private StreamProvider provider() throws MessagingException {
2326+
try {
2327+
try {
2328+
final Session s = this.session;
2329+
if (s != null) {
2330+
return s.getStreamProvider();
2331+
} else {
2332+
return Session.getDefaultInstance(System.getProperties(),
2333+
null).getStreamProvider();
2334+
}
2335+
} catch (ServiceConfigurationError sce) {
2336+
throw new IllegalStateException(sce);
2337+
}
2338+
} catch (RuntimeException re) {
2339+
throw new MessagingException("Unable to get "
2340+
+ StreamProvider.class.getName(), re);
2341+
}
2342+
}
2343+
23222344
boolean isStrict() {
23232345
return strict;
23242346
}

api/src/main/java/jakarta/mail/internet/MimeMultipart.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ public synchronized void writeTo(OutputStream os)
520520

521521
String boundary = "--" +
522522
(new ContentType(contentType)).getParameter("boundary");
523-
LineOutputStream los = getStreamProvider().outputLineStream(os, false);
523+
LineOutputStream los = streamProvider.outputLineStream(os, false);
524524
// if there's a preamble, write it out
525525
if (preamble != null) {
526526
byte[] pb = MimeUtility.getBytes(preamble);
@@ -601,7 +601,7 @@ protected synchronized void parse() throws MessagingException {
601601

602602
try {
603603
// Skip and save the preamble
604-
LineInputStream lin = getStreamProvider().inputLineStream(in, false);
604+
LineInputStream lin = streamProvider.inputLineStream(in, false);
605605
StringBuilder preamblesb = null;
606606
String line;
607607
while ((line = lin.readLine()) != null) {

api/src/main/java/jakarta/mail/internet/PreencodedMimeBodyPart.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public void writeTo(OutputStream os)
7878
if (os instanceof LineOutputStream) {
7979
los = (LineOutputStream) os;
8080
} else {
81-
los = getStreamProvider().outputLineStream(os, false);
81+
los = streamProvider.outputLineStream(os, false);
8282
}
8383

8484
// First, write out the header

0 commit comments

Comments
 (0)