1

I'm trying to read multiple files from S3 bucket and processing using MultiResourceItemReader, I'm getting ArrayStoreException while executing the below line

InputStreamResource[] resources = resourceList.toArray(new InputStreamResource[resourceList.size()]);

please find my code below:

@Bean
public MultiResourceItemReader<String> multiResourceReader()
{
    MultiResourceItemReader<String> resourceItemReader = new MultiResourceItemReader<String>();
    String bucketName = "my-product";
    String key = "/products";

    List<InputStream> resourceList = s3Service.getFiles(bucketName, key);
    List<InputStreamResource> inputStreamResourceList = new ArrayList<>();
    for (InputStream s: resourceList) {
        inputStreamResourceList.add(new InputStreamResource(s));
    }

    InputStreamResource[] resources = resourceList.toArray(new InputStreamResource[inputStreamResourceList.size()]);

    resourceItemReader.setResources(resources);
    return resourceItemReader;
}

Exception:

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.batch.item.file.MultiResourceItemReader]: Factory method 'multiResourceItemReader' threw exception; nested exception is java.lang.ArrayStoreException: arraycopy: element type mismatch: can not cast one of the elements of java.lang.Object[] to the type of the destination array, org.springframework.core.io.InputStreamResource
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
        at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653)
        ... 64 common frames omitted
Caused by: java.lang.ArrayStoreException: arraycopy: element type mismatch: can not cast one of the elements of java.lang.Object[] to the type of the destination array, org.springframework.core.io.InputStreamResource

Can someone please help me to resolve this issue. Appreciated your help in advance. Thanks.

Brady Maf
  • 1,279
  • 2
  • 5
  • 12
  • 2
    An `InputStream` isn't an `InputStreamResource`. So your array construction like this simply won't work. You need to use a for loop (or stream) to convert the `InputStream` to an `InputStreamResource`. The code you have would only work for a `List – M. Deinum Jan 13 '22 at 06:52

0 Answers0