|
| 1 | +/* |
| 2 | + * Copyright 2018-Present The CloudEvents Authors |
| 3 | + * <p> |
| 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 | + * <p> |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * <p> |
| 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 | + */ |
| 17 | +package io.cloudevents.avro; |
| 18 | + |
| 19 | +import java.io.ByteArrayOutputStream; |
| 20 | +import java.io.IOException; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import io.cloudevents.AvroCloudEventData; |
| 25 | +import io.cloudevents.CloudEventData; |
| 26 | + |
| 27 | +/** |
| 28 | + * Encode JSON style cloudevent data into Avro format. |
| 29 | + * |
| 30 | + */ |
| 31 | +public class AvroCloudEventDataWrapper implements CloudEventData { |
| 32 | + |
| 33 | + private AvroCloudEventData avroCloudEventData; |
| 34 | + |
| 35 | + /** |
| 36 | + * Wraps a JSON object-like data structure. |
| 37 | + */ |
| 38 | + public AvroCloudEventDataWrapper(Map<CharSequence, Object> data) { |
| 39 | + avroCloudEventData = new AvroCloudEventData(); |
| 40 | + avroCloudEventData.setValue(data); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public byte[] toBytes() { |
| 45 | + ByteArrayOutputStream bytes = new ByteArrayOutputStream(); |
| 46 | + try { |
| 47 | + AvroCloudEventData.getEncoder().encode(this.avroCloudEventData, bytes); |
| 48 | + } catch (IOException ignore) { |
| 49 | + // ignored |
| 50 | + } |
| 51 | + |
| 52 | + return bytes.toByteArray(); |
| 53 | + } |
| 54 | +} |
0 commit comments