diff --git a/formats/avro/pom.xml b/formats/avro/pom.xml index b2dbe8d2e..bd730296e 100644 --- a/formats/avro/pom.xml +++ b/formats/avro/pom.xml @@ -33,6 +33,7 @@ 1.9.2 + 3.2.0 io.cloudevents.formats.avro @@ -41,7 +42,7 @@ org.apache.avro avro-maven-plugin - 1.10.2 + ${avro.version} generate-sources @@ -59,7 +60,7 @@ org.codehaus.mojo build-helper-maven-plugin - 3.2.0 + ${build.helper.version} add-source diff --git a/formats/avro/src/main/avro/spec.avsc b/formats/avro/src/main/avro/spec.avsc index 8d9b4994d..8b4a76c6c 100644 --- a/formats/avro/src/main/avro/spec.avsc +++ b/formats/avro/src/main/avro/spec.avsc @@ -1,5 +1,5 @@ { - "namespace":"io.cloudevents", + "namespace":"io.cloudevents.avro", "type":"record", "name":"AvroCloudEvent", "version":"1.0", @@ -61,4 +61,3 @@ } ] } - diff --git a/formats/avro/src/main/java/io/cloudevents/avro/AvroCloudEventDataWrapper.java b/formats/avro/src/main/java/io/cloudevents/avro/AvroCloudEventDataWrapper.java index cee76cc98..b95b0d7df 100644 --- a/formats/avro/src/main/java/io/cloudevents/avro/AvroCloudEventDataWrapper.java +++ b/formats/avro/src/main/java/io/cloudevents/avro/AvroCloudEventDataWrapper.java @@ -18,11 +18,11 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.util.List; import java.util.Map; import io.cloudevents.AvroCloudEventData; import io.cloudevents.CloudEventData; +import io.cloudevents.core.format.EventDeserializationException; /** * Encode JSON style cloudevent data into Avro format. @@ -30,7 +30,7 @@ */ public class AvroCloudEventDataWrapper implements CloudEventData { - private AvroCloudEventData avroCloudEventData; + private final AvroCloudEventData avroCloudEventData; /** * Wraps a JSON object-like data structure. @@ -42,13 +42,11 @@ public AvroCloudEventDataWrapper(Map data) { @Override public byte[] toBytes() { - ByteArrayOutputStream bytes = new ByteArrayOutputStream(); - try { + try (ByteArrayOutputStream bytes = new ByteArrayOutputStream()) { AvroCloudEventData.getEncoder().encode(this.avroCloudEventData, bytes); - } catch (IOException ignore) { - // ignored + return bytes.toByteArray(); + } catch (IOException e) { + throw new EventDeserializationException(e); } - - return bytes.toByteArray(); } } diff --git a/formats/avro/src/main/java/io/cloudevents/avro/AvroFormat.java b/formats/avro/src/main/java/io/cloudevents/avro/AvroFormat.java index 75f9421dd..de9d7abb8 100644 --- a/formats/avro/src/main/java/io/cloudevents/avro/AvroFormat.java +++ b/formats/avro/src/main/java/io/cloudevents/avro/AvroFormat.java @@ -37,23 +37,19 @@ public class AvroFormat implements EventFormat { @Override public byte[] serialize(CloudEvent event) throws EventSerializationException { AvroCloudEvent avroCloudEvent = AvroSerializer.toAvro(event); - ByteArrayOutputStream output = new ByteArrayOutputStream(); - try { + try (ByteArrayOutputStream output = new ByteArrayOutputStream()) { AvroCloudEvent.getEncoder().encode(avroCloudEvent, output); + return output.toByteArray(); } catch (IOException e) { throw new EventSerializationException(e); } - - return output.toByteArray(); } @Override public CloudEvent deserialize(byte[] bytes, CloudEventDataMapper mapper) throws EventDeserializationException { - ByteArrayInputStream input = new ByteArrayInputStream(bytes); - - try { + try (ByteArrayInputStream input = new ByteArrayInputStream(bytes)) { AvroCloudEvent avroCloudEvent = AvroCloudEvent.getDecoder().decode(input); return new AvroDeserializer(avroCloudEvent).read(CloudEventBuilder::fromSpecVersion, mapper); diff --git a/formats/avro/src/main/java/io/cloudevents/avro/AvroSerializer.java b/formats/avro/src/main/java/io/cloudevents/avro/AvroSerializer.java index effed9394..fa8ba0b96 100644 --- a/formats/avro/src/main/java/io/cloudevents/avro/AvroSerializer.java +++ b/formats/avro/src/main/java/io/cloudevents/avro/AvroSerializer.java @@ -28,7 +28,7 @@ class AvroSerializer { - public static final AvroCloudEvent toAvro(CloudEvent e) { + static final AvroCloudEvent toAvro(CloudEvent e) { AvroCloudEvent avroCloudEvent = new AvroCloudEvent(); Map attrs = new HashMap<>();