diff --git a/Dorkbox-Util/src/dorkbox/util/storage/Storage.java b/Dorkbox-Util/src/dorkbox/util/storage/Storage.java index 1167c20..de7be61 100644 --- a/Dorkbox-Util/src/dorkbox/util/storage/Storage.java +++ b/Dorkbox-Util/src/dorkbox/util/storage/Storage.java @@ -59,9 +59,7 @@ public class Storage { storage.increaseReference(); } else { try { - StorageBase storageBase = new StorageBase(file); - - storage = new Storage(storageBase); + storage = new Storage(file); storages.put(file, storage); } catch (IOException e) { logger.error("Unable to open storage", e); @@ -270,8 +268,8 @@ public class Storage { /** * Creates or opens a new database file. */ - private Storage(StorageBase _storage) { - this.storage = _storage; + private Storage(File storageFile) throws IOException { + this.storage = new StorageBase(storageFile); this.defaultKey = wrap(""); this.timer = new DelayTimer("Storage Writer", false, new DelayTimer.Callback() { diff --git a/Dorkbox-Util/src/dorkbox/util/storage/StorageBase.java b/Dorkbox-Util/src/dorkbox/util/storage/StorageBase.java index 214059f..4c9af3a 100644 --- a/Dorkbox-Util/src/dorkbox/util/storage/StorageBase.java +++ b/Dorkbox-Util/src/dorkbox/util/storage/StorageBase.java @@ -93,6 +93,8 @@ public class StorageBase { * Creates or opens a new database file. */ StorageBase(File filePath) throws IOException { + this.logger.info("Opening storage file: '{}'", filePath.getAbsolutePath()); + this.baseFile = filePath; File parentFile = this.baseFile.getParentFile(); @@ -119,6 +121,14 @@ public class StorageBase { this.file.setLength(indexPointer); } + if (this.file.length() < this.dataPosition) { + this.logger.error("Corrupted storage file!"); + throw new IllegalArgumentException("Unable to parse header information from storage. Maybe it's corrupted?"); + } + + this.logger.info("Storage version: {}", this.databaseVersion); + + this.kryo = new Kryo(); this.kryo.setRegistrationRequired(false); diff --git a/Dorkbox-Util/test/dorkbox/util/StorageTest.java b/Dorkbox-Util/test/dorkbox/util/StorageTest.java index ed71b18..dd25b96 100644 --- a/Dorkbox-Util/test/dorkbox/util/StorageTest.java +++ b/Dorkbox-Util/test/dorkbox/util/StorageTest.java @@ -10,14 +10,13 @@ import java.util.Arrays; import org.junit.After; import org.junit.Assert; import org.junit.Before; +import org.junit.FixMethodOrder; import org.junit.Test; +import org.junit.runners.MethodSorters; import dorkbox.util.storage.Storage; -/** - * Simple test class for the RecordsFile example. To run the test, set you CLASSPATH and then type - * "java hamner.dbtest.TestRecords" - */ +@FixMethodOrder(MethodSorters.NAME_ASCENDING) public class StorageTest { private static final String TEST_DB = "sampleFile.records";