[
https://issues.apache.org/jira/browse/APEXMALHAR-1984?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15120891#comment-15120891
]
ASF GitHub Bot commented on APEXMALHAR-1984:
--------------------------------------------
Github user siyuanh commented on a diff in the pull request:
https://github.com/apache/incubator-apex-malhar/pull/178#discussion_r51085625
--- Diff:
library/src/main/java/com/datatorrent/lib/util/KryoCloneUtils.java ---
@@ -0,0 +1,153 @@
+/**
+ * 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 com.datatorrent.lib.util;
+
+import java.io.ByteArrayOutputStream;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.commons.io.IOUtils;
+
+import com.esotericsoftware.kryo.Kryo;
+import com.esotericsoftware.kryo.io.Input;
+import com.esotericsoftware.kryo.io.Output;
+
+/**
+ *
+ * A Kryo Clone Util class that clone object by using Kryo serializer and
deserializer
+ * The class has static method that are can be used directly to clone one
object
+ * Or it can be used as util instance to clone as many objects as you need
from the one source object
+ *
+ * @since 3.4.0
+ */
+public class KryoCloneUtils<T>
+{
+
+ /**
+ * Reusable Kryo object as deserializer
+ */
+ private final Kryo kryo;
+
+ /**
+ * Reusable binary data for object that would be deserialized from
+ */
+ private final byte[] bin;
+
+ /**
+ * The class of the object
+ */
+ private final Class<T> clazz;
+
+ private KryoCloneUtils(Kryo kryo, T t)
+ {
+ this.kryo = kryo;
+ ByteArrayOutputStream bos = null;
+ Output output = null;
+ try {
+ bos = new ByteArrayOutputStream();
+ output = new Output(bos);
+ kryo.writeObject(output, t);
+ output.close();
+ bin = bos.toByteArray();
+ } finally {
+ IOUtils.closeQuietly(output);
+ IOUtils.closeQuietly(bos);
+ }
+ clazz = (Class<T>)t.getClass();
+ kryo.setClassLoader(clazz.getClassLoader());
+ }
+
+ /**
+ * Clone from the binary data of the source object
+ * @return
+ */
+ public T getClone()
+ {
+ try (Input input = new Input(bin)) {
+ return kryo.readObject(input, clazz);
+ }
+ }
+
+ /**
+ * Clone array of objects from source object
+ * @param num
+ * @return
+ */
+ public T[] getClones(int num)
+ {
+ List<T> result = new ArrayList<>();
--- End diff --
I don't think compiler is able to compile because of type erasure
> Operators that use Kryo directly would throw exception in local mode
> --------------------------------------------------------------------
>
> Key: APEXMALHAR-1984
> URL: https://issues.apache.org/jira/browse/APEXMALHAR-1984
> Project: Apache Apex Malhar
> Issue Type: Bug
> Reporter: Siyuan Hua
> Assignee: Siyuan Hua
>
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)