I have a method that takes a List of URLs (of remote files) as parameter that should be downloaded. The method returns a List of other type (called Attachment), that actually holds a property of java File-type inside. For this case I used the Java Stream API to iterate over the URLs and start the download within a "map"-function that actually then returns the Attachment instance.
Now my question: Am I abusing the Java Stream API for things that its not meant for? Like putting long running tasks into it? Should I just do small operations on the input data?
The only minus I see right now is that it is a bit harder to test.
private List<Attachment> download(List<URL> attachments) {
return attachments.stream().map(attachmentUrl -> {
try {
Attachment attachment = new Attachment();
File attachmentFile = new File(getFilename(attachment.getAttachmentId(), attachmentUrl));
FileUtils.copyURLToFile(
attachmentUrl,
attachmentFile,
CONNECT_TIMEOUT,
READ_TIMEOUT);
attachment.setAttachmentFile(attachmentFile);
return attachment;
} catch (IOException e) {
e.printStackTrace();
LOGGER.error(e.getLocalizedMessage());
}
return null;
}).filter(Objects::nonNull).collect(Collectors.toList());
}