From e9585ce5b70f8ebbaf8521f9922549df0a5efdd1 Mon Sep 17 00:00:00 2001 From: nathan Date: Sat, 18 Feb 2017 23:58:08 +0100 Subject: [PATCH] Updated storage to throw IOException when there is a problem deserializing the data from disk --- src/dorkbox/util/storage/DiskStorage.java | 21 ++++++++++++++++----- src/dorkbox/util/storage/Storage.java | 8 ++++---- src/dorkbox/util/storage/StorageBase.java | 14 +++++++------- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/src/dorkbox/util/storage/DiskStorage.java b/src/dorkbox/util/storage/DiskStorage.java index 3b37ea2..983ad2f 100644 --- a/src/dorkbox/util/storage/DiskStorage.java +++ b/src/dorkbox/util/storage/DiskStorage.java @@ -35,6 +35,7 @@ import dorkbox.util.bytes.ByteArrayWrapper; *

* Be wary of opening the database file in different JVM instances. Even with file-locks, you can corrupt the data. */ +@SuppressWarnings({"Convert2Diamond", "Convert2Lambda"}) class DiskStorage implements Storage { private final DelayTimer timer; private final ByteArrayWrapper defaultKey; @@ -122,37 +123,45 @@ class DiskStorage implements Storage { /** * Reads a object using the default (blank) key, and casts it to the expected class + * + * @throws IOException if there is a problem deserializing data from disk */ @Override public final - T get() { + T get() throws IOException { return get0(this.defaultKey); } /** * Reads a object using the specific key, and casts it to the expected class + * + * @throws IOException if there is a problem deserializing data from disk */ @Override public final - T get(String key) { + T get(String key) throws IOException { return get0(ByteArrayWrapper.wrap(key)); } /** * Reads a object using the specific key, and casts it to the expected class + * + * @throws IOException if there is a problem deserializing data from disk */ @Override public final - T get(byte[] key) { + T get(byte[] key) throws IOException { return get0(ByteArrayWrapper.wrap(key)); } /** * Reads a object using the specific key, and casts it to the expected class + * + * @throws IOException if there is a problem deserializing data from disk */ @Override public final - T get(ByteArrayWrapper key) { + T get(ByteArrayWrapper key) throws IOException { return get0(key); } @@ -223,9 +232,11 @@ class DiskStorage implements Storage { /** * Reads a object from pending or from storage + * + * @throws IOException if there is a problem deserializing data from disk */ private - T get0(ByteArrayWrapper key) { + T get0(ByteArrayWrapper key) throws IOException { if (!this.isOpen.get()) { throw new RuntimeException("Unable to act on closed storage"); } diff --git a/src/dorkbox/util/storage/Storage.java b/src/dorkbox/util/storage/Storage.java index 19afde3..2cf65fb 100644 --- a/src/dorkbox/util/storage/Storage.java +++ b/src/dorkbox/util/storage/Storage.java @@ -38,22 +38,22 @@ interface Storage { /** * Reads a object using the DEFAULT key ("") key, and casts it to the expected class */ - T get(); + T get() throws IOException; /** * Reads a object using the specific key, and casts it to the expected class */ - T get(String key); + T get(String key) throws IOException; /** * Reads a object using the specific key, and casts it to the expected class */ - T get(byte[] key); + T get(byte[] key) throws IOException; /** * Reads a object using the specific key, and casts it to the expected class */ - T get(ByteArrayWrapper key); + T get(ByteArrayWrapper key) throws IOException; /** * Uses the DEFAULT key ("") to return saved data. diff --git a/src/dorkbox/util/storage/StorageBase.java b/src/dorkbox/util/storage/StorageBase.java index cbee2be..4576a44 100644 --- a/src/dorkbox/util/storage/StorageBase.java +++ b/src/dorkbox/util/storage/StorageBase.java @@ -33,9 +33,11 @@ import java.util.concurrent.locks.ReentrantLock; import org.slf4j.Logger; +import com.esotericsoftware.kryo.KryoException; import com.esotericsoftware.kryo.io.Input; import com.esotericsoftware.kryo.io.Output; +import dorkbox.util.OS; import dorkbox.util.SerializationManager; import dorkbox.util.bytes.ByteArrayWrapper; @@ -251,7 +253,7 @@ class StorageBase { * @return an object for a specified key form referenceCache FIRST, then from DISK */ final - T get(ByteArrayWrapper key) { + T get(ByteArrayWrapper key) throws IOException { // NOT protected by lock Metadata meta = this.memoryIndex.get(key); @@ -298,13 +300,11 @@ class StorageBase { return readRecordData; } catch (Exception e) { - if (this.logger != null) { - this.logger.error("Error while getting data from disk", e); + if (e instanceof KryoException && e.getMessage().contains("(missing no-arg constructor)")) { + throw new IOException("Cannot get data from disk: " + e.getMessage().substring(0, e.getMessage().indexOf(OS.LINE_SEPARATOR))); + } else { + throw new IOException("Cannot get data from disk", e); } - else { - e.printStackTrace(); - } - return null; } }