From 0abc4ad1290913e755dd70ff41c878d590e44307 Mon Sep 17 00:00:00 2001 From: Fredia Huya-Kouadio Date: Mon, 4 Jul 2022 12:56:53 -0700 Subject: [PATCH] Fix the directory copy bug issue The issue was caused by a logic bug causing end of file to be reported for empty files. --- .../godotengine/godot/io/file/DataAccess.kt | 20 +++++++++++++++---- .../godot/io/file/FileAccessHandler.kt | 2 +- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/platform/android/java/lib/src/org/godotengine/godot/io/file/DataAccess.kt b/platform/android/java/lib/src/org/godotengine/godot/io/file/DataAccess.kt index 0fe51662a4d..aef1bed8ce7 100644 --- a/platform/android/java/lib/src/org/godotengine/godot/io/file/DataAccess.kt +++ b/platform/android/java/lib/src/org/godotengine/godot/io/file/DataAccess.kt @@ -103,6 +103,8 @@ internal abstract class DataAccess(private val filePath: String) { } protected abstract val fileChannel: FileChannel + internal var endOfFile = false + private set fun close() { try { @@ -123,6 +125,9 @@ internal abstract class DataAccess(private val filePath: String) { fun seek(position: Long) { try { fileChannel.position(position) + if (position <= size()) { + endOfFile = false + } } catch (e: Exception) { Log.w(TAG, "Exception when seeking file $filePath.", e) } @@ -153,11 +158,15 @@ internal abstract class DataAccess(private val filePath: String) { 0L } - fun isEndOfFile() = position() >= size() - fun read(buffer: ByteBuffer): Int { return try { - fileChannel.read(buffer) + val readBytes = fileChannel.read(buffer) + if (readBytes == -1) { + endOfFile = true + 0 + } else { + readBytes + } } catch (e: IOException) { Log.w(TAG, "Exception while reading from file $filePath.", e) 0 @@ -166,7 +175,10 @@ internal abstract class DataAccess(private val filePath: String) { fun write(buffer: ByteBuffer) { try { - fileChannel.write(buffer) + val writtenBytes = fileChannel.write(buffer) + if (writtenBytes > 0) { + endOfFile = false + } } catch (e: IOException) { Log.w(TAG, "Exception while writing to file $filePath.", e) } diff --git a/platform/android/java/lib/src/org/godotengine/godot/io/file/FileAccessHandler.kt b/platform/android/java/lib/src/org/godotengine/godot/io/file/FileAccessHandler.kt index e493f2449e2..b5044431445 100644 --- a/platform/android/java/lib/src/org/godotengine/godot/io/file/FileAccessHandler.kt +++ b/platform/android/java/lib/src/org/godotengine/godot/io/file/FileAccessHandler.kt @@ -186,7 +186,7 @@ class FileAccessHandler(val context: Context) { return false } - return files[fileId].isEndOfFile() + return files[fileId].endOfFile } fun fileClose(fileId: Int) {