Added benchmark lib

This commit is contained in:
Ziver Koc 2013-08-02 14:49:16 +00:00
parent fe66cc9962
commit b9a662e4b3
5 changed files with 169 additions and 44 deletions

View file

@ -37,11 +37,11 @@ import javax.activation.UnsupportedDataTypeException;
public class JSONObjectInputStream extends InputStream implements ObjectInput, Closeable{
private JSONParser parser;
private HashMap<Integer, Object> objCache;
private HashMap<Integer, Object> objectCache;
public JSONObjectInputStream(Reader in) {
this.parser = new JSONParser(in);
this.objCache = new HashMap<Integer, Object>();
this.objectCache = new HashMap<Integer, Object>();
}
public Object readObject() throws IOException {
@ -60,15 +60,15 @@ public class JSONObjectInputStream extends InputStream implements ObjectInput, C
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
objCache.clear();
objectCache.clear();
}
return null;
}
protected Object readObject(DataNode json) throws IllegalAccessException, InstantiationException, ClassNotFoundException, IllegalArgumentException, UnsupportedDataTypeException {
// See if the Object id is in the cache before continuing
if(json.getString("@object_id") != null && objCache.containsKey(json.getInt("@object_id")))
return objCache.get(json.getInt("@object_id"));
if(json.getString("@object_id") != null && objectCache.containsKey(json.getInt("@object_id")))
return objectCache.get(json.getInt("@object_id"));
Class<?> objClass = Class.forName(json.getString("@class"));
Object obj = objClass.newInstance();
@ -87,7 +87,7 @@ public class JSONObjectInputStream extends InputStream implements ObjectInput, C
}
// Add object to the cache
if(json.getString("@object_id") != null)
objCache.put(json.getInt("@object_id"), obj);
objectCache.put(json.getInt("@object_id"), obj);
return obj;
}

View file

@ -89,6 +89,11 @@ public class JSONObjectOutputStream extends OutputStream implements ObjectOutput
out.write(new DataNode(s));
}
@Override
public void write(int b) throws IOException {
out.write(new DataNode(b));
}
public void writeUTF(String s) throws IOException {
out.write(new DataNode(s));
}
@ -98,6 +103,8 @@ public class JSONObjectOutputStream extends OutputStream implements ObjectOutput
out.write(getDataNode(obj));
} catch (IllegalAccessException e) {
throw new IOException("Unable to serialize object", e);
} finally {
objectCache.clear();
}
}
@ -185,11 +192,6 @@ public class JSONObjectOutputStream extends OutputStream implements ObjectOutput
return node;
}
@Override
public void write(int b) throws IOException {
// TODO:
}
/**
* Enable or disables the use of meta data in the JSON
* stream for class def and caching.
@ -200,11 +202,4 @@ public class JSONObjectOutputStream extends OutputStream implements ObjectOutput
generateMetaData = generate;
}
/**
* Reset the Object stream (clears the cache)
*/
public void reset(){
objectCache.clear();
}
}