rpuch commented on a change in pull request #516: URL: https://github.com/apache/ignite-3/pull/516#discussion_r773113859
########## File path: modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/DefaultUserObjectMarshaller.java ########## @@ -0,0 +1,208 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.internal.network.serialization.marshal; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.Externalizable; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.lang.reflect.Method; +import java.util.List; +import org.apache.ignite.internal.network.serialization.ClassDescriptor; +import org.apache.ignite.internal.network.serialization.ClassDescriptorFactory; +import org.apache.ignite.internal.network.serialization.ClassDescriptorFactoryContext; + +/** + * Default implementation of {@link UserObjectMarshaller}. + */ +public class DefaultUserObjectMarshaller implements UserObjectMarshaller { + private final ClassDescriptorFactoryContext descriptorRegistry; + private final ClassDescriptorFactory descriptorFactory; + + public DefaultUserObjectMarshaller(ClassDescriptorFactoryContext descriptorRegistry, ClassDescriptorFactory descriptorFactory) { + this.descriptorRegistry = descriptorRegistry; + this.descriptorFactory = descriptorFactory; + } + + /** {@inheritDoc} */ + @Override + public MarshalledObject marshal(Object object) throws MarshalException { + ClassDescriptor descriptor = getOrCreateDescriptor(object); + + var baos = new ByteArrayOutputStream(); + + final List<ClassDescriptor> usedDescriptors; + try (var dos = new DataOutputStream(baos)) { + writeDescriptorId(descriptor, dos); + + usedDescriptors = writeObject(object, descriptor, dos); + } catch (IOException e) { + throw new MarshalException("Cannot marshal", e); + } + + return new MarshalledObject(baos.toByteArray(), usedDescriptors); + } + + private ClassDescriptor getOrCreateDescriptor(Object object) { Review comment: I'm not sure that we need to add javadocs for each and every private method when their purpose is obvious from their signatures. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
