/** * Reset the collection of objects already dumped by the receiver. If the * objects are found again in the object graph, the receiver will dump them * again, instead of a handle (cyclic reference). * */ private void resetSeenObjects() { objectsWritten = new SerializationHandleMap(); currentHandle = baseWireHandle; }
public int put(Object key, int value) { Object _key = key; int _value = value; int index = findIndex(_key, keys); // if the key doesn't exist in the table if (keys[index] != _key) { if (++size > threshold) { rehash(); index = findIndex(_key, keys); } // insert the key and assign the value to -1 initially keys[index] = _key; values[index] = -1; } // insert value to where it needs to go, return the old value int result = values[index]; values[index] = _value; return result; }
/** * Dumps the parameter {@code obj} only if it is {@code null} * or an object that has already been dumped previously. * * @param obj * Object to check if an instance previously dumped by this * stream. * @return -1 if it is an instance which has not been dumped yet (and this * method does nothing). The handle if {@code obj} is an * instance which has been dumped already. * * @throws IOException * If an error occurs attempting to save {@code null} or * a cyclic reference. */ private int dumpCycle(Object obj) throws IOException { // If the object has been saved already, save its handle only int handle = objectsWritten.get(obj); if (handle != -1) { writeCyclicReference(handle); return handle; } return -1; }
/** * Remove the unshared object from the table, and restore any previous * handle. * * @param obj * Non-null object being dumped. * @param previousHandle * The handle of the previous identical object dumped */ private void removeUnsharedReference(Object obj, int previousHandle) { if (previousHandle != -1) { objectsWritten.put(obj, previousHandle); } else { objectsWritten.remove(obj); } }
int result; Object object; index = next = findIndex(key, keys); hash = getModuloHash(object, length); hashedOk = hash > index; if (next < index) {
public SerializationHandleMap() { this.size = 0; this.threshold = 21; // Copied from IdentityHashMap. int arraySize = (int) (((long) threshold * 10000) / LOAD_FACTOR); resizeArrays(arraySize); }
public int get(Object key) { int index = findIndex(key, keys); if (keys[index] == key) { return values[index]; } return -1; }
private int registerObjectWritten(Object obj) { int handle = nextHandle(); objectsWritten.put(obj, handle); return handle; }
/** * Returns the index where the key is found at, or the index of the next * empty spot if the key is not found in this table. */ private int findIndex(Object key, Object[] array) { int length = array.length; int index = getModuloHash(key, length); int last = (index + length - 1) % length; while (index != last) { if (array[index] == key || array[index] == null) { /* * Found the key, or the next empty spot (which means key is not * in the table) */ break; } index = (index + 1) % length; } return index; }
/** * Sets the specified protocol version to be used by this stream. * * @param version * the protocol version to be used. Use a {@code * PROTOCOL_VERSION_x} constant from {@code * java.io.ObjectStreamConstants}. * @throws IllegalArgumentException * if an invalid {@code version} is specified. * @throws IOException * if an I/O error occurs. * @see ObjectStreamConstants#PROTOCOL_VERSION_1 * @see ObjectStreamConstants#PROTOCOL_VERSION_2 */ public void useProtocolVersion(int version) throws IOException { if (!objectsWritten.isEmpty()) { throw new IllegalStateException("Cannot set protocol version when stream in use"); } if (version != ObjectStreamConstants.PROTOCOL_VERSION_1 && version != ObjectStreamConstants.PROTOCOL_VERSION_2) { throw new IllegalArgumentException("Unknown protocol: " + version); } protocolVersion = version; }
/** * Remove the unshared object from the table, and restore any previous * handle. * * @param obj * Non-null object being dumped. * @param previousHandle * The handle of the previous identical object dumped */ private void removeUnsharedReference(Object obj, int previousHandle) { if (previousHandle != -1) { objectsWritten.put(obj, previousHandle); } else { objectsWritten.remove(obj); } }
int result; Object object; index = next = findIndex(key, keys); hash = getModuloHash(object, length); hashedOk = hash > index; if (next < index) {
private void rehash() { int newSize = keys.length * 2; resizeArrays(newSize); threshold = (int) ((long) (keys.length) * LOAD_FACTOR / 10000); }
private void resizeArrays(int newSize) { Object[] oldKeys = keys; int[] oldValues = values; this.keys = new Object[newSize]; this.values = new int[newSize]; if (oldKeys != null) { for (int i = 0; i < oldKeys.length; ++i) { Object key = oldKeys[i]; int value = oldValues[i]; int index = findIndex(key, keys); keys[index] = key; values[index] = value; } } }
objectsWritten.put(array, handle);
/** * Returns the index where the key is found at, or the index of the next * empty spot if the key is not found in this table. */ private int findIndex(Object key, Object[] array) { int length = array.length; int index = getModuloHash(key, length); int last = (index + length - 1) % length; while (index != last) { if (array[index] == key || array[index] == null) { /* * Found the key, or the next empty spot (which means key is not * in the table) */ break; } index = (index + 1) % length; } return index; }
/** * Sets the specified protocol version to be used by this stream. * * @param version * the protocol version to be used. Use a {@code * PROTOCOL_VERSION_x} constant from {@code * java.io.ObjectStreamConstants}. * @throws IllegalArgumentException * if an invalid {@code version} is specified. * @throws IOException * if an I/O error occurs. * @see ObjectStreamConstants#PROTOCOL_VERSION_1 * @see ObjectStreamConstants#PROTOCOL_VERSION_2 */ public void useProtocolVersion(int version) throws IOException { if (!objectsWritten.isEmpty()) { throw new IllegalStateException("Cannot set protocol version when stream in use"); } if (version != ObjectStreamConstants.PROTOCOL_VERSION_1 && version != ObjectStreamConstants.PROTOCOL_VERSION_2) { throw new IllegalArgumentException("Unknown protocol: " + version); } protocolVersion = version; }
/** * Remove the unshared object from the table, and restore any previous * handle. * * @param obj * Non-null object being dumped. * @param previousHandle * The handle of the previous identical object dumped */ private void removeUnsharedReference(Object obj, int previousHandle) { if (previousHandle != -1) { objectsWritten.put(obj, previousHandle); } else { objectsWritten.remove(obj); } }
public int put(Object key, int value) { Object _key = key; int _value = value; int index = findIndex(_key, keys); // if the key doesn't exist in the table if (keys[index] != _key) { if (++size > threshold) { rehash(); index = findIndex(_key, keys); } // insert the key and assign the value to -1 initially keys[index] = _key; values[index] = -1; } // insert value to where it needs to go, return the old value int result = values[index]; values[index] = _value; return result; }
int result; Object object; index = next = findIndex(key, keys); hash = getModuloHash(object, length); hashedOk = hash > index; if (next < index) {