From b307db95f80570c4d7bb37c3f9b3e71195c0c0a8 Mon Sep 17 00:00:00 2001 From: nathan Date: Mon, 24 Nov 2014 17:57:08 +0100 Subject: [PATCH] Fixed exception when reading incompatible data, Updated license, fixed warnings. --- .../src/dorkbox/util/storage/Metadata.java | 17 ++++- .../src/dorkbox/util/storage/Storage.java | 72 ++++--------------- .../src/dorkbox/util/storage/StorageBase.java | 21 +++++- 3 files changed, 51 insertions(+), 59 deletions(-) diff --git a/Dorkbox-Util/src/dorkbox/util/storage/Metadata.java b/Dorkbox-Util/src/dorkbox/util/storage/Metadata.java index 2d5fe8e..8bae1cf 100644 --- a/Dorkbox-Util/src/dorkbox/util/storage/Metadata.java +++ b/Dorkbox-Util/src/dorkbox/util/storage/Metadata.java @@ -1,3 +1,18 @@ +/* + * Copyright 2014 dorkbox, llc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package dorkbox.util.storage; import java.io.ByteArrayOutputStream; @@ -228,7 +243,7 @@ public class Metadata { * Reads the record data for the given record header. */ - T readData(Kryo kryo, InflaterInputStream inputStream) throws IOException { + T readData(Kryo kryo, InflaterInputStream inputStream) { Input input = new Input(inputStream, 1024); // read 1024 at a time @SuppressWarnings("unchecked") T readObject = (T) kryo.readClassAndObject(input); diff --git a/Dorkbox-Util/src/dorkbox/util/storage/Storage.java b/Dorkbox-Util/src/dorkbox/util/storage/Storage.java index 22e668e..c0c9713 100644 --- a/Dorkbox-Util/src/dorkbox/util/storage/Storage.java +++ b/Dorkbox-Util/src/dorkbox/util/storage/Storage.java @@ -1,3 +1,18 @@ +/* + * Copyright 2014 dorkbox, llc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package dorkbox.util.storage; import java.io.File; @@ -188,63 +203,6 @@ public class Storage { } } - /** - * @return true if all of the fields in the two objects are the same. - * - * NOTE: This is SLIGHTLY different than .equals(), in that there doesn't have to - * be an EXPLICIT .equals() method in the object - */ - private static boolean compareFields(Object source, Object dest) { - Class sourceClass = source.getClass(); - Class destClass = dest.getClass(); - - if (sourceClass != destClass) { - throw new IllegalArgumentException("Source and Dest objects are not of the same class!"); - } - - // have to walk up the object hierarchy. - while (destClass != Object.class) { - Field[] destFields = destClass.getDeclaredFields(); - - for (Field destField : destFields) { - String name = destField.getName(); - try { - Field sourceField = sourceClass.getDeclaredField(name); - destField.setAccessible(true); - sourceField.setAccessible(true); - - Object sourceObj = sourceField.get(source); - Object destObj = destField.get(dest); - - if (sourceObj == null) { - if (destObj == null) { - return true; - } else { - return false; - } - } else { - if (destObj == null) { - return false; - } else { - return destObj.equals(sourceObj); - } - } - } catch (Exception e) { - logger.error("Unable to copy field: {}", name, e); - return false; - } - } - - destClass = destClass.getSuperclass(); - sourceClass = sourceClass.getSuperclass(); - } - - return true; - } - - - - diff --git a/Dorkbox-Util/src/dorkbox/util/storage/StorageBase.java b/Dorkbox-Util/src/dorkbox/util/storage/StorageBase.java index f240208..487120b 100644 --- a/Dorkbox-Util/src/dorkbox/util/storage/StorageBase.java +++ b/Dorkbox-Util/src/dorkbox/util/storage/StorageBase.java @@ -1,3 +1,18 @@ +/* + * Copyright 2014 dorkbox, llc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package dorkbox.util.storage; import java.io.ByteArrayOutputStream; @@ -24,6 +39,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.KryoException; import com.esotericsoftware.kryo.io.Output; import dorkbox.util.bytes.ByteArrayWrapper; @@ -264,6 +280,9 @@ public class StorageBase { } return readRecordData; + } catch (KryoException e) { + this.logger.error("Error while geting data from disk. Ignoring previous value."); + return null; } catch (Exception e) { this.logger.error("Error while geting data from disk", e); return null; @@ -589,7 +608,7 @@ public class StorageBase { * of the record data of the RecordHeader which is returned. Returns null if the location is not part of a record. * (O(n) mem accesses) */ - private final Metadata index_getMetaDataFromData(long targetFp) throws IOException { + private final Metadata index_getMetaDataFromData(long targetFp) { Iterator iterator = this.memoryIndex.values().iterator(); while (iterator.hasNext()) {