
package com.esotericsoftware.kryo.serializers;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.KryoException;
import com.esotericsoftware.kryo.KryoSerializable;
import com.esotericsoftware.kryo.Serializer;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;

/** Serializes objects using Java's built in serialization mechanism. Note that this is very inefficient and should be avoided if
 * possible.
 * @see Serializer
 * @see FieldSerializer
 * @see KryoSerializable
 * @author Nathan Sweet <misc@n4te.com> */
public class JavaSerializer extends Serializer {
	private ObjectOutputStream objectStream;
	private Output lastOutput;

	public void write (Kryo kryo, Output output, Object object) {
		try {
			if (output != lastOutput) {
				objectStream = new ObjectOutputStream(output);
				lastOutput = output;
			} else
				objectStream.reset();
			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 {
			return new ObjectInputStream(input).readObject();
		} catch (Exception ex) {
			throw new KryoException("Error during Java deserialization.", ex);
		}
	}
}

@@ -49,81 +49,8 @@
 %0D%0A%0D%0A
-import java.io.ObjectInputStream;%0D%0Aimport java.io.ObjectOutputStream;%0D%0A%0D%0A
 impo
@@ -320,16 +320,139 @@
 Output;%0D
+%0Aimport com.esotericsoftware.kryo.util.ObjectMap;%0D%0A%0D%0Aimport java.io.ObjectInputStream;%0D%0Aimport java.io.ObjectOutputStream;%0D
 %0A%0D%0A/** S
@@ -762,180 +762,241 @@
 %0D%0A%09p
-rivate ObjectOutputStream objectStream;%0D%0A%09private Output lastOutput;%0D%0A%0D%0A%09public void write (Kryo kryo, Output output, Object object) %7B%0D%0A%09%09try %7B%0D%0A%09%09%09if (output != lastOutput
+ublic void write (Kryo kryo, Output output, Object object) %7B%0D%0A%09%09try %7B%0D%0A%09%09%09ObjectMap graphContext = kryo.getGraphContext();%0D%0A%09%09%09ObjectOutputStream objectStream = (ObjectOutputStream)graphContext.get(this);%0D%0A%09%09%09if (objectStream == null
 ) %7B%0D
@@ -1056,45 +1056,31 @@
 %09%09%09%09
-lastOutput = output;%0D%0A%09%09%09%7D else%0D%0A%09%09%09%09
+graphContext.put(this, 
 obje
@@ -1091,17 +1091,16 @@
 ream
-.reset();
+);%0D%0A%09%09%09%7D
 %0D%0A%09%09
@@ -1345,43 +1345,291 @@
 %0A%09%09%09
-return new ObjectInputStream(input)
+ObjectMap graphContext = kryo.getGraphContext();%0D%0A%09%09%09ObjectInputStream objectStream = (ObjectInputStream)graphContext.get(this);%0D%0A%09%09%09if (objectStream == null) %7B%0D%0A%09%09%09%09objectStream = new ObjectInputStream(input);%0D%0A%09%09%09%09graphContext.put(this, objectStream);%0D%0A%09%09%09%7D%0D%0A%09%09%09return objectStream
 .rea

Improve Java serialization, reuse object stream for entire graph.