@SuppressWarnings({"unchecked", "rawtypes"}) @Override public void write(Kryo kryo, Output output, T o) { try { ObjectMap graphContext = kryo.getGraphContext(); ObjectOutputStream objectStream = (ObjectOutputStream)graphContext.get(this); if (objectStream == null) { objectStream = new ObjectOutputStream(output); graphContext.put(this, objectStream); } objectStream.writeObject(o); objectStream.flush(); } catch (Exception ex) { throw new KryoException("Error during Java serialization.", ex); } }
/** Reduces the size of the backing arrays to be the specified capacity or less. If the capacity is already less, nothing is * done. If the map contains more items than the specified capacity, the next highest power of two capacity is used instead. */ public void shrink (int maximumCapacity) { if (maximumCapacity < 0) throw new IllegalArgumentException("maximumCapacity must be >= 0: " + maximumCapacity); if (size > maximumCapacity) maximumCapacity = size; if (capacity <= maximumCapacity) return; maximumCapacity = ObjectMap.nextPowerOfTwo(maximumCapacity); resize(maximumCapacity); }
@SuppressWarnings({"unchecked", "rawtypes"}) @Override public T read(Kryo kryo, Input input, Class aClass) { try { ObjectMap graphContext = kryo.getGraphContext(); ObjectInputStream objectStream = (ObjectInputStream)graphContext.get(this); if (objectStream == null) { // make sure we use Kryo's classloader objectStream = new InstantiationUtil.ClassLoaderObjectInputStream(input, kryo.getClassLoader()); graphContext.put(this, objectStream); } return (T) objectStream.readObject(); } catch (Exception ex) { throw new KryoException("Error during Java deserialization.", ex); } }
public synchronized Object read (Connection connection, ByteBuffer buffer) { input.setBuffer(buffer); kryo.getContext().put("connection", connection); return kryo.readClassAndObject(input); }
public synchronized void write (Connection connection, ByteBuffer buffer, Object object) { output.setBuffer(buffer); kryo.getContext().put("connection", connection); kryo.writeClassAndObject(output, object); output.flush(); }
protected Registration readName (Input input) { int nameId = input.readVarInt(true); if (nameIdToClass == null) nameIdToClass = new IntMap(); Class type = nameIdToClass.get(nameId); if (type == null) { // Only read the class name the first time encountered in object graph. String className = input.readString(); if (nameToClass != null) type = nameToClass.get(className); if (type == null) { try { type = Class.forName(className, false, kryo.getClassLoader()); } catch (ClassNotFoundException ex) { throw new KryoException("Unable to find class: " + className, ex); } if (nameToClass == null) nameToClass = new ObjectMap(); nameToClass.put(className, type); } nameIdToClass.put(nameId, type); if (TRACE) trace("kryo", "Read class name: " + className); } else { if (TRACE) trace("kryo", "Read class name reference " + nameId + ": " + className(type)); } return kryo.getRegistration(type); }
T object = kryo.newInstance(type); kryo.reference(object); ObjectMap context = kryo.getGraphContext(); CachedField[] fields = (CachedField[]) context.get(this); if (fields == null) { int length = input.readInt(true); if (TRACE) { trace("kryo", "Read " + length + " field names."); trace("kryo", "Ignoring obsolete field with marker: " + fieldMarker); context.put(this, fields); if (cachedField == null) { if (TRACE) { trace("kryo", "Skip obsolete field.");
public void write (Kryo kryo, Output output, T object) { CachedField[] fields = getFields(); ObjectMap context = kryo.getGraphContext(); if (!context.containsKey(this)) { context.put(this, null); if (TRACE) trace("kryo", "Write " + fields.length + " field names."); output.writeVarInt(fields.length, true); for (int i = 0, n = fields.length; i < n; i++) output.writeString(fields[i].field.getName()); } OutputChunked outputChunked = new OutputChunked(output, 1024); for (int i = 0, n = fields.length; i < n; i++) { fields[i].write(outputChunked, object); outputChunked.endChunks(); } }
public Object read (Kryo kryo, Input input, Class type) { int objectID = input.readInt(true); Connection connection = (Connection)kryo.getContext().get("connection"); Object object = getRegisteredObject(connection, objectID); if (WARN && object == null) warn("kryonet", "Unknown object ID " + objectID + " for connection: " + connection); return object; } });
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; }
public Object read (Kryo kryo, Input input, Class type) { int objectID = input.readInt(true); Connection connection = (Connection)kryo.getContext().get("connection"); return ObjectSpace.getRemoteObject(connection, objectID, type); } }
public void putAll (ObjectMap<K, V> map) { ensureCapacity(map.size); for (Entry<K, V> entry : map.entries()) put(entry.key, entry.value); }
public Registration getRegistration (Class type) { if (type == memoizedClass) return memoizedClassValue; Registration registration = classToRegistration.get(type); if (registration != null) { memoizedClass = type; memoizedClassValue = registration; } return registration; }
/** Resets unregistered class names, references to previously serialized or deserialized objects, and the * {@link #getGraphContext() graph context}. If {@link #setAutoReset(boolean) auto reset} is true, this method is called * automatically when an object graph has been completely serialized or deserialized. If overridden, the super method must be * called. */ public void reset () { depth = 0; if (graphContext != null) graphContext.clear(); classResolver.reset(); if (references) { referenceResolver.reset(); readObject = null; } copyDepth = 0; if (originalToCopy != null) originalToCopy.clear(2048); if (TRACE) trace("kryo", "Object graph complete."); }
/** * {@inheritDoc} */ @SuppressWarnings("unchecked") @Override public void write(Kryo kryo, Output output, T object) { ObjectMap<Object, Object> garphContext = kryo.getGraphContext(); if (Boolean.FALSE.equals(garphContext.get(KryoSerializationPreferences.WRITE_INVOCATION_AFFILIATION_DATA))) { Map<Long, MutableInt> temp = object.getInvocationsParentsIdMap(); object.setInvocationsParentsIdMap(Collections.<Long, MutableInt> emptyMap()); super.write(kryo, output, object); object.setInvocationsParentsIdMap(temp); } else { super.write(kryo, output, object); } } }
/** Increases the size of the backing array to acommodate the specified number of additional items. Useful before adding many * items to avoid multiple backing array resizes. */ public void ensureCapacity (int additionalCapacity) { int sizeNeeded = size + additionalCapacity; if (sizeNeeded >= threshold) resize(nextPowerOfTwo((int)(sizeNeeded / loadFactor))); }
public boolean containsKey (K key) { int hashCode = key.hashCode(); int index = hashCode & mask; if (!key.equals(keyTable[index])) { index = hash2(hashCode); if (!key.equals(keyTable[index])) { index = hash3(hashCode); if (!key.equals(keyTable[index])) { if (isBigTable) { index = hash4(hashCode); if (!key.equals(keyTable[index])) return containsKeyStash(key); } else { return containsKeyStash(key); } } } } return true; }
public boolean containsKey (K key) { int hashCode = key.hashCode(); int index = hashCode & mask; if (!key.equals(keyTable[index])) { index = hash2(hashCode); if (!key.equals(keyTable[index])) { index = hash3(hashCode); if (!key.equals(keyTable[index])) return containsKeyStash(key); } } return true; }