@Override public Object newInstance () { try { return constructor.newInstance(); } catch (Exception ex) { throw new KryoException("Error constructing instance of class: " + className(type), ex); } } };
/** Returns the class formatted as a string. The format varies depending on the type. */ static public String className (Class type) { if (type.isArray()) { Class elementClass = getElementClass(type); StringBuilder buffer = new StringBuilder(16); for (int i = 0, n = getDimensionCount(type); i < n; i++) buffer.append("[]"); return className(elementClass) + buffer; } if (type.isPrimitive() || type == Object.class || type == Boolean.class || type == Byte.class || type == Character.class || type == Short.class || type == Integer.class || type == Long.class || type == Float.class || type == Double.class || type == String.class) { return type.getSimpleName(); } return type.getName(); }
/** Returns false for all primitive wrappers. */ public boolean useReferences (Class type) { return !Util.isWrapperClass(type); } }
if (type.isPrimitive()) type = getWrapperClass(type); boolean referencesSupported = referenceResolver.useReferences(type); int id; id = input.readVarInt(true); if (id == Kryo.NULL) { if (TRACE || (DEBUG && depth == 1)) log("Read", null); readObject = null; return REF; if (TRACE) trace("kryo", "Read initial object reference " + id + ": " + className(type)); readReferenceIds.add(id); return readReferenceIds.size; if (DEBUG) debug("kryo", "Read object reference " + id + ": " + string(readObject)); return REF;
public Registration readClass (Input input) { int classID = input.readVarInt(true); switch (classID) { case Kryo.NULL: if (TRACE || (DEBUG && kryo.getDepth() == 1)) log("Read", null); return null; case NAME + 2: // Offset for NAME and NULL. return readName(input); } if (classID == memoizedClassId) return memoizedClassIdValue; Registration registration = idToRegistration.get(classID - 2); if (registration == null) throw new KryoException("Encountered unregistered class ID: " + (classID - 2)); if (TRACE) trace("kryo", "Read class " + (classID - 2) + ": " + className(registration.getType())); memoizedClassId = classID; memoizedClassIdValue = registration; return registration; }
public Registration register (Registration registration) { if (registration == null) throw new IllegalArgumentException("registration cannot be null."); if (registration.getId() != NAME) { if (TRACE) { trace("kryo", "Register class ID " + registration.getId() + ": " + className(registration.getType()) + " (" + registration.getSerializer().getClass().getName() + ")"); } idToRegistration.put(registration.getId(), registration); } else if (TRACE) { trace("kryo", "Register class name: " + className(registration.getType()) + " (" + registration.getSerializer().getClass().getName() + ")"); } classToRegistration.put(registration.getType(), registration); if (registration.getType().isPrimitive()) classToRegistration.put(getWrapperClass(registration.getType()), registration); return registration; }
/** Writes an object using the specified serializer. The registered serializer is ignored. */ public void writeObject (Output output, Object object, Serializer serializer) { if (output == null) throw new IllegalArgumentException("output cannot be null."); if (object == null) throw new IllegalArgumentException("object cannot be null."); if (serializer == null) throw new IllegalArgumentException("serializer cannot be null."); beginObject(); try { if (references && writeReferenceOrNull(output, object, false)) { serializer.setGenerics(this, null); return; } if (TRACE || (DEBUG && depth == 1)) log("Write", object); serializer.write(this, output, object); } finally { if (--depth == 0 && autoReset) reset(); } }
/** @param object May be null if mayBeNull is true. * @return true if no bytes need to be written for the object. */ boolean writeReferenceOrNull (Output output, Object object, boolean mayBeNull) { if (object == null) { if (TRACE || (DEBUG && depth == 1)) log("Write", null); output.writeVarInt(Kryo.NULL, true); return true; } if (!referenceResolver.useReferences(object.getClass())) { if (mayBeNull) output.writeVarInt(Kryo.NOT_NULL, true); return false; } // Determine if this object has already been seen in this object graph. int id = referenceResolver.getWrittenId(object); // If not the first time encountered, only write reference ID. if (id != -1) { if (DEBUG) debug("kryo", "Write object reference " + id + ": " + string(object)); output.writeVarInt(id + 2, true); // + 2 because 0 and 1 are used for NULL and NOT_NULL. return true; } // Otherwise write NOT_NULL and then the object bytes. id = referenceResolver.addWrittenObject(object); output.writeVarInt(NOT_NULL, true); if (TRACE) trace("kryo", "Write initial object reference " + id + ": " + string(object)); return false; }
public Class read (Kryo kryo, Input input, Class<Class> type) { Registration registration = kryo.readClass(input); int isPrimitive = input.read(); Class typ = registration != null ? registration.getType() : null; if (typ == null || !typ.isPrimitive()) return typ; return (isPrimitive == 1) ? typ : getWrapperClass(typ); } }
final private void writeLittleEndianLong (long val) { if (isLittleEndian) writeLong(val); else writeLong(Util.swapLong(val)); }
final private void writeLittleEndianInt (int val) { if (isLittleEndian) writeInt(val); else writeInt(Util.swapInt(val)); }
/** Returns true if the specified type is final. Final types can be serialized more efficiently because they are * non-polymorphic. * <p> * This can be overridden to force non-final classes to be treated as final. Eg, if an application uses ArrayList extensively * but never uses an ArrayList subclass, treating ArrayList as final could allow FieldSerializer to save 1-2 bytes per * ArrayList field. */ public boolean isFinal (Class type) { if (type == null) throw new IllegalArgumentException("type cannot be null."); if (type.isArray()) return Modifier.isFinal(Util.getElementClass(type).getModifiers()); return Modifier.isFinal(type.getModifiers()); }
/** Logs a message about an object. The log level and the string format of the object depend on the object type. */ static public void log (String message, Object object) { if (object == null) { if (TRACE) trace("kryo", message + ": null"); return; } Class type = object.getClass(); if (type.isPrimitive() || type == Boolean.class || type == Byte.class || type == Character.class || type == Short.class || type == Integer.class || type == Long.class || type == Float.class || type == Double.class || type == String.class) { if (TRACE) trace("kryo", message + ": " + string(object)); } else { debug("kryo", message + ": " + string(object)); } }
if (type.isPrimitive()) type = getWrapperClass(type); boolean referencesSupported = referenceResolver.useReferences(type); int id; id = input.readVarInt(true); if (id == Kryo.NULL) { if (TRACE || (DEBUG && depth == 1)) log("Read", null); readObject = null; return REF; if (TRACE) trace("kryo", "Read initial object reference " + id + ": " + className(type)); readReferenceIds.add(id); return readReferenceIds.size; if (DEBUG) debug("kryo", "Read object reference " + id + ": " + string(readObject)); return REF;
public Registration readClass (Input input) { int classID = input.readVarInt(true); switch (classID) { case Kryo.NULL: if (TRACE || (DEBUG && kryo.getDepth() == 1)) log("Read", null); return null; case NAME + 2: // Offset for NAME and NULL. return readName(input); } if (classID == memoizedClassId) return memoizedClassIdValue; Registration registration = idToRegistration.get(classID - 2); if (registration == null) throw new KryoException("Encountered unregistered class ID: " + (classID - 2)); if (TRACE) trace("kryo", "Read class " + (classID - 2) + ": " + className(registration.getType())); memoizedClassId = classID; memoizedClassIdValue = registration; return registration; }
public Registration register (Registration registration) { if (registration == null) throw new IllegalArgumentException("registration cannot be null."); if (registration.getId() != NAME) { if (TRACE) { trace("kryo", "Register class ID " + registration.getId() + ": " + className(registration.getType()) + " (" + registration.getSerializer().getClass().getName() + ")"); } idToRegistration.put(registration.getId(), registration); } else if (TRACE) { trace("kryo", "Register class name: " + className(registration.getType()) + " (" + registration.getSerializer().getClass().getName() + ")"); } classToRegistration.put(registration.getType(), registration); if (registration.getType().isPrimitive()) classToRegistration.put(getWrapperClass(registration.getType()), registration); return registration; }
/** Writes an object using the specified serializer. The registered serializer is ignored. */ public void writeObject (Output output, Object object, Serializer serializer) { if (output == null) throw new IllegalArgumentException("output cannot be null."); if (object == null) throw new IllegalArgumentException("object cannot be null."); if (serializer == null) throw new IllegalArgumentException("serializer cannot be null."); beginObject(); try { if (references && writeReferenceOrNull(output, object, false)) { serializer.setGenerics(this, null); return; } if (TRACE || (DEBUG && depth == 1)) log("Write", object); serializer.write(this, output, object); } finally { if (--depth == 0 && autoReset) reset(); } }
/** @param object May be null if mayBeNull is true. * @return true if no bytes need to be written for the object. */ boolean writeReferenceOrNull (Output output, Object object, boolean mayBeNull) { if (object == null) { if (TRACE || (DEBUG && depth == 1)) log("Write", null); output.writeVarInt(Kryo.NULL, true); return true; } if (!referenceResolver.useReferences(object.getClass())) { if (mayBeNull) output.writeVarInt(Kryo.NOT_NULL, true); return false; } // Determine if this object has already been seen in this object graph. int id = referenceResolver.getWrittenId(object); // If not the first time encountered, only write reference ID. if (id != -1) { if (DEBUG) debug("kryo", "Write object reference " + id + ": " + string(object)); output.writeVarInt(id + 2, true); // + 2 because 0 and 1 are used for NULL and NOT_NULL. return true; } // Otherwise write NOT_NULL and then the object bytes. id = referenceResolver.addWrittenObject(object); output.writeVarInt(NOT_NULL, true); if (TRACE) trace("kryo", "Write initial object reference " + id + ": " + string(object)); return false; }
public Class read (Kryo kryo, Input input, Class<Class> type) { Registration registration = kryo.readClass(input); int isPrimitive = input.read(); Class typ = registration != null ? registration.getType() : null; if (typ == null || !typ.isPrimitive()) return typ; return (isPrimitive == 1) ? typ : getWrapperClass(typ); } }
final private void writeLittleEndianLong (long val) { if (isLittleEndian) writeLong(val); else writeLong(Util.swapLong(val)); }