public void writeClass(Class<?> clazz) { ObjectSerializationHandler handler = ObjectSerializerRegistry.getHandlerByObjectType(clazz); ObjectTypeMapper mapper = ObjectSerializerRegistry.getMapperByObjectType(clazz); if (handler == null && clazz.isArray()) { // we may have special handlers for certain types of arrays // if handler is null, treat like any other array writeByte(OBJECT_TYPE_ARRAY); writeClass(clazz.getComponentType()); } else if (mapper == null) { throw new ConnectorException("No serializer for class: " + clazz); } else { String typeName = mapper.getHandledSerialType(); writeByte(OBJECT_TYPE_CLASS); writeString(typeName, true); } }
public void writeObject(ObjectEncoder encoder, Object object) { if (firstObject) { writeInt(OBJECT_MAGIC); writeInt(ENCODING_VERSION); firstObject = false; } // push the stack OutputBuffer objectBuffer; { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream data = new DataOutputStream(baos); objectBuffer = new OutputBuffer(baos, data); outputBufferStack.push(objectBuffer); } if (object == null) { writeByte(OBJECT_TYPE_NULL); } else { Class<?> clazz = object.getClass(); writeClass(clazz); ObjectSerializationHandler handler = ObjectSerializerRegistry.getHandlerByObjectType(clazz); if (handler == null) { // we may have special handlers for certain types of arrays // if handler is null, treat like any other array if (clazz.isArray()) { int length = Array.getLength(object); for (int i = 0; i < length; i++) { Object val = Array.get(object, i); startAnonymousField(); writeObject(encoder, val); endField(); } } else { throw new ConnectorException("No serializer for class: " + clazz); } } else { handler.serialize(object, encoder); } } writeByte(FIELD_TYPE_END_OBJECT); // write end-object into the // current obj buffer // pop the stack outputBufferStack.pop(); // it's a top-level object, flush the constant pool if (outputBufferStack.size() == 0) { writeInt(constantBuffer.size()); for (String constant : constantBuffer) { writeString(constant, false); writeInt(constantPool.get(constant)); } constantBuffer.clear(); } // now write the actual object try { objectBuffer.second.close(); } catch (IOException e) { throw ConnectorException.wrap(e); } byte[] bytes = objectBuffer.first.toByteArray(); writeBytes(bytes); }