I have a small snippet which reads the attachments from an IMAP inbox:
BodyPart bodyPart = content.getBodyPart(i);
if(bodyPart.getFileName() != null){
logger.info("Retrieving file {} with size: {} MB", bodyPart.getFileName(), bodyPart.getSize()/1024D/1024);
Object innerContent = bodyPart.getContent();
switch(innerContent.getClass().getCanonicalName()){
case "com.sun.mail.util.BASE64DecoderStream":
BASE64DecoderStream innerContentStream = (BASE64DecoderStream) innerContent;
final byte[] actualContent = innerContentStream.readAllBytes();
//final byte[] actualContent = IOUtils.toByteArray(innerContentStream);
logger.info("Downloaded {} bytes", actualContent.length);
files.put(bodyPart.getFileName(), actualContent);
break;
default:
logger.error("Unimplemented content type: {}",innerContent.getClass().getCanonicalName());
}
With InputStream.readAllBytes() the filesize is always 8192 bytes (one buffer), whereas Apache's IOUtils.toByteArray(InputStream) reads the files correctly.
I actually wonder why this is the case. Did anyone run into this issue as well?
Java versions:
openjdk version "11.0.5" 2019-10-15
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.5+10)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.5+10, mixed mode)