I am trying to parse an EML using java mail API using mimeMessage
Something like this:
InputStream stream = new FileInputStream("/my.eml");
MimeMessage message = new MimeMessage(Session.getDefaultInstance(System.getProperties()),stream);
It takes too long to read that EML if it's more than 10MBs(around 10-25 seconds)
The code is working fine so no worry about the code. I am looking something that can increase the parsing speed.
Thanks in advance.
Wrap your FileInputStream inside a BufferedInputStream. This way you avoid going through the storage stack of your operating system for every single byte but instead read a bunch of them in a single pass to memory.
Or for the purpose of measuring the parsing performance seperate from reading the bytes from the file try loading it in advance in memory:
public static void main( String[] args ) throws Exception {
File file = new File("C:\\Users\\Public\\Documents\\bigemail.eml");
byte[] data = new byte[Math.toIntExact(file.length())];
FileInputStream fis = new FileInputStream(file);
int bytesRead = fis.read(data);
System.out.println("Read " + bytesRead + " bytes");
fis.close();
for(int i=0; i<5; i++) {
parseMail(new ByteArrayInputStream(data));
}
}
private static void parseMail(InputStream memoryStream) throws Exception {
var start = System.nanoTime();
MimeMessage message = new MimeMessage(Session.getDefaultInstance(System.getProperties()),memoryStream);
var stop = System.nanoTime();
System.out.printf("Parsing? took %f s\n", (stop-start)/1_000_000_000.0);
System.out.println("Subject: " + message.getSubject());
}
On my laptop the output shows:
Read 12610414 bytes
Parsing? Took 0,261948 s
Subject: FW: .........
Parsing? Took 0,024068 s
Subject: FW: .........
Parsing? Took 0,020336 s
Subject: FW: .........
Parsing? Took 0,010707 s
Subject: FW: .........
Parsing? Took 0,010973 s
Subject: FW: .........
So the first time seems to be quite a bit slower, but for 12 MB still a far cry from what you are reporting.
This is using Oracle Java 10 on a system with 16 GB of memory and using this maven dependency:
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
Try using a SharedFileInputStream .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.