-
Notifications
You must be signed in to change notification settings - Fork 122
EMAIL-204: Disable eager attachment loading on MimeMessageParser #135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
HiuKwok
wants to merge
19
commits into
apache:master
Choose a base branch
from
HiuKwok:ft-hf-EMAIL-204-disable-eager-loading-for-email-attachment
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
49503b9
EMAIL-204: Disable eager loading while parsing email attachement
HiuKwok 4a34933
EMAIL-204: Remove unused methods
HiuKwok ae324f9
EMAIL-204: Update dep
HiuKwok e48d8c8
EMAIL-204: Wrapper class for data src
HiuKwok a976fff
EMAIL-204: Update count
HiuKwok 8712ae0
EMAIL-204: Update test-cases
HiuKwok 395ca7a
EMAIL-204: Update testcases
HiuKwok 44c8ae5
EMAIL-204: Java doc
HiuKwok e5e3b06
EMAIL-204: Update testcase
HiuKwok eb2673f
EMAIL-204: Remove unused class
HiuKwok 0af6160
EMAIL-204: Update import
HiuKwok 6bb26b6
EMAIL-204: Update code style
HiuKwok 286bd35
EMAIL-204: Update import
HiuKwok 23ba449
EMAIL-204: Typo
HiuKwok 5dbcc5f
EMAIL-204: Code style fix
HiuKwok 1685f07
EMAIL-204: Update testcase
HiuKwok c101e96
EMAIL-204: Update java doc
HiuKwok e5164d8
EMAIL-204: Update test import
HiuKwok 094a2b3
EMAIL-204: Update test-case
HiuKwok File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
src/main/java/org/apache/commons/mail/LazyByteArrayDataSource.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.commons.mail; | ||
|
||
import javax.activation.DataSource; | ||
import javax.mail.util.ByteArrayDataSource; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
|
||
/** | ||
* <p> This class is created to replace the usage of {@code org.apache.commons.mail.ByteArrayDataSource} and {@code javax.mail.util.ByteArrayDataSource}, | ||
* as both implementations load attachment binary in eager manner. | ||
* | ||
* <p> In order to cater the scenario that user only access the metadata (Name, Type) but not interested in the actual attachment binary, | ||
* in this scenario, the memory usage can be further reduced as attachment binary only loaded when .getInputStream() called. | ||
* | ||
* @since 1.5 | ||
*/ | ||
public class LazyByteArrayDataSource implements DataSource { | ||
|
||
/** InputStream reference for the email attachment binary. */ | ||
private final InputStream referenceInputStream; | ||
|
||
/** ByteArrayDateSource instance which contain email attachment binary in the form of byte array. */ | ||
private ByteArrayDataSource ds; | ||
|
||
/** Name of the attachment. */ | ||
private final String name; | ||
|
||
/** Type of the attachment. */ | ||
private final String type; | ||
|
||
/** | ||
* Constructs a new instance to read all necessary information for an email attachment. | ||
* | ||
* @param is the InputStream which represent the attachment binary. | ||
* @param type the type of the attachment. | ||
* @param name the name of the attachment. | ||
*/ | ||
public LazyByteArrayDataSource(InputStream is, String type, String name) { | ||
this.referenceInputStream = is; | ||
this.type = type; | ||
this.name = name; | ||
} | ||
|
||
/** | ||
* Gets an {@code ByteArrayDataSource} instance, to represent the email attachment. | ||
* | ||
* @return An ByteArrayDataSource instance which contain the email attachment. | ||
* @throws IOException resolving the email attachment failed | ||
*/ | ||
@Override | ||
public synchronized InputStream getInputStream() throws IOException { | ||
if (ds == null) { | ||
HiuKwok marked this conversation as resolved.
Show resolved
Hide resolved
|
||
//Only read attachment data to memory when getInputStream() is called. | ||
ds = new ByteArrayDataSource(referenceInputStream, type); | ||
ds.setName(name); | ||
} | ||
return ds.getInputStream(); | ||
} | ||
|
||
/** | ||
* Not supported. | ||
* | ||
* @return N/A | ||
*/ | ||
@Override | ||
public OutputStream getOutputStream() throws UnsupportedOperationException { | ||
throw new UnsupportedOperationException("cannot do this"); | ||
} | ||
|
||
/** | ||
* Gets the content type. | ||
* | ||
* @return A String. | ||
*/ | ||
@Override | ||
public String getContentType() { | ||
return type; | ||
} | ||
|
||
/** | ||
* Gets the name. | ||
* | ||
* @return A String. | ||
*/ | ||
@Override | ||
public String getName() { | ||
return name; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.