Код: Выделить всё
java.io.IOException: Stream Closed
at java.base/java.io.FileInputStream.readBytes(Native Method)
at java.base/java.io.FileInputStream.read(FileInputStream.java:279)
at com.amazonaws.internal.SdkFilterInputStream.read(SdkFilterInputStream.java:90)
at java.base/java.io.FilterInputStream.read(FilterInputStream.java:107)
at
Код: Выделить всё
@Test
public void minimalExampleTest() throws IOException {
when(s3Service.getObject(Mockito.anyString())).thenReturn(getS3Object("image.jpg"));
try(FileOutputStream fos = new FileOutputStream( "src/test/resources/exported_data_test.zip");
BufferedOutputStream bos = new BufferedOutputStream(fos);
ZipOutputStream zos = new ZipOutputStream(bos)){
service.minimalExample(zos, "directory/s3");
}
ZipFile zipFile = new ZipFile( "src/test/resources/exported_data_test.zip");
List entries = zipFile.stream().map(ZipEntry::getName).collect(Collectors.toList());
//assert
assertNotNull(entries);
assertTrue(entries.stream().filter(ent -> ent.contains(".jpg")).collect(Collectors.toList()).size() == 4);
List imagesNameList = List.of("doc1_export.jpg", "doc2_export.jpg", "doc3_export.jpg", "doc4_export.jpg");
entries.stream().forEach(imageName -> assertTrue(imagesNameList.contains(imageName)));
//generated files deletion
entries.stream().forEach(imageName -> {
File file = new File("src/test/resources" + "/" + imageName);
file.delete();
});
File file = new File( "src/test/resources/exported_data_test.zip");
file.delete();
}
private static S3Object getS3Object(String fileName) throws FileNotFoundException {
S3Object s3Object = new S3Object();
s3Object.setKey(fileName);
File initialFile = new File(IMAGES_DOSSIER + "/" + fileName);
InputStream targetStream = new FileInputStream(initialFile);
s3Object.setObjectContent(targetStream);
return s3Object;
}
Код: Выделить всё
public ZipOutputStream minimalExample (ZipOutputStream zipOutputStream, String directoryName) {
try {
List listDocumentType = List.of("doc1", "doc2", "doc3", "doc4");
Map s3ObjectInputStreamList = new HashMap();
listDocumentType.stream().forEach(docType -> {
String fileName = directoryName + '/' + docType + "_export.jpg";
S3Object s3Object = s3Service.getObject(fileName);
if (s3Object != null)
s3ObjectInputStreamList.put(docType + "_export.jpg", s3Object.getObjectContent());
});
s3ObjectInputStreamList.keySet().forEach(key -> {
var zipEntry = new ZipEntry(key);
try {
zipOutputStream.putNextEntry(zipEntry);
var objectContent = s3ObjectInputStreamList.get(key);
var bytes = new byte[1024];
int length;
while ((length = objectContent.read(bytes)) >= 0) {
zipOutputStream.write(bytes, 0, length);
}
objectContent.close();
zipEntry.clone();
} catch (IOException e) {
e.printStackTrace();
}
});
zipOutputStream.closeEntry();
zipOutputStream.close();
} catch (IOException e) {
logger.log(Level.LOW, e.getMessage());
}
return zipOutputStream;
}
Я не знаю, почему у меня возникает ошибка java.io.IOException: Stream Closed. Может ли кто-нибудь дать объяснение/решение этой проблемы?
Подробнее здесь: https://stackoverflow.com/questions/787 ... in-my-test