public Registration writeClass (Output output, Class type) {
		if (output == null) throw new IllegalArgumentException("output cannot be null.");
		try {
			if (type == null) {
				if (DEBUG) log("Write", null);
				output.writeByte(NULL);
				return null;
			}
			Registration registration = getRegistration(type);
			if (registration.getId() == NAME) {
				output.writeByte(NAME + 2);
				int nameId = classToNameId.get(type, -1);
				if (nameId != -1) {
					if (TRACE) trace("kryo", "Write class name reference " + nameId + ": " + className(type));
					output.writeInt(nameId, true);
					return registration;
				}
				// Only write the class name the first time encountered in object graph.
				if (TRACE) trace("kryo", "Write class name: " + className(type));
				nameId = nextNameId++;
				classToNameId.put(type, nameId);
				output.write(nameId);
				output.writeString(type.getName());
			} else {
				if (TRACE) trace("kryo", "Write class " + registration.getId() + ": " + className(type));
				output.writeInt(registration.getId() + 2, true);
			}
			return registration;
		} finally {
			if (depth == 0) reset();
		}
	}