@Override public Object read(Kryo kryo, Input input, Class type) { try { ObjectMap graphContext = kryo.getGraphContext(); ObjectInputStream objectStream = (ObjectInputStream) graphContext.get(this); if (objectStream == null) { objectStream = new ObjectInputStream(input) { @Override protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException { return ClassUtils.getClass(KryoSerialization.class.getClassLoader(), desc.getName()); } }; graphContext.put(this, objectStream); } return objectStream.readObject(); } catch (Exception ex) { throw new KryoException("Error during Java deserialization.", ex); } }
@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); } }
@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 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(); } }
/** Creates a new map with the specified initial capacity and load factor. This map will hold initialCapacity * loadFactor items * before growing the backing table. */ public ObjectIntMap (int initialCapacity, float loadFactor) { if (initialCapacity < 0) throw new IllegalArgumentException("initialCapacity must be >= 0: " + initialCapacity); if (initialCapacity > 1 << 30) throw new IllegalArgumentException("initialCapacity is too large: " + initialCapacity); capacity = ObjectMap.nextPowerOfTwo(initialCapacity); if (loadFactor <= 0) throw new IllegalArgumentException("loadFactor must be > 0: " + loadFactor); this.loadFactor = loadFactor; threshold = (int)(capacity * loadFactor); mask = capacity - 1; hashShift = 31 - Integer.numberOfTrailingZeros(capacity); stashCapacity = Math.max(3, (int)Math.ceil(Math.log(capacity)) * 2); pushIterations = Math.max(Math.min(capacity, 8), (int)Math.sqrt(capacity) / 8); keyTable = (K[])new Object[capacity + stashCapacity]; valueTable = new int[keyTable.length]; }
/** * Creates a new map with the specified initial capacity and load factor. * This map will hold initialCapacity * loadFactor items before growing the * backing table. */ @SuppressWarnings("unchecked") public ObjectIntMap(int initialCapacity, float loadFactor) { if (initialCapacity < 0) throw new IllegalArgumentException("initialCapacity must be >= 0: " + initialCapacity); if (initialCapacity > 1 << 30) throw new IllegalArgumentException("initialCapacity is too large: " + initialCapacity); capacity = ObjectMap.nextPowerOfTwo(initialCapacity); if (loadFactor <= 0) throw new IllegalArgumentException("loadFactor must be > 0: " + loadFactor); this.loadFactor = loadFactor; threshold = (int) (capacity * loadFactor); mask = capacity - 1; hashShift = 31 - Integer.numberOfTrailingZeros(capacity); stashCapacity = Math.max(3, (int) Math.ceil(Math.log(capacity)) * 2); pushIterations = Math.max(Math.min(capacity, 8), (int) Math.sqrt(capacity) / 8); keyTable = (K[]) new Object[capacity + stashCapacity]; valueTable = new int[keyTable.length]; }
@Override public Object read(Kryo kryo, Input input, Class type) { try { ObjectMap graphContext = kryo.getGraphContext(); ObjectInputStream objectStream = (ObjectInputStream)graphContext.get(this); if (objectStream == null) { objectStream = new ObjectInputStreamWithKryoClassLoader(input, kryo); graphContext.put(this, objectStream); } return objectStream.readObject(); } catch (Exception ex) { throw new KryoException("Error during Java deserialization.", ex); } }
public void write (Kryo kryo, Output output, Object object) { try { ObjectMap graphContext = kryo.getGraphContext(); ObjectOutputStream objectStream = (ObjectOutputStream)graphContext.get(this); if (objectStream == null) { objectStream = new ObjectOutputStream(output); graphContext.put(this, objectStream); } objectStream.writeObject(object); objectStream.flush(); } catch (Exception ex) { throw new KryoException("Error during Java serialization.", ex); } }
public Object read (Kryo kryo, Input input, Class type) { try { ObjectMap graphContext = kryo.getGraphContext(); ObjectInputStream objectStream = (ObjectInputStream)graphContext.get(this); if (objectStream == null) { objectStream = new ObjectInputStream(input); graphContext.put(this, objectStream); } return objectStream.readObject(); } catch (Exception ex) { throw new KryoException("Error during Java deserialization.", ex); } }
private List<Field> buildValidFields (boolean transientFields, List<Field> allFields, ObjectMap context, IntArray useAsm) { List<Field> result = new ArrayList(allFields.size()); for (int i = 0, n = allFields.size(); i < n; i++) { Field field = allFields.get(i); int modifiers = field.getModifiers(); if (Modifier.isTransient(modifiers) != transientFields) continue; if (Modifier.isStatic(modifiers)) continue; if (field.isSynthetic() && ignoreSyntheticFields) continue; if (!field.isAccessible()) { if (!setFieldsAsAccessible) continue; try { field.setAccessible(true); } catch (AccessControlException ex) { continue; } } Optional optional = field.getAnnotation(Optional.class); if (optional != null && !context.containsKey(optional.value())) continue; result.add(field); // BOZO - Must be public? useAsm.add(!Modifier.isFinal(modifiers) && Modifier.isPublic(modifiers) && Modifier.isPublic(field.getType().getModifiers()) ? 1 : 0); } return result; }
private JavaSerializer getCachedSerializer (Class type) { if (javaSerializerByType == null) { javaSerializerByType = new ObjectMap<Class, JavaSerializer>(); return null; } return javaSerializerByType.get(type); }
private List<Field> buildValidFields (boolean transientFields, List<Field> allFields, ObjectMap context, IntArray useAsm) { List<Field> result = new ArrayList(allFields.size()); for (int i = 0, n = allFields.size(); i < n; i++) { Field field = allFields.get(i); int modifiers = field.getModifiers(); if (Modifier.isTransient(modifiers) && !transientFields) continue; if (Modifier.isStatic(modifiers)) continue; if (field.isSynthetic() && ignoreSyntheticFields) continue; if (!field.isAccessible()) { if (!setFieldsAsAccessible) continue; try { field.setAccessible(true); } catch (AccessControlException ex) { continue; } } Optional optional = field.getAnnotation(Optional.class); if (optional != null && !context.containsKey(optional.value())) continue; result.add(field); // BOZO - Must be public? useAsm.add(!Modifier.isFinal(modifiers) && Modifier.isPublic(modifiers) && Modifier.isPublic(field.getType().getModifiers()) ? 1 : 0); } return result; }
/** 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); }
/** * 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); }
public T read (Kryo kryo, Input input, Class<T> type) { T object = create(kryo, input, type); kryo.reference(object); ObjectMap context = kryo.getGraphContext(); CachedField[] fields = (CachedField[])context.get(this); if (fields == null) { int length = input.readVarInt(true); if (TRACE) trace("kryo", "Read " + length + " field names."); String[] names = new String[length]; for (int i = 0; i < length; i++) names[i] = input.readString(); fields = new CachedField[length]; CachedField[] allFields = getFields(); outer: for (int i = 0, n = names.length; i < n; i++) { String schemaName = names[i]; for (int ii = 0, nn = allFields.length; ii < nn; ii++) { if (allFields[ii].field.getName().equals(schemaName)) { fields[i] = allFields[ii]; continue outer; } } if (TRACE) trace("kryo", "Ignore obsolete field: " + schemaName); } context.put(this, fields); } InputChunked inputChunked = new InputChunked(input, 1024); boolean hasGenerics = getGenerics() != null; for (int i = 0, n = fields.length; i < n; i++) { CachedField cachedField = fields[i]; if(cachedField != null && hasGenerics) { // Generic type used to instantiate this field could have // been changed in the meantime. Therefore take the most // up-to-date definition of a field cachedField = getField(cachedField.field.getName()); } if (cachedField == null) { if (TRACE) trace("kryo", "Skip obsolete field."); inputChunked.nextChunks(); continue; } cachedField.read(inputChunked, object); inputChunked.nextChunks(); } return object; }
public T read (Kryo kryo, Input input, Class<T> type) { T object = create(kryo, input, type); kryo.reference(object); ObjectMap context = kryo.getGraphContext(); CachedField[] fields = (CachedField[])context.get(this); if (fields == null) { int length = input.readVarInt(true); if (TRACE) trace("kryo", "Read " + length + " field names."); String[] names = new String[length]; for (int i = 0; i < length; i++) names[i] = input.readString(); fields = new CachedField[length]; CachedField[] allFields = getFields(); outer: for (int i = 0, n = names.length; i < n; i++) { String schemaName = names[i]; for (int ii = 0, nn = allFields.length; ii < nn; ii++) { if (allFields[ii].field.getName().equals(schemaName)) { fields[i] = allFields[ii]; continue outer; } } if (TRACE) trace("kryo", "Ignore obsolete field: " + schemaName); } context.put(this, fields); } InputChunked inputChunked = new InputChunked(input, 1024); for (int i = 0, n = fields.length; i < n; i++) { CachedField cachedField = fields[i]; if (cachedField == null) { if (TRACE) trace("kryo", "Skip obsolete field."); inputChunked.nextChunks(); continue; } cachedField.read(inputChunked, object); inputChunked.nextChunks(); } return 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(ObjectMap.nextPowerOfTwo((int)(sizeNeeded / loadFactor))); }
/** * 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(ObjectMap.nextPowerOfTwo((int) (sizeNeeded / loadFactor))); }
/** Called when the list of cached fields must be rebuilt. This is done any time settings are changed that affect which fields * will be used. It is called from the constructor for FieldSerializer, but not for subclasses. Subclasses must call this from * their constructor. */ protected void rebuildCachedFields () { if (TRACE && generics != null) trace("kryo", "generic type parameters are: " + Arrays.toString(generics)); if (type.isInterface()) { fields = new CachedField[0]; // No fields to serialize. return; } hasObjectFields = false; // For generic classes, generate a mapping from type variable names to the concrete types // This mapping is the same for the whole class. Generics genScope = buildGenericsScope(type, generics); genericsScope = genScope; // Push proper scopes at serializer construction time if (genericsScope != null) kryo.pushGenericsScope(type, genericsScope); // Collect all fields. List<Field> allFields = new ArrayList(); Class nextClass = type; while (nextClass != Object.class) { Field[] declaredFields = nextClass.getDeclaredFields(); if (declaredFields != null) { for (Field f : declaredFields) { if (Modifier.isStatic(f.getModifiers())) continue; allFields.add(f); } } nextClass = nextClass.getSuperclass(); } ObjectMap context = kryo.getContext(); IntArray useAsm = new IntArray(); // Sort fields by their offsets if (useMemRegions && !useAsmEnabled && unsafe() != null) { Field[] allFieldsArray = softFieldsByOffset(allFields); allFields = Arrays.asList(allFieldsArray); } // TODO: useAsm is modified as a side effect, this should be pulled out of buildValidFields List<Field> validFields = buildValidFields(false, allFields, context, useAsm); List<Field> validTransientFields = buildValidFields(true, allFields, context, useAsm); // Use ReflectASM for any public fields. if (useAsmEnabled && !Util.isAndroid && Modifier.isPublic(type.getModifiers()) && useAsm.indexOf(1) != -1) { try { access = FieldAccess.get(type); } catch (RuntimeException ignored) { } } List<CachedField> cachedFields = new ArrayList(validFields.size()); List<CachedField> cachedTransientFields = new ArrayList(validTransientFields.size()); createCachedFields(useAsm, validFields, cachedFields, 0); createCachedFields(useAsm, validTransientFields, cachedTransientFields, validFields.size()); Collections.sort(cachedFields, this); fields = cachedFields.toArray(new CachedField[cachedFields.size()]); Collections.sort(cachedTransientFields, this); transientFields = cachedTransientFields.toArray(new CachedField[cachedTransientFields.size()]); initializeCachedFields(); if (genericsScope != null) kryo.popGenericsScope(); }