From 31116f092dd78b2df81de13c48a3aa9bdb2f3a43 Mon Sep 17 00:00:00 2001 From: Julian Raphael Jautz Date: Mon, 27 Nov 2023 08:28:28 +0100 Subject: [PATCH 1/2] PAINTROID_693_auto_load_file_not_found removed bug where path to temp file was set incorrectly - streamlined saving and loading of temp file - closed streams - avoid kryo buffer underflow when app is started and no temp file available --- .../java/org/catrobat/paintroid/FileIO.kt | 32 ++++++------------- 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/Paintroid/src/main/java/org/catrobat/paintroid/FileIO.kt b/Paintroid/src/main/java/org/catrobat/paintroid/FileIO.kt index 864f069b0c..ccc15662c8 100644 --- a/Paintroid/src/main/java/org/catrobat/paintroid/FileIO.kt +++ b/Paintroid/src/main/java/org/catrobat/paintroid/FileIO.kt @@ -596,18 +596,18 @@ object FileIO { val stream = FileOutputStream("$tempPath/$newFileName") commandSerializer.writeToInternalMemory(stream) - temporaryFilePath = TEMP_IMAGE_TEMP_PATH + stream.close() } catch (e: IOException) { Log.e("Cannot write", "Can't write to stream", e) } val oldFile = File(internalMemoryPath, TEMP_IMAGE_PATH) - if (oldFile.exists()) { + val newFile = File(internalMemoryPath, TEMP_IMAGE_TEMP_PATH) + if (oldFile.exists() && newFile.exists()) { oldFile.delete() } - val newFile = File(internalMemoryPath, TEMP_IMAGE_TEMP_PATH) if (newFile.exists()) { - newFile.renameTo(File(internalMemoryPath, TEMP_IMAGE_PATH)) - temporaryFilePath = TEMP_IMAGE_PATH + val rename = File(internalMemoryPath, TEMP_IMAGE_PATH) + newFile.renameTo(rename) } } @@ -616,34 +616,22 @@ object FileIO { if (!tempPath.exists()) { return false } - val fileList = tempPath.listFiles() - if (fileList != null && fileList.isNotEmpty()) { - if (fileList.size == 2) { - if (fileList[1].lastModified() > fileList[0].lastModified()) { - reorganizeTempFiles(fileList[1], fileList[0], internalMemoryPath) - } else { - reorganizeTempFiles(fileList[0], fileList[1], internalMemoryPath) - } - } else { - temporaryFilePath = fileList[0].path - } + val file = File(internalMemoryPath, TEMP_IMAGE_PATH) + if (file.exists()){ + temporaryFilePath = file.path return true } return false } - private fun reorganizeTempFiles(file1: File, file2: File, internalMemoryPath: File) { - file2.delete() - file1.renameTo(File(internalMemoryPath, TEMP_IMAGE_PATH)) - temporaryFilePath = TEMP_IMAGE_PATH - } - fun openTemporaryPictureFile(commandSerializer: CommandSerializer): WorkspaceReturnValue? { var workspaceReturnValue: WorkspaceReturnValue? = null if (temporaryFilePath != null) { try { val stream = FileInputStream(temporaryFilePath) + if (stream.available() == 0) throw IOException("FileInputStream not available!") workspaceReturnValue = commandSerializer.readFromInternalMemory(stream) + stream.close() } catch (e: IOException) { Log.e("Cannot read", "Can't read from stream", e) } From 16c459f47da5cce9132d76401c9b0ca775485b3f Mon Sep 17 00:00:00 2001 From: Julian Raphael Jautz Date: Mon, 27 Nov 2023 08:50:50 +0100 Subject: [PATCH 2/2] PAINTROID_693_auto_load_file_not_found removed bug where path to temp file was set incorrectly - streamlined saving and loading of temp file - closed streams - avoid kryo buffer underflow when app is started and no temp file available --- Paintroid/src/main/java/org/catrobat/paintroid/FileIO.kt | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Paintroid/src/main/java/org/catrobat/paintroid/FileIO.kt b/Paintroid/src/main/java/org/catrobat/paintroid/FileIO.kt index ccc15662c8..b9a605282f 100644 --- a/Paintroid/src/main/java/org/catrobat/paintroid/FileIO.kt +++ b/Paintroid/src/main/java/org/catrobat/paintroid/FileIO.kt @@ -606,8 +606,7 @@ object FileIO { oldFile.delete() } if (newFile.exists()) { - val rename = File(internalMemoryPath, TEMP_IMAGE_PATH) - newFile.renameTo(rename) + newFile.renameTo(File(internalMemoryPath, TEMP_IMAGE_PATH)) } } @@ -617,7 +616,7 @@ object FileIO { return false } val file = File(internalMemoryPath, TEMP_IMAGE_PATH) - if (file.exists()){ + if (file.exists()) { temporaryFilePath = file.path return true }