Verified Commit f03848d6 authored by AtjonTV's avatar AtjonTV

FFDB: Check the database file size before trying to read it

parent a9f13421
......@@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Status number from `klib.files.Directory2.DirectoryError`
### Fixed
- FFDB now checks if the File exists before writing/reading
- FFDB now checks if the FIle is empty before reading
### Security
## 4.1.0 - 25.02.2020 - Release 24
......
......@@ -8,6 +8,7 @@ import klib.exceptions.IncompatibleDatabaseException
import klib.extensions.asFile
import klib.extensions.objectInputStream
import klib.extensions.objectOutputStream
import java.lang.Exception
/**
* FlatFile DataBase
......@@ -60,14 +61,10 @@ class FFDB(val storageFile: File, val schemaVersion: Int = Version.V2.version) {
if (!storageFile.exists()) {
storageFile.createNewFile()
}
when (schemaVersion) {
Version.V2.version -> {
val data = readAllV2(storageFile.objectInputStream())
data.forEach {
if (it != null) {
writeBuffer.add(it)
}
}
if (storageFile.length() > 0) {
when (schemaVersion) {
Version.V2.version -> preloadV2()
}
}
}
......@@ -238,4 +235,19 @@ class FFDB(val storageFile: File, val schemaVersion: Int = Version.V2.version) {
return ArrayList()
}
private fun preloadV2() {
try {
val data = readAllV2(storageFile.objectInputStream())
data.forEach {
if (it != null) {
writeBuffer.add(it)
}
}
}catch (e: Exception) {
println("Cannot preload FFDB Buffer due to the following error, proceeding with empty Buffer.")
clearBuffer()
e.printStackTrace()
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment