|
| 1 | +/* |
| 2 | + * Copyright 2020-Present The Serverless Workflow Specification Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.serverlessworkflow.impl.model.func; |
| 17 | + |
| 18 | +import io.serverlessworkflow.impl.marshaller.CustomObjectMarshaller; |
| 19 | +import io.serverlessworkflow.impl.marshaller.WorkflowInputBuffer; |
| 20 | +import io.serverlessworkflow.impl.marshaller.WorkflowOutputBuffer; |
| 21 | +import java.io.ByteArrayInputStream; |
| 22 | +import java.io.ByteArrayOutputStream; |
| 23 | +import java.io.IOException; |
| 24 | +import java.io.ObjectInputStream; |
| 25 | +import java.io.ObjectOutputStream; |
| 26 | +import java.io.Serializable; |
| 27 | +import java.io.UncheckedIOException; |
| 28 | + |
| 29 | +public class SerializableObjectMarshaller implements CustomObjectMarshaller<Serializable> { |
| 30 | + |
| 31 | + @Override |
| 32 | + public void write(WorkflowOutputBuffer buffer, Serializable object) { |
| 33 | + try (ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); |
| 34 | + ObjectOutputStream out = new ObjectOutputStream(bytesOut)) { |
| 35 | + out.writeObject(object); |
| 36 | + buffer.writeBytes(bytesOut.toByteArray()); |
| 37 | + } catch (IOException io) { |
| 38 | + throw new UncheckedIOException(io); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public Serializable read(WorkflowInputBuffer buffer, Class<? extends Serializable> objectClass) { |
| 44 | + try (ByteArrayInputStream bytesIn = new ByteArrayInputStream(buffer.readBytes()); |
| 45 | + ObjectInputStream in = new ObjectInputStream(bytesIn)) { |
| 46 | + return objectClass.cast(in.readObject()); |
| 47 | + } catch (IOException io) { |
| 48 | + throw new UncheckedIOException(io); |
| 49 | + } catch (ClassNotFoundException ex) { |
| 50 | + throw new IllegalStateException(ex); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public Class<Serializable> getObjectClass() { |
| 56 | + return Serializable.class; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public int priority() { |
| 61 | + return Integer.MAX_VALUE; |
| 62 | + } |
| 63 | +} |
0 commit comments