You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-11-09 12:50:35 +00:00
[FileAccess] Implement resize method.
This commit is contained in:
@@ -36,7 +36,9 @@ import android.util.Log
|
||||
import org.godotengine.godot.io.StorageScope
|
||||
import java.io.IOException
|
||||
import java.nio.ByteBuffer
|
||||
import java.nio.channels.ClosedChannelException
|
||||
import java.nio.channels.FileChannel
|
||||
import java.nio.channels.NonWritableChannelException
|
||||
import kotlin.math.max
|
||||
|
||||
/**
|
||||
@@ -50,6 +52,11 @@ internal abstract class DataAccess(private val filePath: String) {
|
||||
companion object {
|
||||
private val TAG = DataAccess::class.java.simpleName
|
||||
|
||||
private const val OK_ERROR_ID = 0;
|
||||
private const val FAILED_ERROR_ID = -1;
|
||||
private const val FILE_CANT_OPEN_ERROR_ID = -2;
|
||||
private const val INVALID_PARAMETER_ERROR_ID = -3;
|
||||
|
||||
fun generateDataAccess(
|
||||
storageScope: StorageScope,
|
||||
context: Context,
|
||||
@@ -135,6 +142,21 @@ internal abstract class DataAccess(private val filePath: String) {
|
||||
seek(positionFromBeginning)
|
||||
}
|
||||
|
||||
fun resize(length: Long): Int {
|
||||
return try {
|
||||
fileChannel.truncate(length)
|
||||
OK_ERROR_ID
|
||||
} catch (e: NonWritableChannelException) {
|
||||
FILE_CANT_OPEN_ERROR_ID
|
||||
} catch (e: ClosedChannelException) {
|
||||
FILE_CANT_OPEN_ERROR_ID
|
||||
} catch (e: IllegalArgumentException) {
|
||||
INVALID_PARAMETER_ERROR_ID
|
||||
} catch (e: IOException) {
|
||||
FAILED_ERROR_ID
|
||||
}
|
||||
}
|
||||
|
||||
fun position(): Long {
|
||||
return try {
|
||||
fileChannel.position()
|
||||
|
||||
@@ -45,6 +45,7 @@ class FileAccessHandler(val context: Context) {
|
||||
companion object {
|
||||
private val TAG = FileAccessHandler::class.java.simpleName
|
||||
|
||||
private const val FAILED_ERROR_ID = -1;
|
||||
private const val FILE_NOT_FOUND_ERROR_ID = -1
|
||||
internal const val INVALID_FILE_ID = 0
|
||||
private const val STARTING_FILE_ID = 1
|
||||
@@ -190,6 +191,14 @@ class FileAccessHandler(val context: Context) {
|
||||
}
|
||||
}
|
||||
|
||||
fun fileResize(fileId: Int, length: Long): Int {
|
||||
if (!hasFileId(fileId)) {
|
||||
return FAILED_ERROR_ID
|
||||
}
|
||||
|
||||
return files[fileId].resize(length)
|
||||
}
|
||||
|
||||
fun fileGetPosition(fileId: Int): Long {
|
||||
if (!hasFileId(fileId)) {
|
||||
return 0L
|
||||
|
||||
Reference in New Issue
Block a user