Skip to content

Commit

Permalink
Added AsmFieldSerializer.
Browse files Browse the repository at this point in the history
Minor updates.
  • Loading branch information
NathanSweet committed Jan 10, 2010
1 parent 0c48ab9 commit 628a247
Show file tree
Hide file tree
Showing 7 changed files with 505 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
<classpathentry excluding="**/.svn/*" kind="src" path="test"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="build/junit-4.6.jar"/>
<classpathentry exported="true" kind="lib" path="lib/minlog-1.1.jar"/>
<classpathentry kind="lib" path="lib/asm-3.2.jar" sourcepath="C:/Dev/Java/Misc/asm-3.2-src/src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
Binary file added lib/asm-3.2.jar
Binary file not shown.
9 changes: 5 additions & 4 deletions src/com/esotericsoftware/kryo/Kryo.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.nio.ByteBuffer;
import java.util.Collection;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

Expand Down Expand Up @@ -44,7 +45,7 @@ protected Context initialValue () {
};

private final IntHashMap<RegisteredClass> idToRegisteredClass = new IntHashMap(64);
private final HashMap<Class, RegisteredClass> classToRegisteredClass = new HashMap(64);
private final IdentityHashMap<Class, RegisteredClass> classToRegisteredClass = new IdentityHashMap(64);
private AtomicInteger nextClassID = new AtomicInteger(1);
private Object listenerLock = new Object();
private KryoListener[] listeners = {};
Expand Down Expand Up @@ -254,7 +255,7 @@ public Serializer getSerializer (Class type) {
* Writes the ID of the specified class to the buffer. The ID will be an int for {@link #register(Class, Serializer) registered
* classes}. If {@link #setAllowUnregisteredClasses(boolean) allowUnregisteredClasses} is true and the class was not explicitly
* registered, the ID will be the class name String.
* @param type Can be null (writes a special class ID for a null object).
* @param type Can be null (writes a special ID for a null object).
* @return The registered information for the class that was written, or null of the specified class was null.
*/
public RegisteredClass writeClass (ByteBuffer buffer, Class type) {
Expand Down Expand Up @@ -306,7 +307,7 @@ public RegisteredClass readClass (ByteBuffer buffer) {
/**
* Writes the object's class ID to the buffer, then uses the serializer registered for that class to write the object to the
* buffer.
* @param object Can be null (writes a special class ID for a null object instead).
* @param object Can be null (writes a special ID for a null object instead).
*/
public void writeClassAndObject (ByteBuffer buffer, Object object) {
if (object == null) {
Expand All @@ -324,7 +325,7 @@ public void writeClassAndObject (ByteBuffer buffer, Object object) {

/**
* Uses the serializer registered for the object's class to write the object to the buffer.
* @param object Can be null (writes a special class ID for a null object instead).
* @param object Can be null (writes a special ID for a null object instead).
*/
public void writeObject (ByteBuffer buffer, Object object) {
if (object == null) {
Expand Down
9 changes: 6 additions & 3 deletions src/com/esotericsoftware/kryo/Serializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
* @author Nathan Sweet <[email protected]>
*/
abstract public class Serializer {
static private final byte NULL_OBJECT = 0;
static private final byte NOT_NULL_OBJECT = 1;

private boolean canBeNull = true;

/**
Expand All @@ -30,10 +33,10 @@ public final void writeObject (ByteBuffer buffer, Object object) {
if (canBeNull) {
if (object == null) {
if (TRACE) trace("kryo", "Wrote object: null");
buffer.put((byte)0);
buffer.put(NULL_OBJECT);
return;
}
buffer.put((byte)1);
buffer.put(NOT_NULL_OBJECT);
}
writeObjectData(buffer, object);
}
Expand All @@ -49,7 +52,7 @@ public final void writeObject (ByteBuffer buffer, Object object) {
* @return The deserialized object, or null if the object read from the buffer was a null.
*/
public final <T> T readObject (ByteBuffer buffer, Class<T> type) {
if (canBeNull && buffer.get() == 0) {
if (canBeNull && buffer.get() == NULL_OBJECT) {
if (TRACE) trace("kryo", "Read object: null");
return null;
}
Expand Down
17 changes: 11 additions & 6 deletions src/com/esotericsoftware/kryo/serialize/ArraySerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,11 @@ public void writeObjectData (ByteBuffer buffer, Object array) {
// If element class is final (this includes primitives) then all elements are the same type.
Serializer elementSerializer = null;
Class elementClass = getElementClass(array.getClass());
boolean elementsCanBeNull = this.elementsCanBeNull && !elementClass.isPrimitive();
if (elementsAreSameType || Modifier.isFinal(elementClass.getModifiers()))
elementSerializer = kryo.getRegisteredClass(elementClass).serializer;
// Write array data.
writeArray(buffer, array, elementSerializer, 0, dimensions.length);
writeArray(buffer, array, elementSerializer, 0, dimensions.length, elementsCanBeNull);
if (TRACE) {
StringBuilder stringBuffer = new StringBuilder(16);
for (int i = 0, n = dimensions.length; i < n; i++) {
Expand All @@ -96,7 +97,8 @@ public void writeObjectData (ByteBuffer buffer, Object array) {
}
}

private void writeArray (ByteBuffer buffer, Object array, Serializer elementSerializer, int dimension, int dimensionCount) {
private void writeArray (ByteBuffer buffer, Object array, Serializer elementSerializer, int dimension, int dimensionCount,
boolean elementsCanBeNull) {
int length = Array.getLength(array);
if (dimension > 0) {
// Write array length. With Java's "jagged arrays" this could be less than the dimension size.
Expand All @@ -108,7 +110,8 @@ private void writeArray (ByteBuffer buffer, Object array, Serializer elementSeri
Object element = Array.get(array, i);
if (elementsAreArrays) {
// Nested array.
if (element != null) writeArray(buffer, element, elementSerializer, dimension + 1, dimensionCount);
if (element != null)
writeArray(buffer, element, elementSerializer, dimension + 1, dimensionCount, elementsCanBeNull);
} else if (elementSerializer != null) {
// Use same serializer for all elements.
if (elementsCanBeNull)
Expand Down Expand Up @@ -136,11 +139,12 @@ public <T> T readObjectData (ByteBuffer buffer, Class<T> type) {
// Get element serializer if all elements are the same type.
Serializer elementSerializer = null;
Class elementClass = getElementClass(type);
boolean elementsCanBeNull = this.elementsCanBeNull && !elementClass.isPrimitive();
if (elementsAreSameType || Modifier.isFinal(elementClass.getModifiers()))
elementSerializer = kryo.getRegisteredClass(elementClass).serializer;
// Create array and read in the data.
T array = (T)Array.newInstance(elementClass, dimensions);
readArray(buffer, array, elementSerializer, elementClass, 0, dimensions);
readArray(buffer, array, elementSerializer, elementClass, 0, dimensions, elementsCanBeNull);
if (TRACE) {
StringBuilder stringBuffer = new StringBuilder(16);
for (int i = 0; i < dimensionCount; i++) {
Expand All @@ -154,7 +158,7 @@ public <T> T readObjectData (ByteBuffer buffer, Class<T> type) {
}

private void readArray (ByteBuffer buffer, Object array, Serializer elementSerializer, Class elementClass, int dimension,
int[] dimensions) {
int[] dimensions, boolean elementsCanBeNull) {
boolean elementsAreArrays = dimension < dimensions.length - 1;
int length;
if (dimension == 0)
Expand All @@ -165,7 +169,8 @@ private void readArray (ByteBuffer buffer, Object array, Serializer elementSeria
if (elementsAreArrays) {
// Nested array.
Object element = Array.get(array, i);
if (element != null) readArray(buffer, element, elementSerializer, elementClass, dimension + 1, dimensions);
if (element != null)
readArray(buffer, element, elementSerializer, elementClass, dimension + 1, dimensions, elementsCanBeNull);
} else if (elementSerializer != null) {
// Use same serializer (and class) for all elements.
if (elementsCanBeNull)
Expand Down
Loading

0 comments on commit 628a247

Please sign in to comment.