Skip to content

Commit

Permalink
fix: Files map is transient that needs to be restored if null (#6873) (
Browse files Browse the repository at this point in the history
…#6875)

* fix: Files map is transient that needs to be restored if null

* add test

* try remove suppression

---------

Co-authored-by: Tatu Lund <tatu@vaadin.com>
Co-authored-by: Sascha Ißbrücker <sissbruecker@vaadin.com>
  • Loading branch information
3 people authored Nov 27, 2024
1 parent 280084a commit 893fd14
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -78,7 +79,15 @@ public OutputStream receiveUpload(String fileName, String mimeType) {
* @return files stored
*/
public Set<String> getFiles() {
return files.keySet();
return getFilesMap().keySet();
}

private Map<String, FileData> getFilesMap() {
if (files == null) {
// Restore transient map if it is null
files = new HashMap<>();
}
return files;
}

/**
Expand All @@ -89,7 +98,7 @@ public Set<String> getFiles() {
* @return file data for filename or null if not found
*/
public FileData getFileData(String fileName) {
return files.get(fileName);
return getFilesMap().get(fileName);
}

/**
Expand All @@ -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,
Expand All @@ -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 + "'",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

0 comments on commit 893fd14

Please sign in to comment.