diff --git a/vaadin-upload-flow-parent/vaadin-upload-flow/src/main/java/com/vaadin/flow/component/upload/receivers/MultiFileBuffer.java b/vaadin-upload-flow-parent/vaadin-upload-flow/src/main/java/com/vaadin/flow/component/upload/receivers/MultiFileBuffer.java index 3427facbb55..6aa06feb947 100644 --- a/vaadin-upload-flow-parent/vaadin-upload-flow/src/main/java/com/vaadin/flow/component/upload/receivers/MultiFileBuffer.java +++ b/vaadin-upload-flow-parent/vaadin-upload-flow/src/main/java/com/vaadin/flow/component/upload/receivers/MultiFileBuffer.java @@ -67,7 +67,8 @@ public MultiFileBuffer(FileFactory factory) { @Override public OutputStream receiveUpload(String fileName, String mimeType) { FileOutputStream outputBuffer = createFileOutputStream(fileName); - files.put(fileName, new FileData(fileName, mimeType, outputBuffer)); + getFilesMap().put(fileName, + new FileData(fileName, mimeType, outputBuffer)); return outputBuffer; } @@ -78,7 +79,15 @@ public OutputStream receiveUpload(String fileName, String mimeType) { * @return files stored */ public Set getFiles() { - return files.keySet(); + return getFilesMap().keySet(); + } + + private Map getFilesMap() { + if (files == null) { + // Restore transient map if it is null + files = new HashMap<>(); + } + return files; } /** @@ -89,7 +98,7 @@ public Set getFiles() { * @return file data for filename or null if not found */ public FileData getFileData(String fileName) { - return files.get(fileName); + return getFilesMap().get(fileName); } /** @@ -100,9 +109,9 @@ public FileData getFileData(String fileName) { * @return file output stream or null if not available */ public FileDescriptor getFileDescriptor(String fileName) { - if (files.containsKey(fileName)) { + if (getFilesMap().containsKey(fileName)) { try { - return ((FileOutputStream) files.get(fileName) + return ((FileOutputStream) getFilesMap().get(fileName) .getOutputBuffer()).getFD(); } catch (IOException e) { getLogger().log(Level.WARNING, @@ -121,9 +130,10 @@ public FileDescriptor getFileDescriptor(String fileName) { * @return input stream for file or empty stream if file not found */ public InputStream getInputStream(String fileName) { - if (files.containsKey(fileName)) { + if (getFilesMap().containsKey(fileName)) { try { - return new FileInputStream(files.get(fileName).getFile()); + return new FileInputStream( + getFilesMap().get(fileName).getFile()); } catch (IOException e) { getLogger().log(Level.WARNING, "Failed to create InputStream for: '" + fileName + "'", diff --git a/vaadin-upload-flow-parent/vaadin-upload-flow/src/test/java/com/vaadin/flow/component/upload/tests/UploadSerializableTest.java b/vaadin-upload-flow-parent/vaadin-upload-flow/src/test/java/com/vaadin/flow/component/upload/tests/UploadSerializableTest.java index 4dee62241a2..5380260ab98 100644 --- a/vaadin-upload-flow-parent/vaadin-upload-flow/src/test/java/com/vaadin/flow/component/upload/tests/UploadSerializableTest.java +++ b/vaadin-upload-flow-parent/vaadin-upload-flow/src/test/java/com/vaadin/flow/component/upload/tests/UploadSerializableTest.java @@ -63,4 +63,17 @@ public void serializeMultiFileBuffer() throws Throwable { serializeAndDeserialize(multiFileBuffer); } + + @Test + public void serializeMultiFileBuffer_restoreFileMap() { + MultiFileBuffer multiFileBuffer = new MultiFileBuffer(); + try { + multiFileBuffer = serializeAndDeserialize(multiFileBuffer); + } catch (Throwable e) { + throw new RuntimeException(e); + } + + // Verifies that internal file map is restored, would throw otherwise + multiFileBuffer.receiveUpload("bar.txt", "text/plain"); + } }