Fixed exception when reading incompatible data, Updated license, fixed warnings.

This commit is contained in:
nathan 2014-11-24 17:57:08 +01:00
parent 567f790e3f
commit b307db95f8
3 changed files with 51 additions and 59 deletions

View File

@ -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; package dorkbox.util.storage;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@ -228,7 +243,7 @@ public class Metadata {
* Reads the record data for the given record header. * Reads the record data for the given record header.
*/ */
<T> T readData(Kryo kryo, InflaterInputStream inputStream) throws IOException { <T> T readData(Kryo kryo, InflaterInputStream inputStream) {
Input input = new Input(inputStream, 1024); // read 1024 at a time Input input = new Input(inputStream, 1024); // read 1024 at a time
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
T readObject = (T) kryo.readClassAndObject(input); T readObject = (T) kryo.readClassAndObject(input);

View File

@ -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; package dorkbox.util.storage;
import java.io.File; 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<? extends Object> sourceClass = source.getClass();
Class<? extends Object> 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;
}

View File

@ -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; package dorkbox.util.storage;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@ -24,6 +39,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.esotericsoftware.kryo.Kryo; import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.KryoException;
import com.esotericsoftware.kryo.io.Output; import com.esotericsoftware.kryo.io.Output;
import dorkbox.util.bytes.ByteArrayWrapper; import dorkbox.util.bytes.ByteArrayWrapper;
@ -264,6 +280,9 @@ public class StorageBase {
} }
return readRecordData; return readRecordData;
} catch (KryoException e) {
this.logger.error("Error while geting data from disk. Ignoring previous value.");
return null;
} catch (Exception e) { } catch (Exception e) {
this.logger.error("Error while geting data from disk", e); this.logger.error("Error while geting data from disk", e);
return null; 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. * 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) * (O(n) mem accesses)
*/ */
private final Metadata index_getMetaDataFromData(long targetFp) throws IOException { private final Metadata index_getMetaDataFromData(long targetFp) {
Iterator<Metadata> iterator = this.memoryIndex.values().iterator(); Iterator<Metadata> iterator = this.memoryIndex.values().iterator();
while (iterator.hasNext()) { while (iterator.hasNext()) {